@teppeis/nock-back-mocha
v3.0.0
Published
Use nock.back with your mocha tests with minimal hassle.
Downloads
124
Readme
@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.
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'sbeforeEach
.Context.afterEach: () => void
: Tear down nock.back. Call in mocha'safterEach
.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 <[email protected]>
Derived from porchdotcom/nock-back-mocha under ICS license