最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Jest Encountered Unexpected Token 'export' in Node Module "@kubernetesclient-node&quot

programmeradmin2浏览0评论

I am running a Jest(Version:^29.7.0) test suite in a Node.js v22 environment using TypeScript. Used "@kubernetes/client-node" library version: "^1.0.0". When I try to execute my tests, I encounter the following error:

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  for how to enable it.
   • If you are trying to use TypeScript, see 
   • 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:
  
  For information about custom transformations, see:
  

  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

{
  "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"
    ]
}

Jest is trying to execute a file that uses ES module (import/export) syntax, but Jest is not properly configured to handle ES modules, leading to the Unexpected token 'export' error.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论