0

I have a Next.js project with TypeScript.

  1. It's a page router instead of an app router
  2. 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);      ...  });
gilly's user avatar
gilly
677 bronze badges
askedSep 12 at 0:28
User A's user avatar

2 Answers2

0

You can't export like this

You need to import the component first and then exportRailRadar – Live Train Tracking Platform

Talha's user avatar
Talha
94812 silver badges24 bronze badges
answeredSep 12 at 9:12
Vishal Kaleria's user avatar
Sign up to request clarification or add additional context in comments.

1 Comment

As it’s currently written, your answer is unclear. Pleaseedit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answersin the help center.
0

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:

  1. Configurets-jest (or your transformer) to support ESM by settinguseESM: true.

  2. AddtransformIgnorePatterns 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.

answeredSep 13 at 18:45
Rahul Singh's user avatar

Comments

Your Answer

Sign up orlog in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

By clicking “Post Your Answer”, you agree to ourterms of service and acknowledge you have read ourprivacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.