https://github.com/npm/tap-nock
a tap extension providing nock integration
https://github.com/npm/tap-nock
npm-cli
Last synced: 9 months ago
JSON representation
a tap extension providing nock integration
- Host: GitHub
- URL: https://github.com/npm/tap-nock
- Owner: npm
- License: other
- Archived: true
- Created: 2022-04-19T18:20:41.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2024-07-22T23:30:57.000Z (almost 2 years ago)
- Last Synced: 2024-10-01T16:06:28.913Z (over 1 year ago)
- Topics: npm-cli
- Language: JavaScript
- Homepage:
- Size: 65.4 KB
- Stars: 6
- Watchers: 6
- Forks: 3
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE.md
- Code of conduct: CODE_OF_CONDUCT.md
- Codeowners: .github/CODEOWNERS
- Security: SECURITY.md
Awesome Lists containing this project
README
## `@npmcli/tap-nock`
A [tap](https://node-tap.org) extension that integrates [nock](https://github.com/nock/nock).
### Features
* `t.nock()`
* automatically calls `nock.disableNetConnect()`
* arguments are passed directly to `nock`, and the result from `nock` returned
* asserts all nock scopes created within a test have been consumed
* cleans only the nock scopes created within the current test, keeping nocks from parent tests
* if no parent tests have nocks, calls `nock.enableNetConnect()` in teardown
* `t.nock.snapshot()`
* when [snapshots are enabled](https://node-tap.org/docs/api/snapshot-testing/#testing-output-with-snapshots), sends real requests and records responses to a fixture
* when snapshots are not enabled, loads fixture data and sets up `nock` scopes for you
### Usage
```js
const fetch = require('minipass-fetch')
const tapNock = require('@npmcli/tap-nock')
const t = tapNock(require('tap'))
t.test('sends a request', async (t) => {
t.nock('https://registry.npmjs.org')
.get('/')
.reply(200, { hello: 'world' })
const res = await fetch('https://registry.npmjs.org')
t.equal(res.status, 200)
const body = await res.json()
t.same(body, { hello: 'world' })
})
// when snapshots are enabled, this test will send a real request and record the response
// when they are disabled, the recorded response will be automatically loaded into a nock
// scope, and the test will receive that response
t.test('snapshots a request', async (t) => {
t.nock.snapshot()
const res = await fetch('https://registry.npmjs.org')
t.equal(res.status, 200)
const body = await res.json()
t.match(body, { db_name: 'registry' })
})
```