Provides an alternative mock implementation to jest.fn()

Downloads in past


0.3.56 years ago7 years agoMinified + gzip package size for jest-deep-mock in KB


Jest Deep Mock
npm version Build status
Provides an alternative mock implementation to jest.fn() allowing to walk a mock object without specifying the value at each level. If no value is set for a property or no return value when calling a mock function, a new mock object will be returned.
The interface follows and the same expects can be done per

Getting Started

Install jest-deep-mock using npm:
npm install --save-dev jest-deep-mock

Write your first test using DeepMock:
const DeepMock = require("jest-deep-mock")

test('set return value and assert response', () => {
    // GIVEN
    let mock = new DeepMock()

    // WHEN
    let actual = mock.nested.func(1, 2)

    // THEN

    expect(mock.nested.func).toHaveBeenCalledWith(1, 2)

Add the following section to your package.json:
  "scripts": {
    "test": "jest"

Run npm test to run the test with the following output:
PASS  ./example.test.js
 ✓ set return value and assert response (11ms)

Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        1.005s
Ran all test suites.