heisennock
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
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 = ; // recommended by nock's documentationconst myNock = ; ; // 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 = ; // hijacking nock's APIlet body;const myNock = ; ; // => now we'll get a proper diff and stacktrace, and know exactly why we failedtodeep;
"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 = ; // Now we get to keep both:// 1) the lean API of the original nockconst myNock = ; ; // 2) a simple way of doing a proper maintainable assertiontodeep;
Install
$ npm install heisennock
Use
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 = ;
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 = ; ; // simple APIs for single calls (covers most cases)to;to;to; todeep;todeep;todeep;
Support for multiple calls
// array APIs for multiple callsto;todeep;todeep;todeep;todeep;
Supported nock features
replyWithError()
Same as the original method from nock
.
const myNock = ;
replyWithFile()
Same as the original method from nock
.
const myNock = ;
times()
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 = ; const mySecondNock = ;
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