Skip to content

neverendingqs-sandbox/node-nock-example

master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
src
 
 
 
 
 
 
 
 
 
 

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)

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published