Nock
HTTP server mocking and expectations library for Node.js — intercept outgoing requests in tests.
Pricing
Free / Open source
Type
Automation
Languages
JavaScript, TypeScript
// VERDICT
Reach for Nock when you want to intercept and mock outgoing HTTP from Node tests deterministically. Skip it when you're not in Node, want browser-level mocking (MSW), or need to test against the real service.
Best for
HTTP server mocking for Node - intercepting outgoing HTTP requests and returning canned responses, so tests run without hitting real external services.
Avoid when
You're not in Node, you want to mock at the browser/network level (MSW), or you need real integration against the service.
CI/CD fit
Library in Node tests · intercepts HTTP · deterministic CI
Languages
JavaScript · TypeScript
Team fit
Node/JS teams · Testing code that calls HTTP APIs · Deterministic unit tests
Setup
Maintenance
Learning
Licence
// BEST FOR
- Intercepting outgoing HTTP requests in Node
- Returning canned responses for external APIs
- Making tests deterministic without real services
- Asserting requests were made as expected
- Simulating errors/timeouts from dependencies
- Fast, isolated unit tests of API-calling code
// AVOID WHEN
- You're not in Node
- Browser/network-level mocking is needed (MSW)
- You need real integration against the service
- You want a standalone mock server (WireMock/Mockoon)
- End-to-end realism is required
- Minimal mocking suffices
// QUICK START
npm install -D nock
// nock('https://api.example.com').get('/users/1').reply(200, {id:1});// ALTERNATIVES TO CONSIDER
// FEATURES
- Intercept HTTP requests at the Node.js http module level
- Match by method, URL, headers, and request body
- Replay recorded fixtures with nock.recorder
- Strict allow-no-unmocked mode to catch unintended calls
- Per-test isolation via nock.cleanAll and nock.restore
// PROS
- Zero network in tests — fast and deterministic
- Mature, widely used in the Node.js ecosystem
- Recording mode makes fixture creation easy
- Composes naturally with Mocha, Jest, Vitest, and Tap
// CONS
- Node.js-only — does not intercept browser requests
- Doesn't intercept fetch by default in older Node versions
- Mismatched interceptors can produce confusing test failures
// EXAMPLE QA WORKFLOW
Install Nock
Declare intercepts in test setup
Return canned responses (incl. errors)
Assert requests were made as expected
Clean up intercepts between tests
Keep mocks aligned with the real contract