jest-nock-back
TypeScript icon, indicating that this package has built-in type declarations

0.2.1 • Public • Published

Jest Nock Back

Record HTTP fixtures (Network Mocks) for your tests. This is simply a light Jest-wrapper around Nock's nock-back feature.

Heavily inspired by jest-nock

Usage

Configure Jest to setup this module.

// setup.js
import { JestNockBack } from "../src/index";
 
JestNockBack({
  jasmine,
  global
});
// jest.config.js
module.exports = {
  // ...
  setupFilesAfterEnv: ["./setup.ts"]
  // ...
};

Write tests as normal, except for tests you will want to mock, should append .nock to the test definition. Works for (it, beforeEach et. al., though only tested for it)

// my-test.js
import Axios from "axios";
 
describe("e2e", () => {
  it.nock("should record fixtures for http calls", async () => {
    const example = await Axios.get("https://example.com");
    expect(example.status).toBe(200);
    expect(example.data).toMatchSnapshot();
  });
});

By default, this will hit the network and perform normally.

To record fixtures, run the tests in record mode.

$ TEST_MODE=record jest

If a fixture is found for a given test (it.nock), all requests will be played back from the filesystem and it will not hit the network. If a request is not found, Nock will give a connection error. This is by design, and likely an indicator that you need to fix your test or your fixture. To rerecord, simply remove the offending fixture, and run tests in record mode again.

This will generate fixture files in the directory you configured (test/fixtures be default).

Options

See here for examples of how nock options can be used: https://github.com/nock/nock#options-1

interface IJestNockBackOptions {
  defaultMode?: NockBackMode;
  fixtureDir?: string;
  jasmine: any;
  global: any;
  generateMockName?(
    testTitle: string,
    testPath: string,
    fixtureDirectory: string
  ): string;
  nock?: Partial<NockBackOptions>;
}

Readme

Keywords

none

Package Sidebar

Install

npm i jest-nock-back

Weekly Downloads

602

Version

0.2.1

License

MIT

Unpacked Size

14.1 kB

Total Files

10

Last publish

Collaborators

  • jonjaques