jest Errors

20 error patterns

jest3 fixes

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
jest3 fixes

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
jest3 fixes

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()
jest3 fixes

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
jest3 fixes

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
jest3 fixes

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
jest3 fixes

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')
jest3 fixes

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
jest3 fixes

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
jest3 fixes

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
jest3 fixes

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
jest3 fixes

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
jest3 fixes

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
jest3 fixes

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
jest3 fixes

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
jest3 fixes

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
jest3 fixes

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
jest3 fixes

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
jest3 fixes

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
jest3 fixes

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