New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

jest-mock-recorder

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

jest-mock-recorder

A tool to record and replay jest mocks

  • 3.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
94
decreased by-61%
Maintainers
1
Weekly downloads
 
Created
Source

Jest Mock Recorder • latest version release

npm status

Inspired by Nock, this library allows you to record and replay functions (usually libraries) in your jest tests. By doing so, you don't have to mock your dependencies while testing your code without relying on external services.

Installation

npm i -S jest-mock-recorder

Usage

The environment variable MOCK_RECORDER controls how jest-mock-recorder behaves. If it is set to record, jest-mock-recorder will record and save the calls to the original function. If it is not set, or set to other values, jest-mock-recorder will only replay the recorded calls and throw an error if no recorded call for the function is available.

import { mockClass } from "jest-mock-recorder";
import { ExampleDatabaseClient } from "example-database-client";

beforeAll(() => {
  mockClass(ExampleDatabaseClient, "query"); // <-- just this one line
});

test("getting user from database", async () => {
  ///
  /// .. some code
  ///

  // the ExampleDatabaseClient that is used by your code
  // will be mocked. If there are recordings available, it will use the
  // recording instead of calling the real function. Otherwise,
  // it will call the function and record the returning value.
  await expect(yourFunctionThatUsesDatabaseClient(a, b, c, d)).resolves.toEqual(
    expectedResult
  );
});

or if you need to restore the original implementation of the class function:

import { mockClass } from "jest-mock-recorder";
import { ExampleDatabaseClient } from "example-database-client";

test("getting user from database", async () => {
  const restore = mockClass(ExampleDatabaseClient, "query"); // <-- just this one line
  ///
  /// .. some code
  ///

  // do some test
  await expect(yourFn(a, b, c, d)).resolves.toEqual(expectedResult);

  // need to restore?
  restore();

  //
  // do other stuff here...
  //

  //
  // you can mock it again later:
  const restore2 = mockClass(ExampleDatabaseClient, "query");

  // do other stuff
});

FAQs

Package last updated on 04 Nov 2022

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc