Skip to content

Instantly share code, notes, and snippets.

@tobythetester
Last active May 1, 2016 12:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tobythetester/45c45e19bdb4abf99e32ecd2bb5a421a to your computer and use it in GitHub Desktop.
Save tobythetester/45c45e19bdb4abf99e32ecd2bb5a421a to your computer and use it in GitHub Desktop.
Mocked Time Out
var nock = require('nock');
var request = require('supertest')("http://api.postcodes.io");
var expect = require('chai').expect;
describe("Testing API with a mocked backend", function () {
it("returns a successful mocked response", function (done) {
//override the mocha timeout in this test to 60 seconds
this.timeout(60000);
//setup mocked backend for a specific end point
nock("http://api.postcodes.io")
.get('/postcodes/')
//simulate a 10 second delay
.delayBody(10000)
.reply(200, {
"status": 200,
"message": "This simulates a 10 second delay"
});
//perform the request to the api which will now be intercepted by nock
request
.get('/postcodes/')
.end(function (err, res) {
expect(res.body.status).to.equal(200);
expect(res.body.message).to.equal("This simulates a 10 second delay");
done();
});
})
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment