jest test suite failed to run: unexpected token
Fixes Jest 'unexpected token' suite failures: transforms, ESM, and file types. Use when the suite fails to run at all with a syntax error. Not for individual test failures.
TL;DR
Jest failed to parse a file, usually an untransformed ESM import or a non-JS asset. Find which file and token the error names, add the transform or moduleNameMapper entry, and re-run.
Error
Test suite failed to run
Jest encountered an unexpected token
/app/node_modules/some-lib/index.js:1
({"Object.[anonymous]":function(module,exports,require,__dirname,__filename,jest){import x from 'y';
^^^^^^Steps
- Read the error: it names the file and the token (
import,export,<for JSX/SVG). Expected: you know what failed to parse. - If it is ESM in nodemodules, add it to
transformIgnorePatternsexceptions: `transformIgnorePatterns: ['nodemodules/(?!(some-lib)/)']`. Expected: Jest transforms that package. - If it is an asset (svg, css, image), map it:
moduleNameMapper: { '[.](svg|css)$': '[rootDir]/test/__mocks__/fileMock.js' }. Expected: imports resolve to a stub. - If it is your own ESM source, ensure babel-jest or ts-jest is configured for it. Expected: your code transforms.
- Re-run the single suite. Expected: the suite runs instead of failing to parse.
When to use
Test suite failed to runwithunexpected token.- After adding a new dependency or asset import.
When not to use
- Tests run but fail (assertion or logic failures).
- TypeScript type errors (tsc, not Jest transform).
Tool compatibility
- Jest 27 through 30; babel-jest, ts-jest.
Variant phrasings
Jest encountered an unexpected token import
The ESM variant; fix with transformIgnorePatterns.
Jest unexpected token export
Same fix from the export side.
Why it happens
Jest transforms files with babel by default but skips node_modules. Any ESM or non-JS syntax outside the transform pipeline crashes the parser.
Edge cases
transformIgnorePatternsis a denylist; the regex exception syntax is easy to get backwards.- Dual CJS/ESM packages need the right
main/moduleresolution; check the package's exports. - Vitest handles ESM natively; migrating avoids this whole class of error.
Provenance
Resolved from the public thread: https://vectle.com/posts/pst_cNkoLABm3803XTlm07II2g
Maintainer review
No maintainer verification is recorded for this version.
This records the version a maintainer checked. It does not assert that the version is the latest upstream release.