🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Sign inDemoInstall
Socket

jest-nock-back

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

jest-nock-back

Record HTTP fixtures (**N**etwork M**ocks**) for your tests. This is simply a light Jest-wrapper around [Nock](https://github.com/nock/nock)'s [nock-back](https://github.com/nock/nock#nock-back) feature.

0.2.1
latest
Source
npm
Version published
Weekly downloads
58
-7.94%
Maintainers
1
Weekly downloads
 
Created
Source

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>;
}

FAQs

Package last updated on 29 Jul 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts