This lesson takes a deeper look at the specific Jest mistakes that come up again and again across real codebases, explaining not just what to avoid, but why each mistake happens and how to fix it.
Mistake #1: Unreturned Promises in Tests
The single most damaging Jest mistake is a test that never actually waits for its async code, letting the test framework consider it "passed" before an important assertion even runs. This happens because JavaScript doesn't force you to await or return a promise — the code still runs, just not in time for Jest to notice a failure.
The fix is mechanical: always return or await any promise containing an assertion, and use expect.assertions(n) as a safety net for error-handling tests specifically.
// Mistake
test('rejects for invalid input (broken)', () => {
fetchUser(-1).catch((err) => {
expect(err.message).toBe('Invalid id'); // may never actually run in time
});
});
// Fix
test('rejects for invalid input (fixed)', async () => {
await expect(fetchUser(-1)).rejects.toThrow('Invalid id');
});
The fixed version both awaits the assertion and uses a more direct matcher for the rejection.
.toBe() checks reference identity, not content — two separately created objects are never === equal.
.toEqual() recursively compares contents, which is what you almost always actually want for objects/arrays.
This mistake is especially common for developers coming from languages where ==/.equals() behave differently.
A quick mental rule: primitives use .toBe(); objects and arrays use .toEqual().
Common Mistakes Cheat Sheet
The most frequent Jest mistakes and their direct fixes.
Mistake
Fix
Unreturned promise in a test
return/await the promise, or use .resolves/.rejects
.toBe() on objects/arrays
Use .toEqual() instead
Leftover test.only()
Remove before committing; consider a lint rule
Mock state leaking between tests
jest.clearAllMocks()/resetAllMocks() in afterEach()
jest.runAllTimers() with setInterval
Use jest.advanceTimersByTime() instead
Blindly running jest -u
Review the snapshot diff first
Vague test names
Name tests after specific observable behavior
Mistake #3: Leftover test.only()
test.only() is meant for temporary local debugging, but it's easy to forget to remove before committing — silently disabling every other test in that file in CI, sometimes for a long time before anyone notices coverage quietly dropped.
// Committed by accident, disables every other test in the file
test.only('this one test', () => { /* ... */ });
test('this test never runs anymore', () => { /* ... */ });
The eslint-plugin-jest rule jest/no-focused-tests catches this automatically before it's committed.
Mistake #4: Mock State Leaking Between Tests
Because mock call history and configured return values persist by default across tests within the same file, a mock configured in one test can silently affect a later test that assumes a fresh, unconfigured mock — producing confusing, order-dependent failures.
afterEach(() => {
jest.clearAllMocks(); // clears call history
// jest.resetAllMocks(); // also clears mockReturnValue/mockImplementation
});
Common Mistakes
Not returning/awaiting promises inside tests, hiding real assertion failures.
Using .toBe() for deep equality checks on objects and arrays.
Committing test.only() and silently disabling the rest of a test file.
Letting mock configuration and call history leak between tests without resetting.
Key Takeaways
Most impactful Jest mistakes involve async handling, equality matchers, or mock state leaking.
Each of these mistakes has a simple, mechanical fix once you recognize the pattern.
Lint rules (eslint-plugin-jest) can catch several of these mistakes automatically.
Reviewing test files as carefully as production code catches most of these before they merge.
Pro Tip
Install eslint-plugin-jest's recommended config in every project. Rules like no-focused-tests, valid-expect, and no-identical-title catch a surprising share of the mistakes covered in this lesson automatically.
You now understand the most common Jest mistakes in depth. Next, use the full Jest cheat sheet as a quick reference for everything covered in this course.