-
Notifications
You must be signed in to change notification settings - Fork 27.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
nextjs/TypeScript with Jest: "SyntaxError: Cannot use import statement outside a module" for some packages #58707
Comments
You are probably using babel-jest for transforming TS and TSX files. Take a look at your jest config file and in the "transform" option try switching to ts-jest for these two file types.
|
Update:When I use nextjs 14.0.2, the error disappear. The cause could be the version 14.0.3 |
I'm seeing the same issue as @thesonpb with Note: My issue is not when running Not sure how to fix this, or if it should be reported as a separate issue. For now, I'm pinning 14.0.2. ✓ Creating an optimized production build
✓ Compiled successfully
Collecting page data ../app/node_modules/antd/es/spin/index.js:1
import _extends from "@babel/runtime/helpers/esm/extends";
^^^^^^
SyntaxError: Cannot use import statement outside a module
at internalCompileFunction (node:internal/vm:73:18)
at wrapSafe (node:internal/modules/cjs/loader:1153:20)
at Module._compile (node:internal/modules/cjs/loader:1197:27)
at Module._extensions..js (node:internal/modules/cjs/loader:1287:10)
at Module.load (node:internal/modules/cjs/loader:1091:32)
at Module._load (node:internal/modules/cjs/loader:938:12)
at Module.require (node:internal/modules/cjs/loader:1115:19)
at mod.require (/app/node_modules/next/dist/server/require-hook.js:64:28)
at require (node:internal/modules/helpers:130:18)
at 2600 (/app/.next/server/pages/admin.js:1:2698)
> Build error occurred
Error: Failed to collect page data for /admin
at /app/node_modules/next/dist/build/utils.js:1217:15
at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
type: 'Error'
} |
@victorandree, @thesonpb I cannot confirm that this is connected to the issue I am posting. Changing to the nextjs version you mentioned does not help for me. What ultimately ended up working for me is to add the problematic packages into jest.config.ts const tsconfig = require("./tsconfig.json");
const moduleNameMapper = require("tsconfig-paths-jest")(tsconfig)
module.exports = {
preset: 'ts-jest',
setupFilesAfterEnv: ['<rootDir>/jest.setup.js'],
moduleNameMapper,
testEnvironment: 'jest-environment-jsdom',
"transform": {
"^.+\\.(ts|tsx)$": "ts-jest",
'^.+\\.(js|jsx)$': 'babel-jest'
},
"transformIgnorePatterns": [
"node_modules/(?!(ora|chalk|cli-cursor))",
],
} |
@erikmuttersbach Seems likely. I've reported my issue as a new one: #58817. |
I also got this issue, I am pretty new to setting up Jest so I hope someone can help me
this code I follow the jest example in Nextjs source code, I got this issue when running jest to test component render
|
transpilePackages option fixed this issue for me #58817 (comment) |
Verify canary release
Provide environment information
Operating System: Platform: darwin Arch: arm64 Version: Darwin Kernel Version 23.1.0: Mon Oct 9 21:28:12 PDT 2023; root:xnu-10002.41.9~6/RELEASE_ARM64_T8103 Binaries: Node: 21.1.0 npm: 10.2.0 Yarn: 1.22.19 pnpm: N/A Relevant Packages: next: 14.0.4-canary.5 eslint-config-next: N/A react: 18.2.0 react-dom: 18.2.0 typescript: 5.2.2 Next.js Config: output: N/A
Expected Behavior
Using the same packages for running the application works. I was expecting jest to behave the same
To Reproduce
Start with a clean setup:
Introduce breaking changes:
Edit
app/client/page.test.tsx
(or any other test file) and use chalk:Now
npx jest
fails:The text was updated successfully, but these errors were encountered: