- index.js is a module which sends requests to the server and this module has to be tested.
- server.js is used to create a server which responds with a delay of 1600 ms.
- test/withoutnock.test.js contains test which when run, lets the module send actual requests to server.
- test/withnock.test.js contains test which when run, intercepts the request made by the module.
- Open server using
npm run server
. - Run tests in watch mode using
npm run test:watch
.
- When you run the tests you'll notice that withoutnock.test.js takes around 1600 ms to complete
- However, withnock.test.js takes around 5 ms only.
- Also if you monitor the server logs, they will show that only one request was received by the server when the tests were run.
- 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.