Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

Jest fails with SyntaxError: Unexpected token when testing a Next.js component

ErrorJest encountered an unexpected token

Short 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.

  • Applies to Next.js 15, Next.js 16, Jest 30, Node.js 22
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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"
  2. 2Match what you see, then jump to its fix

    1. 1Details 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. 2Details 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. 3Details 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

What does the error look like?

  • Jest encountered an unexpected token
  • Jest 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 jest

Confirmed by nextjs.org

Did this fix it?

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/null

2Fix

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.tsx

Confirmed by nextjs.org

Did this fix it?

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 jest

Confirmed by jestjs.io

Did this fix it?

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

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.