The `jest-mock` package is a fundamental component of the Jest testing framework, providing the core utilities for creating, controlling, and asserting on mock functions, spies, and module mocks. Currently stable at version 30.3.0, it's released as part of the broader Jest project, which is evolving towards more frequent major updates after a significant three-year cycle for Jest 30. This library enables developers to isolate units of code for testing by replacing dependencies with controlled, test-specific implementations. Its key differentiators include deep integration with the Jest runner, comprehensive APIs for various mocking scenarios (functions, objects, modules), automatic mock resets, and robust TypeScript definitions, all designed to facilitate predictable and efficient unit and integration testing.
npm install jest-mockVerified import paths — ran on the pinned version, not inferred.
Demonstrates creating mock functions with `jest.fn`, spying on existing functions with `jest.spyOn`, overriding mock implementations, and asserting on mock calls and return values, including type safety with `MockedFunction`.
Review your `jest.config.js` or CLI arguments. If you need mock state to persist, explicitly set `clearMocks: false`. Otherwise, ensure your tests are properly isolated and do not depend on previous mock state.
Update tests to align with the `modern` timer implementation. If necessary, you can revert to the legacy implementation by calling `jest.useFakeTimers('legacy')` or configuring `timers: 'legacy'` in `jest.config.js`.If your mock factory needs access to variables, define them outside the test file or use `jest.doMock` within a `beforeEach` or `test` block, which is not hoisted but is more complex for module mocking. For dynamic values in mocks, consider using `jest.mock` with a factory that returns a function that then uses context from the test.
Ensure the target method/property is defined on the object before calling `jest.spyOn`. For dynamic methods or properties that might not exist, consider using `jest.fn()` to create a standalone mock and inject it, or using `Object.defineProperty` to add the property before spying.
Prefer a consistent module system (either all ESM `import`/`export` or all CommonJS `require`/`module.exports`) within a given test file and its dependencies where mocking is involved. When mocking ESM, use dynamic `import()` or the `unstable_mockModule` API where appropriate.
Ensure `someFunction` is a Jest mock by creating it with `jest.fn()` or `jest.spyOn()`. If it's an imported function, either spy on it or use `jest.mock()` to replace the module.
Verify the spelling of 'methodName' and ensure the property exists on `object` before `jest.spyOn` is invoked. If the property is created dynamically, ensure it's created *before* the spy. For non-existent properties you want to mock, use `jest.fn()` directly.
Ensure your `jest.mock` factory function explicitly returns an object that represents the mocked module's exports, or a function if you're mocking a default export that is a function. Example: `jest.mock('./my-module', () => ({ default: jest.fn(() => 'mocked') }));`Define variables needed by `jest.mock` factories in a separate file imported by the test, or pass them as parameters if using `jest.doMock` within a `beforeEach` block (which avoids hoisting but changes the mocking scope).
No dependency data recorded yet.