@teppeis/nock-back-mocha

3.0.0 • Public • Published

@teppeis/nock-back-mocha

Thin wrapper around nock.back that uses different nock files for each test, and cleans up when the mocha test is done.

npm version Node.js Version Support build status dependency status License

Install

$ npm i -D @teppeis/nock-back-mocha

Usage

const nockBackMocha = require('nock-back-mocha')();

describe('Tests!', () => {
  beforeEach(nockBackMocha.beforeEach);
  afterEach(nockBackMocha.afterEach);

  it('makes an http request', () =>
    fetch('http://example.com').then(() => {
      // "/path/to/__nock_fixtures__/Tests!/makes\ an\ http\ request.json"
      console.log(nockBackMocha.fixtureFile);
      nockBackMocha.assertScopesFinished();
    }));
});

API: factory(directory: string?): Context

  • directory: string?: Where do you want the nock files stored? The default is __nock_fixtures__ in the same directory.
  • Context.beforeEach: (this: MochaContext) => Promise<undefined>: Setup nock.back. Call in mocha's beforeEach.
  • Context.afterEach: () => void: Tear down nock.back. Call in mocha's afterEach.
  • Context.fixtureFile: string: Path to the fixture file for the current test.
  • Context.assertScopesFinished: () => void: Assert all nocks in fixture were satisfied. See nock document.

Pro Tip

Use in combination with env NOCK_BACK_MODE to generate http fixtures for your tests

License

MIT License: Teppei Sato <teppeis@gmail.com>

Derived from porchdotcom/nock-back-mocha under ICS license

Readme

Keywords

none

Package Sidebar

Install

npm i @teppeis/nock-back-mocha

Weekly Downloads

5

Version

3.0.0

License

MIT

Unpacked Size

5.2 kB

Total Files

3

Last publish

Collaborators

  • teppeis