Skip to content

itaditya/nock-with-jest

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

A simple demo to showoff Nock usage with Jest

Explainer

  1. index.js is a module which sends requests to the server and this module has to be tested.
  2. server.js is used to create a server which responds with a delay of 1600 ms.
  3. test/withoutnock.test.js contains test which when run, lets the module send actual requests to server.
  4. test/withnock.test.js contains test which when run, intercepts the request made by the module.

How To Run

  1. Open server using npm run server.
  2. Run tests in watch mode using npm run test:watch.

What to make of it

  1. When you run the tests you'll notice that withoutnock.test.js takes around 1600 ms to complete
  2. However, withnock.test.js takes around 5 ms only.
  3. Also if you monitor the server logs, they will show that only one request was received by the server when the tests were run.
  4. All this happens because in withnock.test.js nock intercepts the requests and don't actually send the request to server, hence don't wait 1600 ms for the server to respond.