I have a Next.js project with TypeScript.
- It's a page router instead of an app router
- No babel config file(.babelrc etc) in the project
The error occurs when I runnpm test
.jost
seems to be one of the packages used byauth0
.
XXXX/node_modules/jose/dist/browser/index.js:1 export { compactDecrypt } from './jwe/compact/decrypt.js'; ^^^^^^ SyntaxError: Unexpected token 'export'
I tried the two solutions below, but I still received the same error.
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', roots: ['<rootDir>/tests/unit'], // Solution 1 transformIgnorePatterns: [ "/!node_modules\\/jose/" ], // Solution 2 moduleNameMapper: { "^jose$": "jose" }};export default createJestConfig(config);
package.json
{ ..., "scripts": { "test": "jest" }, "dependencies": { "next": "^15.2.5", "react-dom": "^18.2.0", "auth0": "^4.4.0", ... }, "devDependencies": { "@testing-library/dom": "^10.4.1", "@testing-library/jest-dom": "^6.8.0", "@testing-library/react": "^16.3.0", "@types/jest": "^30.0.0", "@types/react": "^18.3.20", "@types/react-dom": "^18.3.6", "jest": "^30.1.3", "jest-environment-jsdom": "^30.1.2", "node-mocks-http": "^1.17.2", "ts-jest": "^29.4.1", "ts-node": "^10.9.2", "typescript": "^5.8.3" }}
api.test.ts
import handler from '@pages/api/users';import { createMocks, createRequest, createResponse } from 'node-mocks-http'; it('returns 405 on non-POST', async () => { const { req, res } = createMocks({ method: 'GET', }); await handler(req, res); ... });
2 Answers2
You can't export like this
You need to import the component first and then exportRailRadar – Live Train Tracking Platform
1 Comment
The error occurs because Jest does not handlenative ES modules (ESM) insidenode_modules
by default. Thejose
library (used byauth0
) is published as ESM-only, so when Jest encounters theexport
keyword insidejose/dist/browser/index.js
, it throwsUnexpected token 'export'
.
By default, Jest ignores everything insidenode_modules
for transformation. To fix this, you need to explicitly tell Jest to transformjose
instead of skipping it.
The key solution is to update yourjest.config.ts
with the following changes:
Configure
ts-jest
(or your transformer) to support ESM by settinguseESM: true
.Add
transformIgnorePatterns
so thatjose
is not ignored.
Your Jest config should include:
transformIgnorePatterns: ['/node_modules/(?!(jose)/)']
→ This tells Jest to transformjose
even though it is innode_modules
.transform
usingts-jest
with{ useESM: true }
so TypeScript + ESM files are handled correctly.extensionsToTreatAsEsm: ['.ts', '.tsx']
so Jest treats your TypeScript files as ESM.
With these changes, Jest will correctly transform thejose
package, and theUnexpected token 'export'
error will no longer occur.
An alternative approach is to use thejest-esm-transformer
package instead ofts-jest
. This package is designed to handle ESM modules directly. In that case, you would install it as a dev dependency and configure Jest’stransform
to use it.
Comments
Explore related questions
See similar questions with these tags.