Skip to content

Mock Implementation Incorrect

Error message

                Mock returned unexpected value — test assertion failed
              

Quick summary

The mock's return value doesn't match what the test expects. The mock setup (mockReturnValue, mockResolvedValue) has the wrong value.

Why this happens

  • mockReturnValue or mockResolvedValue was given the wrong value.

  • The mock implementation doesn't match the real function's behavior.

  • The mock was reconfigured between tests without resetting.

  • mockImplementationOnce was used but the mock was called more times than configured.

Minimal example

✗ Broken code
const mockFetch = jest.fn().mockResolvedValue({ id: 1 });
test('gets user', async () => {
  const user = await getUser(5);
  expect(user.id).toBe(5); // fails — mock always returns id: 1
});
✓ Fixed code
test('gets user', async () => {
  mockFetch.mockResolvedValue({ id: 5 }); // match expected id
  const user = await getUser(5);
  expect(user.id).toBe(5);
});

Configure the mock to return the correct value that matches the test expectation.

How to diagnose

  • Check the mock setup — does mockReturnValue/mockResolvedValue return the right value?

  • Verify the mock is called the expected number of times.

  • Check if mockReset() or mockClear() is needed between tests.

  • Compare the mock's return value to what the real function would return.

How to fix

  • Fix the mock return value to match what the test expects.

  • Use mockImplementation for more complex mock behavior.

  • Add mockReset() in beforeEach to ensure clean mock state.

  • Use mockResolvedValueOnce for sequential different return values.

How to prevent

  • Review mock setup carefully — the mock should return realistic test data.

  • Use beforeEach to reset mocks between tests.

Related Resources

Related Glossary

Related Lessons

  • Mocking

    Using mocks to isolate code.

← Back to language