6.0.1 • Published 4 months ago

@tapjs/nock v6.0.1

Weekly downloads
-
License
BlueOak-1.0.0
Repository
github
Last release
4 months ago

@tapjs/nock

A tap extension that integrates 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, 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

Load the @tapjs/nock plugin in your tap config.

For example, in .taprc:

plugins:
  - @tapjs/nock

Or in package.json:

{
  "tap": {
    "plugins": ["@tapjs/nock"]
  }
}

Then use it in your tests like so:

import t from '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
// This requires that the @tapjs/snapshot plugin is enabled.
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' })
})

You may use it directly, though that is a bit more clunky, because it won't be automatically applied to child tests, but it can be good if you only need nock in a small number of tests.

import { plugin as tapNock } from '@tapjs/nock'
import t from 'tap'

t.test('sends a request', async t => {
  const tn = tapNock(t)
  tn.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
// This requires that the @tapjs/snapshot plugin is enabled.
t.test('snapshots a request', async t => {
  const tn = tapNock(t)
  tn.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' })
})

Credits

This plugin was originally developed by nlf as the standalone extension @npmcli/tap-nock, and the use case was a major inspiration for tap's plugin eventual architecture.

6.0.1

4 months ago

4.0.5

1 year ago

4.0.4

1 year ago

4.0.7

1 year ago

4.0.6

1 year ago

4.0.1

1 year ago

4.0.3

1 year ago

4.0.2

1 year ago

5.0.3

1 year ago

5.0.2

1 year ago

5.0.1

1 year ago

5.0.0

1 year ago

6.0.0

12 months ago

4.0.8

1 year ago

4.0.0

1 year ago

3.1.22

1 year ago

3.1.21

1 year ago

3.1.20

1 year ago

3.1.19

1 year ago

3.1.18

1 year ago

3.1.17

2 years ago

3.1.16

2 years ago

3.1.15

2 years ago

3.1.14

2 years ago

3.1.13

2 years ago

3.1.12

2 years ago

3.1.11

2 years ago

3.1.10

2 years ago

3.1.9

2 years ago

3.1.8

2 years ago

3.1.7

2 years ago

3.1.6

2 years ago

3.1.5

2 years ago

3.1.4

2 years ago

3.1.3

2 years ago

3.1.2

2 years ago

3.1.1

2 years ago

3.1.0

2 years ago

3.0.7

2 years ago

3.0.6

2 years ago

3.0.5

2 years ago

3.0.4

2 years ago

3.0.3

2 years ago

3.0.2

2 years ago

3.0.1

2 years ago

3.0.0

2 years ago

3.0.0-22

2 years ago

3.0.0-21

2 years ago

3.0.0-20

2 years ago

3.0.0-19

2 years ago

3.0.0-18

2 years ago

3.0.0-17

2 years ago

3.0.0-16

2 years ago

3.0.0-15

2 years ago

3.0.0-14

2 years ago

3.0.0-13

2 years ago

3.0.0-12

2 years ago

3.0.0-11

2 years ago

3.0.0-10

2 years ago

3.0.0-9

2 years ago

3.0.0-8

2 years ago

3.0.0-7

2 years ago

3.0.0-6

2 years ago

3.0.0-5

2 years ago

3.0.0-4

2 years ago

3.0.0-3

2 years ago

3.0.0-2

2 years ago

3.0.0-1

2 years ago