Mistake: Mocking Too Much
Question
What is wrong with this test?
Code
test('calculate', () => {
const mockAdd = jest.fn().mockReturnValue(5);
const mockSub = jest.fn().mockReturnValue(3);
const result = calculate(mockAdd, mockSub);
expect(result).toBe(8);
});
Hint
If you mock everything, you're testing the mocks, not the code.
Explanation
The mistake is mocking too much. By replacing add and sub with mocks that return fixed values, you're testing that calculate calls them and sums the results — but you're not testing whether add and sub actually work. If the real add function has a bug, this test won't catch it. Only mock external dependencies (database, network), not the code you're testing.
Related Resources
Related Lessons
- Mocking
Using mocks to isolate code.
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.
Build real projects
Apply what you learned by building guided projects with starter code and solutions.