TDD-friendly HTTP mocking for nodeJS
Last updated a year ago by sabiwara .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install heisennock 
SYNC missed versions from official npm registry.


Build Status codecov Greenkeeper badge

heisennock is a TDD-friendly wrapper for the awesome HTTP mocking library nock. It totally removes the assertion logic from nock, focusing on the mocking and making contract checking a breeze by using the assertion library of your choice (chai...).

"I am the one who nocks." Walter White, a.k.a. Heisenberg

I am the one who knocks

Contract checking?

Asserting on mocks (HTTP or other stubs) call parameters is the key to do proper contract checking. Too often, developers tend to squeeze this part because it does not bring any "coverage". However, mocking without asserting on parameters is nothing but a coverage lie, leaving the code vulnerable to possible regressions whch could easily be avoided.

Why a wrapper?

While nock is perfect for mocking outbound HTTP calls, it unfortunately also tries to handle the assertion part. The resulting mix between matching rules and assertions can end up making TDD very unpratical:

  • we do not know for sure the callback was called and how many times
  • no helpful stacktrace (because the assertion is done in a callback)
  • no helpful diff

See the following snippet for a concrete example.

const nock = require('nock');

// recommended by nock's documentation
const myNock = nock('http://localhost:9876')
  .post('/some/path', { message: 'Say my name!' })


// test a call with the body: { message: 'Wrong message :(' }
// => ends up with a `Nock: No match for request ...` Error, no helpful stacktrace

For a more TDD-friendly way of failing, we could come up with the following hack:

const nock = require('nock');

// hijacking nock's API
let body;
const myNock = nock('http://localhost:9876')
  .post('/some/path', _body => {
    body = _body;  // use a closure to "capture" the body
    return true;  // lie to the nock matcher so we can handle our assertion later


// => now we'll get a proper diff and stacktrace, and know exactly why we failed
expect(body).to.deep.equal({ message: 'Say my name!' });

"Oh my god, this is ugly!"

This is far more maintainable and makes for more robust tests. But, this is indeed ugly and discourages developers to do proper contract checking.

This is where Heisennock comes in:

const { hnock } = require('heisennock');

// Now we get to keep both:
// 1) the lean API of the original nock
const myNock = hnock('http://localhost:9876')


// 2) a simple way of doing a proper maintainable assertion
expect(myNock.body()).to.deep.equal({ message: 'Say my name!' });


$ npm install heisennock


Always clean after your tests

For maintainable tests, we recommend you to always call cleanAll after each test. This way you always leave a non-polluted HTTP layer behind you.

With mocha, this will is typically done as follows:

const { hnock } = require('heisennock');

describe('...', () => {
  afterEach(() => hnock.cleanAll());

  // the tests

Always perform assertions

As said above, it is critical to perform proper contract checking on your mocks. Always assert that the following items are matching your expectations:

  • numbers of calls
  • headers
  • bodies
  • querystrings
const myNock = hnock('http://localhost:9876')


// simple APIs for single calls (covers most cases)
expect(myNock.header('authorization')).to.equal('Bearer token');
expect(myNock.headers('authorization', 'accept'))
  .to.deep.equal({ authorization: 'Bearer token', accept: 'application/json' });
expect(myNock.body()).to.deep.equal({ message: 'Say my name!' });
expect(myNock.query()).to.deep.equal({ q: 'heisenberg' });

Support for multiple calls

// array APIs for multiple calls
  { authorization: 'Bearer token1' },
  { authorization: 'Bearer token2' }
  { message: 'Say my name!' },
  { message: 'You\'re goddam right!' }
  { q: 'walter' },
  { q: 'white' }

Supported nock features


Same as the original method from nock.

const myNock = hnock('http://localhost:9876')
  .replyWithError({ code: 'ECONNREFUSED' });


Same as the original method from nock.

const myNock = hnock('http://localhost:9876')
  .replyWithFile(200, path.join(__dirname, 'fixture.json'), { 'Content-Type': 'application/json' });


Same as the original method from nock. times can be useful to test different behaviours on a same route, typically for a retry logic.

const myFirstNock = hnock('http://localhost:9876')
  .replyWithError({ code: 'ECONNREFUSED' });

const mySecondNock = hnock('http://localhost:9876')
  .reply(200, 'success');

By default, heisennock uses Infinity because we want multiple calls to be detected by assertion, not matching.

  • If you want to make sure your nock was called once, make an assertion on callCount
  • If you want to simulate several behaviours on a same route, use times

Current Tags

  • 0.3.1                                ...           latest (a year ago)

14 Versions

  • 0.3.1                                ...           a year ago
  • 0.2.2                                ...           a year ago
  • 0.2.1                                ...           a year ago
  • 0.2.0                                ...           a year ago
  • 0.1.10                                ...           a year ago
  • 0.1.9                                ...           a year ago
  • 0.1.8                                ...           2 years ago
  • 0.1.6                                ...           2 years ago
  • 0.1.5                                ...           2 years ago
  • 0.1.4                                ...           2 years ago
  • 0.1.3                                ...           2 years ago
  • 0.1.2                                ...           2 years ago
  • 0.1.1                                ...           2 years ago
  • 0.1.0                                ...           2 years ago
Maintainers (1)
Today 0
This Week 0
This Month 1
Last Day 0
Last Week 1
Last Month 1
Dependencies (1)
Dev Dependencies (14)
Dependents (0)

Copyright 2014 - 2016 © taobao.org |