No description, website, or topics provided.
Branch: master
Clone or download
Latest commit 0766c77 Mar 19, 2017
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src aftereach-isdone - example init. Mar 11, 2017
test/aftereach-isdone Minor formatting. Mar 19, 2017
.editorconfig
.gitignore
LICENSE Initial commit Mar 11, 2017
README.md aftereach-isdone - example init. Mar 11, 2017
package.json

README.md

node-nock-example

All examples are for nock using mocha.

Using isDone() and cleanAll() in afterEach()

Include the following to ensure nock interceptors don't affect other tests (and make debugging hard):

afterEach(function() {
  ...
  if(!nock.isDone()) {
    this.test.error(new Error('Not all nock interceptors were used!'));
    nock.cleanAll();
  }
  ...
});

See the difference in action. The bug is with the GET / five times test; it was only consuming one of the nock interceptors.

> npm test test/aftereach-isdone/server-original.js

  server-original
    √ GET / once (46ms)
    √ GET / five times
    1) GET / fails gracefully if example.com is down


  2 passing (76ms)
  1 failing

  1) server-original GET / fails gracefully if example.com is down:
     AssertionError: expected 200 to equal 503
      at test\aftereach-isdone\server-original.js:38:29
> npm test test/aftereach-isdone/server-improved.js

  server-improved
    √ GET / once (47ms)
    √ GET / five times
    1) GET / five times
    √ GET / fails gracefully if example.com is down


  3 passing (78ms)
  1 failing

  1) server-improved GET / five times:
     Error: Not all nock interceptors were used!
      at Context.<anonymous> (test\aftereach-isdone\server-improved.js:12:23)