
Nock
Mocks HTTP requests in Node.js so tests can run without calling real servers.
- Stars
- 13.1k
- Forks
- 783
- Licence
- MIT
- Language
- JavaScript
- Last update
- 7 days ago
- Type
- Library
What it is
Nock is a JavaScript library for Node.js that fakes HTTP responses during testing. It works by overriding Node's built-in HTTP request function, so code that calls external APIs can be tested without contacting those APIs. You describe which requests to expect and what replies to return.
- Mock a GitHub API call so a test checks how your code handles the response without network access
- Simulate a server error or a delayed response to see how your code recovers
- Match request bodies or headers and return different replies on repeated calls to the same URL
Great for
- Developers writing automated tests for Node.js code that makes HTTP calls
- Teams who want tests isolated from external services like third-party APIs
- Projects needing to simulate error responses or delays in a controlled way
Not ideal for
- Non-developers, since it is a code library that requires writing tests in JavaScript
- Projects not running on Node.js, because the README describes it specifically for Node.js
- Anyone needing to test against a real live server, since its purpose is to replace that server
How to get it
- 1
Install with a package manager
You'll need: Node.js
Install as a dev dependency
$ npm install --save-dev nock - 2
Run it
- Install nock as a dev dependency with npm
- In a test file, require nock with const nock = require('nock')
- Call nock with the host URL, then chain .get or .post and .reply to define a mock
- Run your test suite; requests matching the mock will receive the fake reply
- 3
Get the most out of it
- Use .persist() when one mock should answer many identical requests
- Use body matching with strings, objects, RegExp or functions to test specific payloads
- Use nock.back to record and replay fixtures, as described in the Nock Back section
- Use .isDone() or .pendingMocks() to confirm expected requests actually happened
Downloads
Strengths
- Widely used, with about 13,000 GitHub stars and an MIT license
- Flexible matching of hostnames, paths, bodies, query strings and headers
- Supports persisting or sequencing interceptors and checking whether mocks were used
Trade-offs
- Interceptors are removed after use by default, so each expected request needs its own setup unless you use persist()
- The README lists a compatibility table of which Node versions each old release supported, which means version choice matters
How it compares
| Project | Stars | Type | Level | Updated | Best for |
|---|---|---|---|---|---|
| nock (this) | 13.1k | Library | intermediate | 7 days ago | Developers writing automated tests for Node.js code that makes HTTP calls |
| msw | 18.3k | Library | intermediate | 20 days ago | Frontend developers who need to work before a backend API is ready |
| axum | 27.7k | Library | intermediate | 4 days ago | Rust developers building HTTP APIs or web services |
| kratos | 26k | Library | intermediate | 5 days ago | Go developers building a set of backend services that talk over HTTP or gRPC |
| validator.js | 23.7k | Library | beginner | 2 months ago | JavaScript developers validating form or API input on Node.js |
| chi | 23k | Library | intermediate | 19 days ago | Go developers building REST API services |
Stats synced from GitHub. The assessment above was drafted with AI from the project's README on 2026-10-29 and may miss recent changes — check the README before relying on a command. MIT licence: free to use, modify and distribute, including commercially, as long as you keep the copyright notice.