Jest fails with SyntaxError: Unexpected token when testing a Next.js component
Jest encountered an unexpected tokenShort answer
Jest only understands plain JavaScript until a transform and a few file mappings tell it more. The error means it hit JSX, TypeScript, a CSS file, an image, or an untranspiled package it cannot parse. In a Next.js project the fix is to build the config with next/jest, and to allow any ESM-only package through the transform.
Fix it fast
1Find out which cause you have
Read which file Jest names under Details. A path in your own code points to the first cause, a .css or image path to the second, and a path inside node_modules to the third.
npx jest 2>&1 | grep -A12 "Jest encountered an unexpected token"2Match what you see, then jump to its fix
- 1
Details names a .tsx, .jsx or .ts file from your own source folderThe Jest config does not use next/jest, so nothing transforms JSX or TypeScriptFix - 2
Details names a .css, .module.css, .scss or image file, and the first character shown is a CSS rule or binaryA CSS or image import reaches Jest as raw textFix - 3
Details names a path under node_modules, and the code shown starts with import or exportA package in node_modules ships ES modules or untranspiled codeFix
- 1
What does the error look like?
Jest encountered an unexpected tokenJest failed to parse a file.SyntaxError: Unexpected token
Cause 1 of 3
The Jest config does not use next/jest, so nothing transforms JSX or TypeScript
1Check
Open jest.config.ts or jest.config.js. If it is a plain object with no createJestConfig wrapper, Jest has no transform that fits Next.js code.
grep -n "next/jest" jest.config.*2Fix
Wrap your config with next/jest. The Next.js docs say it sets up the transform with the Next.js Compiler, loads .env files and loads next.config.js for you.
// jest.config.ts
import type { Config } from 'jest'
import nextJest from 'next/jest.js'
const createJestConfig = nextJest({ dir: './' })
const config: Config = {
coverageProvider: 'v8',
testEnvironment: 'jsdom',
}
export default createJestConfig(config)3Verify
Run the suite again. The parse error for your own files should be gone.
npx jestConfirmed by nextjs.org
Cause 2 of 3
A CSS or image import reaches Jest as raw text
1Check
Find which component imports the file. Jest cannot parse a stylesheet or a PNG as JavaScript.
grep -rnE "import .*\.(css|scss|png|jpg|svg)" src app components 2>/dev/null2Fix
Build the config with the next/jest wrapper (see the first cause). The Next.js docs say it automatically mocks stylesheets (.css, .module.css and the scss variants), image imports and next/font, so those files never reach the parser.
const createJestConfig = nextJest({ dir: './' })
export default createJestConfig(config)3Verify
Run only the failing test file.
npx jest path/to/failing.test.tsxConfirmed by nextjs.org
Cause 3 of 3
A package in node_modules ships ES modules or untranspiled code
1Check
next/jest ignores node_modules for transforms, and Jest's default transformIgnorePatterns does the same. A package published as untranspiled code therefore fails to parse.
npx jest 2>&1 | grep -m1 "node_modules/"2Fix
Tell Jest to transform that one package by negating it in transformIgnorePatterns. Use <rootDir> in the pattern, and with pnpm match the path under node_modules/.pnpm instead.
const config: Config = {
testEnvironment: 'jsdom',
transformIgnorePatterns: ['<rootDir>/node_modules/(?!(some-esm-package)/)'],
}3Verify
Run the suite and confirm the error no longer names that package.
npx jestConfirmed by jestjs.io
How do I stop it coming back?
- Create the Jest config from the start with the next/jest wrapper instead of a plain object.
- Keep one jest.config file; two competing configs make it unclear which one Jest reads.
- When you add a package that is ESM-only, add it to transformIgnorePatterns in the same commit.
- Test async Server Components with an end-to-end tool; the Next.js docs say Jest does not support them.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Web apps
Apollo Server rejects a request as a potential CSRF
HTTP 400 Bad Request from the GraphQL endpoint
Apollo Server's CSRF prevention is on by default.
3 causes, each with a fixWeb apps
Clerk cannot read its publishable key in a Next.js app
NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY is undefined
Clerk reads NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY.
3 causes, each with a fixWeb apps
The shell cannot find create-next-app
command not found: create-next-app
create-next-app is not meant to be installed as a shell command.
2 causes, each with a fixWeb apps
ESLint fails with: Cannot find module 'next/babel'
ESLint stops on the first file with a Babel or parser error instead of listing lint results
next/babel is a Babel preset that ships inside the next package.
3 causes, each with a fixWeb apps
Firebase Auth says the API key is not valid
Firebase: Error (auth/api-key-not-valid.-please-pass-a-valid-api-key.)
The apiKey your app sends is either empty, wrong, or blocked.
4 causes, each with a fixWeb apps
NextAuth sends the user to ?error=AccessDenied after Google sign in
error=AccessDenied
AccessDenied is not a Google or MongoDB error.
2 causes, each with a fix