Jest Mock Return Value Not Returned Fix
In this tutorial, you'll learn about Jest Mock Return Value Not Returned Fix. We cover key concepts, practical examples, and best practices.
Your mock function returns undefined instead of the value you set with mockReturnValue() — the call count is correct but the return value is missing.
The Problem
// WRONG — mixing mockReturnValue with mockImplementation
const mockFn = jest.fn();
mockFn.mockReturnValue('hello');
mockFn.mockImplementation(() => 'world');
expect(mockFn()).toBe('hello'); // FAILS — returns 'world'
Expected: "hello"
Received: "world"
mockImplementation() overrides any previously set mockReturnValue(). The last definition wins.
Step-by-Step Fix
1. Use one method consistently
// RIGHT — use mockReturnValue alone
const mockFn = jest.fn().mockReturnValue('hello');
expect(mockFn()).toBe('hello');
// RIGHT — use mockImplementation alone
const mockFn2 = jest.fn().mockImplementation(() => 'hello');
expect(mockFn2()).toBe('hello');
2. Return values with arguments
// RIGHT — conditional return with mockImplementation
const mockFn = jest.fn().mockImplementation((arg) => {
if (arg > 0) return 'positive';
return 'non-positive';
});
expect(mockFn(5)).toBe('positive');
expect(mockFn(-1)).toBe('non-positive');
3. Chain return values with mockReturnValueOnce
// RIGHT — sequence of return values
const mockFn = jest.fn()
.mockReturnValueOnce('first')
.mockReturnValueOnce('second')
.mockReturnValue('default');
expect(mockFn()).toBe('first');
expect(mockFn()).toBe('second');
expect(mockFn()).toBe('default');
expect(mockFn()).toBe('default');
4. Use mockResolvedValue for async functions
// RIGHT — async return value
const mockFetch = jest.fn().mockResolvedValue({ data: 'ok' });
const result = await mockFetch();
expect(result).toEqual({ data: 'ok' });
Expected output:
PASS tests/returnValue.test.js
✓ returns value consistently (3 ms)
✓ uses conditional returns (5 ms)
✓ chains return values (4 ms)
Prevention Tips
- Don't mix
mockReturnValue()andmockImplementation()on the same mock - Use
mockReturnValue()for static returns,mockImplementation()for logic - Chain
mockReturnValueOnce()for multi-call sequences - Use
mockResolvedValue()for async mocks - Use
mockRejectedValue()for error scenarios
Common Mistakes with mock return value
- Mixing let bindings with <- bindings in do notation, producing type errors
- Overlapping type class instances that cause GHC to reject the program with ambiguous dispatch errors
- Non-exhaustive pattern matches that compile with warnings then crash at runtime
These mistakes appear frequently in real-world JEST code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro