Nock is a robust HTTP server mocking and expectations library designed specifically for Node.js environments. It enables developers to test modules that make outbound HTTP/HTTPS requests in isolation by intercepting network traffic and responding with predefined data. The current stable release series is v14, with v14.0.12 being the latest as of April 2026. An actively developed v15 beta series introduces new features such as `passthrough()` for granular control over unmocked requests and improved error handling, but is not yet recommended for production use due to an accidental v15.0.0 release that was later deprecated. Nock maintains an active release cadence, frequently publishing bug fixes and beta updates. Its key differentiators include comprehensive control over request matching (by host, path, query, body, headers, and HTTP verb), the ability to define repeatable or one-time responses, and functionalities for recording and playing back HTTP interactions using 'nock-back' for fixture-based testing. It aims to provide deep control over the network layer to facilitate reliable unit and integration testing without relying on actual network connectivity.
npm install nockVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to mock an HTTP GET request using Nock. It sets up an interceptor for a specific URL, path, and header, defines a mock JSON response, makes a request using `fetch`, and then asserts both the received data and that all Nock expectations were met for that specific scope, finally cleaning up the mocks.
Downgrade to the latest v14 stable version or specify a v15 beta version in your `package.json`.
Ensure your Node.js environment meets the minimum requirement of `>=18.20.0 <20 || >=20.12.1` as specified in the package's `engines` field.
Refactor calls to `replyWithError(errorObject)` to ensure `errorObject` is an instance of `Error` (e.g., `new Error('Something went wrong')`).Add `afterEach(() => nock.cleanAll());` to your test suite setup to ensure a clean state before each test.
Use `nock.enableNetConnect('localhost')` or `nock.enableNetConnect('*.my-real-service.com')` to allow specified hostnames to make real network requests.Carefully review the `nock` definition and the actual outgoing request. Use `nock.pendingMocks()` to identify unmatched scopes or `nock.on('request')` and `nock.recorder.rec()` for debugging mismatch details. Ensure all parameters (host, path, method, query, body, headers) align exactly.For CommonJS, use `const nock = require('nock');`. For ESM, ensure `import nock from 'nock';` is used, as `nock` is the default export. Methods like `cleanAll()` are accessed via `nock.cleanAll()`.Verify that `nock` mocks are responding within expected timeframes. If using `delay()` or `delayConnection()`, adjust them or ensure your test runner's fake timers (e.g., `jest.runAllTimers()`) are correctly advancing.
Ensure `nock.activate()` has been called. If the mock should apply to multiple requests, add `.persist()` to the interceptor definition. Check `nock.isDone()` before assertions to confirm all mocks were consumed, or `nock.pendingMocks()` for unfulfilled expectations. Reconfirm the URL/path of the request and mock.
No dependency data recorded yet.