Security News
New Proposed CISA Mandate Would Require Critical Infrastructure to Report Ransom Payments Within 24 Hours
CISA has proposed a set of new rules that would require critical infrastructure to report cyber incidents and ransom payments.
jest-mock-props
Advanced tools
Mock module and object properties
Weekly downloads
Readme
Extends jest to allow easy mocking of object and module properties.
Install the extension using npm or yarn
npm install -D 'jest-mock-props'
Mock properties are "spies" that let you control the behavior of a property that is accessed indirectly by some other code.
These are the methods available on every mocked property spy object.
spy.mockClear()
Equivalent to mockReset.
spy.mockReset()
Removes any mocked values.
This is useful when you want to completely reset a property back to its initial value.
spy.mockRestore()
Restores the original (non-mocked) value.
This is useful when you want to mock properties in certain test cases and restore the original value in others.
spy.mockValue(value)
Accepts a value that should be result of accessing the mocked property.
Note: This is the same function used when setting the mocked property directly; e.g. obj.mockedProp = 'newValue'
spy.mockValueOnce(value)
Accepts a value that will be result of a single access to the mocked property. Can be chained so that multiple accesses produce different results. See example.
Note: When the mocked property runs out of values defined with mockValueOnce
, it will have the default value set with obj.mockedProp = 'defaultValue'
or spy.mockValue(defaultValue)
.
The jest object needs to be extended in every test file. This allows mocked properties to be reset and restored with jest.resetAllMocks
and jest.restoreAllMocks
respectively.
jest.isMockProp(object, propertyName)
Determines if the given object property has been mocked.
jest.spyOnProp(object, propertyName)
Note: This is aliased as jest.spyOn
as of v1.9.0
, overriding the existing jest.spyOn
to use spyOnProp
when spying on a regular object property.
Creates a mock property attached to object[propertyName]
and returns a mock property spy object, which controls all access to the object property. Repeating spying on the same object property will return the same mocked property spy.
Note: By default, spyOnProp
preserves the object property value. If you want to overwrite the original value, you can use jest.spyOnProp(object, propertyName).mockValue(customValue)
or jest.spyOn(object, methodName, accessType?)
to spy on a getter or a setter.
const video = {
length: 1000,
};
module.exports = video;
import * as mockProps from "jest-mock-props";
mockProps.extend(jest);
const video = require("./video");
it("mocks video length", () => {
const spy = jest.spyOn(video, "length");
spy.mockValueOnce(200)
.mockValueOnce(400)
.mockValueOnce(600);
expect(video.length).toEqual(200);
expect(video.length).toEqual(400);
video.length = 800;
expect(video.length).toEqual(800);
spy.mockReset();
expect(video.length).toEqual(1000);
expect(jest.isMockProp(video, "length")).toBe(true);
spy.mockRestore();
expect(jest.isMockProp(video, "length")).toBe(false);
});
FAQs
Mock module and object properties
The npm package jest-mock-props receives a total of 275 weekly downloads. As such, jest-mock-props popularity was classified as not popular.
We found that jest-mock-props 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
CISA has proposed a set of new rules that would require critical infrastructure to report cyber incidents and ransom payments.
Security News
Redis is no longer OSS, breaking its explicit commitment to remain under the BSD 3-Clause License forever. This has angered contributors who are now working to fork the software.
Product
Socket AI now enables 'AI detected potential malware' alerts by default, ensuring users benefit from AI-powered state-of-the-art malware detection without needing to opt-in.