Nock is an HTTP mocking and expectations library for Node.js. Test code that makes HTTP requests in isolation, without depending on a live service.
Website | Getting started | API reference | Contributing
pnpm install --save-dev nockSee the installation guide for other package managers.
import assert from 'node:assert/strict'
import nock from 'nock'
const scope = nock('https://api.github.com')
.get('/repos/atom/atom/license')
.reply(200, { license: { key: 'mit', name: 'MIT License' } })
const response = await fetch('https://api.github.com/repos/atom/atom/license')
assert.equal(response.status, 200)
assert.deepEqual(await response.json(), {
license: { key: 'mit', name: 'MIT License' },
})
scope.done()Nock intercepts the request and returns the configured response. scope.done()
asserts that the expected request was made. Interceptors are consumed after one
matching request by default; see the guide to repeating mocks
for other options.
Visit nockjs.dev for the full documentation, guides, and API reference.
Thanks for wanting to contribute! Take a look at our Contributing Guide for notes on our commit message conventions and how to run tests.
Please note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms.
Thanks goes to these wonderful people (emoji key):
This project follows the all-contributors specification. Contributions of any kind welcome!
Support this project by becoming a sponsor. Your logo will show up here with a link to your website. [Become a sponsor]
Copyright (c) 2011β2019 Pedro Teixeira and other contributors.