Jest运行测试时在@kubernetes/client-node模块中遇到Unexpected Token 'export'错误求助
Jest运行测试时在@kubernetes/client-node模块中遇到Unexpected Token 'export'错误求助
嘿,我来帮你搞定这个问题!你遇到的核心问题是@kubernetes/client-node从v1.0.0开始已经全面切换为ES模块输出了,但你的Jest配置还没跟上,没法正确解析它的export语法,导致测试跑不起来。
先理清楚你的场景:你在Node.js v22 + TypeScript环境下使用Jest 29.7.0,依赖@kubernetes/client-node ^1.0.0,运行测试时触发了如下错误:
FAIL src/modules/_common/k8s/services/k8s-storage-class.service.test.ts ● Test suite failed to run Jest encountered an unexpected token Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is not configured to support such syntax. By default "node_modules" folder is ignored by transformers. Here's what you can do: • If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/ecmascript-modules for how to enable it. • If you are trying to use TypeScript, see https://jestjs.io/docs/getting-started#using-typescript • To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config. • If you need a custom transformation specify a "transform" option in your config. • If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option. You'll find more details and examples of these config options in the docs: https://jestjs.io/docs/configuration For information about custom transformations, see: https://jestjs.io/docs/code-transformation Details: D:\project\node_modules\@kubernetes\client-node\dist\index.js:1 ({\"Object.<anonymous>\":function(module,exports,require,__dirname,__filename,jest){export * from './config.js'; ^^^^^^ SyntaxError: Unexpected token 'export' at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1505:14) at Object.<anonymous> (src/modules/_common/k8s/k8s.config.ts:2:1) at Object.<anonymous> (src/modules/_common/k8s/services/k8s-storage-class.service.test.ts:3:1)
你的现有tsconfig.json和jest.config配置如下:
tsconfig.json
{ "compilerOptions": { "module": "commonjs", "target": "es2020", "allowJs": true, "outDir": "dist", "strict": true, "moduleResolution": "node", "esModuleInterop": true, "skipLibCheck": true } }
jest config
{ "verbose": true, "transform": { "^.+\\.tsx?$": "ts-jest", "^.+\\.js$": "babel-jest", "\\.(yaml|yml)$": "jest-yaml-transform", "node_modules/@kubernetes/client-node/dist/index.js": [ "ts-jest", { "isolatedModules": true } ] }, "transformIgnorePatterns": [ "/node_modules/(?!@kubernetes/client-node)/", "/node_modules/(?!.*.js$)", "/dist/" ], "testEnvironment": "node", "testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(tsx?)$", "moduleNameMapper": { "^@root(.*)$": "<rootDir>$1", "^@configs(.*)$": "<rootDir>/configs$1", "^@constants(.*)$": "<rootDir>/src/constants$1", "^@common(.*)$": "<rootDir>/src/common$1", "^@core(.*)$": "<rootDir>/src/core$1", "^@middleware(.*)$": "<rootDir>/src/middleware$1", "^@modules(.*)$": "<rootDir>/src/modules$1", "^@utils(.*)$": "<rootDir>/src/utils$1" }, "moduleFileExtensions": [ "ts", "tsx", "js", "jsx", "json", "node" ], "setupFiles": [ "./test.config.ts" ] }
解决方案
你的配置里transformIgnorePatterns冗余且不够精准,transform里针对该包的配置也没必要这么复杂,我们可以通过以下几步调整:
- 精简并修正
transformIgnorePatterns
把Jest配置里的transformIgnorePatterns改成:
"transformIgnorePatterns": [ "/node_modules/(?!@kubernetes/client-node)" ]
这样Jest就会跳过所有node_modules里的包,除了@kubernetes/client-node,确保它会被转换处理。
- 调整
transform配置并补充Babel依赖
简化transform配置,确保JS文件(包括第三方包的)能被正确转换:
"transform": { "^.+\\.tsx?$": "ts-jest", "^.+\\.js$": ["babel-jest", { "presets": ["@babel/preset-env"] }], "\\.(yaml|yml)$": "jest-yaml-transform" }
同时需要安装必要的Babel依赖:
npm install --save-dev @babel/core @babel/preset-env babel-jest
- 添加Babel配置文件
在项目根目录新建babel.config.json,内容如下:
{ "presets": [ ["@babel/preset-env", { "targets": { "node": "current" } }] ] }
这个配置会把ES模块语法转换为当前Node.js版本支持的CommonJS语法,让Jest能顺利解析。
- 保持现有tsconfig的关键配置
确保tsconfig.json里的esModuleInterop保持为true,这个配置能帮助TypeScript处理CommonJS和ES模块之间的互操作,避免额外的导入错误。
做完这些调整后,重新运行你的Jest测试,应该就能解决Unexpected token 'export'的问题了!
备注:内容来源于stack exchange,提问作者Parameswar Mondal
相关产品推荐
相关产品推荐

