testdouble-nock
Support for testdouble.js for
users of Nock!
Installation
$ npm i -D testdouble-nock
Then, from a test helper, invoke the module and pass in both td
and nock
, like so:
global.td = require('testdouble')
global.nock = require('nock')
require('testdouble-nock')(td, nock)
We'll want to clear out our stubbings after each test, so add something like this to your test runner's beforeEach() hook:
beforeEach: function () {
td.reset.onNextReset(() => nock.cleanAll())
},
afterEach: function () {
td.reset()
}
For an example of a helper that sets up testdouble.js, testdouble-nock, and ensures td.reset() is called after each test, look at test/helper.js in this repo.
Usage
When you invoke testdouble-nock
, it adds a new top-level td.api()
function that allows you to mock an external API by exposing common HTTP methods as mocked methods. Here's an example:
describe('My API test', () => {
it('should return a 200 for status', (done) => {
const myApi = td.api('http://www.my-api.com')
td.when(myApi.get('/status')).thenReturn('All good here!')
request.get('http://www.my-api.com/status', (req, res, body) => {
assert.equals(body, 'All good here!')
done()
})
})
})
You can also verify that a call has taken place using td.verify
:
describe('My API test', () => {
it('should return a 200 for status', (done) => {
const myApi = td.api('http://www.my-api.com')
request.get('http://www.my-api.com/status', (req, res, body) => {
td.verify(myApi.get('/status'))
done()
})
})
})