Search code examples
javascriptnode.jsnock

Fail test if a network request is made (using Nock)


I have logic that conditionally makes a network request, how can I, using Nock, make a test that would fail if a network request is made? Basically asserting that 0 calls to an endpoint was made.


Solution

  • I was able to solve this by listening to a "no match" event being emitted from nock.

    nock.emitter.on('no match', (req: any) => {
      throw new Error(`Unexpected request was sent to ${req.path}`);
    });