jest Errors
20 error patterns
Jest assertion value mismatch
expect\(received\)\.toBe\(expected\)|Expected.*Received
- •Check expected vs actual values — use .toEqual() for objects instead of .toBe()
- •For floating point, use .toBeCloseTo() instead of exact equality
Jest mock function not called as expected
Expected.*mock.*to have been called|not been called|toHaveBeenCalled
- •Verify the mock is injected correctly — check the module path in jest.mock()
- •Ensure the code path that calls the function is actually reached
Jest async test timeout
Exceeded timeout.*Async callback|exceeded.*5000.*ms|jest.*timeout
- •Ensure async functions resolve: await the promise or call done()
- •Increase timeout: jest.setTimeout(10000) or third arg to it()
Jest snapshot mismatch
Snapshot.*does not match|toMatchSnapshot.*failed|1 snapshot.*failed
- •Review the diff — if change is intentional, update with: jest --updateSnapshot
- •Avoid snapshots of timestamps, random IDs, or unstable output
Jest cannot resolve module in test
Cannot find module.*from.*test|jest.*cannot.*resolve.*module
- •Configure moduleNameMapper in jest.config for path aliases
- •Add moduleDirectories: ['node_modules', 'src'] for absolute imports
Jest coverage threshold not met
Coverage.*threshold.*not met|global.*coverage.*below
- •Write tests for uncovered code paths identified in coverage report
- •Adjust thresholds in jest.config coverageThreshold if temporarily unreachable
Jest spy on non-existent or non-function property
Cannot spy.*property.*not a function|spyOn.*undefined
- •Verify the method name exists on the object being spied on
- •For getters/setters, use jest.spyOn(obj, 'prop', 'get')
DOM APIs not available in Jest test
ReferenceError.*document.*not defined|window.*not defined.*jest
- •Set testEnvironment: 'jsdom' in jest.config.js
- •Add @jest-environment jsdom docblock at top of specific test file
Jest ES Module mocking limitation
Cannot mock.*ES Module|The module factory.*jest\.mock.*not allowed
- •Use jest.unstable_mockModule() for ESM modules
- •Move jest.mock() calls to the top of file (before imports) — it's hoisted
React/Angular state update not wrapped in act()
not wrapped in act|Warning.*update.*not wrapped.*act
- •Wrap state-triggering code in act(() => { ... }) or use async act
- •Use @testing-library's findBy* queries which handle act() internally
Fetch API not available in Jest environment
TypeError.*fetch.*not a function|fetch is not defined.*test
- •Mock fetch globally: global.fetch = jest.fn()
- •Install jest-fetch-mock or msw for comprehensive HTTP mocking
Jest cannot parse ES module syntax
SyntaxError.*Unexpected token.*import|Jest.*transform.*failed
- •Configure transform in jest.config to use babel-jest or ts-jest
- •Add transformIgnorePatterns to transform specific node_modules packages
Test state leaking between test cases
Test.*isolation|tests.*sharing.*state|leaking.*between tests
- •Reset mocks in beforeEach: jest.clearAllMocks() or jest.resetAllMocks()
- •Don't mutate shared module-level variables — reinitialize in beforeEach
Jest fake timers not advancing
timer.*not advancing|setTimeout.*never called|useFakeTimers.*stuck
- •Call jest.advanceTimersByTime(ms) or jest.runAllTimers() to advance
- •Use jest.useFakeTimers() before the code that sets timers
Unhandled promise rejection in Jest test
Unhandled promise rejection.*test|promise.*rejected.*unhandled
- •Add expect(promise).rejects.toThrow() for expected rejections
- •Wrap async test body in try/catch if testing error paths
Jest mock declared after module import
Cannot.*mock.*already.*required|mock.*after.*import
- •Move jest.mock() to the top of the file — it's hoisted before imports
- •For dynamic mocking, use jest.doMock() with require() inside the test
Event listener leak in Jest tests
MaxListenersExceededWarning.*test|memory leak.*detected.*test
- •Remove event listeners in afterEach or component cleanup
- •Use --detectOpenHandles flag to identify what's keeping tests alive
Jest object equality confusion (toBe vs toEqual)
Received.*serializes.*same.*string|toEqual.*pass.*with.*deep.*equality
- •Use .toEqual() for deep equality of objects and arrays
- •.toBe() uses Object.is — same reference required for objects
Jest worker process crash
Worker.*error|jest-worker.*failed|worker.*terminated
- •Increase memory: --max-workers=2 to reduce parallel memory pressure
- •Run with --runInBand to execute tests serially for debugging
Jest mock not properly initialized
Cannot.*read.*property.*mockImplementation|mockReturnValue.*undefined
- •Ensure jest.mock() path exactly matches the import path in source code
- •Use jest.fn() to create mock functions before assigning mockReturnValue