Security News
38% of CISOs Fear They’re Not Moving Fast Enough on AI
CISOs are racing to adopt AI for cybersecurity, but hurdles in budgets and governance may leave some falling behind in the fight against cyber threats.
jest-mock-recorder
Advanced tools
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.
npm i -S jest-mock-recorder
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
A tool to record and replay jest mocks
The npm package jest-mock-recorder receives a total of 0 weekly downloads. As such, jest-mock-recorder popularity was classified as not popular.
We found that jest-mock-recorder demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
CISOs are racing to adopt AI for cybersecurity, but hurdles in budgets and governance may leave some falling behind in the fight against cyber threats.
Research
Security News
Socket researchers uncovered a backdoored typosquat of BoltDB in the Go ecosystem, exploiting Go Module Proxy caching to persist undetected for years.
Security News
Company News
Socket is joining TC54 to help develop standards for software supply chain security, contributing to the evolution of SBOMs, CycloneDX, and Package URL specifications.