Open-Awesome
CategoriesAlternativesStacksSelf-HostedExplore
Open-Awesome

© 2026 Open-Awesome. Curated for the developer elite.

TermsPrivacyAboutGitHubRSS
  1. Home
  2. Node.js
  3. Nock

Nock

MITJavaScriptv14.0.13

A comprehensive HTTP server mocking and expectations library for Node.js to test modules in isolation.

GitHubGitHub
13.1k stars754 forks0 contributors

What is Nock?

Nock is an HTTP server mocking and expectations library for Node.js that intercepts and simulates HTTP requests. It allows developers to test modules that rely on external APIs or services in isolation by mocking their responses without making actual network calls. This ensures tests are fast, reliable, and independent of external dependencies.

Target Audience

Node.js developers writing unit or integration tests for applications that make HTTP requests, such as API clients, microservices, or modules interacting with third-party services like CouchDB or Amazon APIs.

Value Proposition

Developers choose Nock for its comprehensive feature set, including precise request matching, flexible response simulation, and recording capabilities. It integrates seamlessly with Node's HTTP stack, provides detailed expectation tracking, and is widely adopted in the Node.js ecosystem for reliable test isolation.

Overview

HTTP server mocking and expectations library for Node.js

Use Cases

Best For

  • Testing API clients without hitting real endpoints
  • Simulating error responses (e.g., 4xx/5xx status codes) from external services
  • Isolating unit tests for modules that depend on HTTP requests
  • Recording and replaying HTTP interactions for consistent test fixtures
  • Mocking third-party services like GitHub, Dropbox, or AWS APIs
  • Testing race conditions and timeouts with delayed responses

Not Ideal For

  • Projects needing to mock WebSocket or non-HTTP protocols (Nock is HTTP/HTTPS only)
  • Teams using Jest without proper cleanup routines, due to memory leak risks
  • Browser-based testing environments (Nock is Node.js-specific)
  • Applications where you prefer high-level API mocking over low-level HTTP interception

Pros & Cons

Pros

Precise Request Matching

Nock intercepts requests based on hostname, path, method, query strings, headers, and body using strings, regex, or functions, as detailed in the 'Specifying hostname' and 'Specifying path' sections.

Flexible Response Simulation

It can reply with static data, JSON, files, streams, or dynamic callbacks, including custom status codes and headers, enabling realistic mock setups without network calls.

Expectation Tracking

Methods like .isDone() and .pendingMocks() help ensure all mocked requests are made, preventing false positives in tests, as explained in the 'Expectations' section.

Recording and Playback

The Nock Back feature records real HTTP interactions to generate fixtures for playback, simplifying test maintenance and setup, covered in the 'Recording' and 'Nock Back' sections.

Cons

ES Module Interception Issues

The README admits requests from ES modules aren't intercepted unless Node is preloaded with Nock via --import flag, adding complexity for modern codebases.

Memory Leaks with Jest

Nock can cause memory issues in Jest tests if nock.restore() isn't called after each suite, requiring manual cleanup that's easy to overlook.

Configuration Overhead for Libraries

Using Nock with Axios requires setting axios.defaults.adapter and adjusting Jest configs, as noted in the 'Axios' section, adding setup friction.

Frequently Asked Questions

Quick Stats

Stars13,100
Forks754
Contributors0
Open Issues54
Last commit3 days ago
CreatedSince 2011

Tags

#fixtures#test-isolation#unit-testing#http#api-testing#testing#node#mock#javascript#node-js#http-client#http-mocking

Built With

N
Node.js

Included in

Node.js65.5k
Auto-fetched 1 day ago

Related Projects

PuppeteerPuppeteer

JavaScript API for Chrome and Firefox

Stars94,169
Forks9,409
Last commit2 days ago
PlaywrightPlaywright

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

Stars87,084
Forks5,554
Last commit1 day ago
JestJest

Delightful JavaScript Testing.

Stars45,333
Forks6,646
Last commit4 days ago
MochaMocha

☕️ Classic, reliable, trusted test framework for Node.js and the browser

Stars22,882
Forks3,066
Last commit2 days ago
Community-curated · Updated weekly · 100% open source

Found a gem we're missing?

Open-Awesome is built by the community, for the community. Submit a project, suggest an awesome list, or help improve the catalog on GitHub.

Submit a projectStar on GitHub