Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 679
per_jansson Avatar asked Oct 21 '25 03:10

per_jansson


1 Answers

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}`);
});
like image 141
per_jansson Avatar answered Oct 23 '25 17:10

per_jansson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!