如何使Jest使用Node_modules中的不同编译的打字稿工作

发布于 2025-02-12 03:34:06 字数 5324 浏览 1 评论 0原文

在我的项目中,我从纱线测试中获得以下内容,

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.

    Out of the box Jest supports Babel, which will be used to transform your files into valid JS based on your Babel configuration.

    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:

    /Users/danwt/Documents/work/interchain-security/diff-model-ts/node_modules/time-span/index.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){import convertHrtime from 'convert-hrtime';
                                                                                      ^^^^^^

    SyntaxError: Cannot use import statement outside a module

      1 | import * as fs from 'fs';
    > 2 | import timeSpan from 'time-span';
...

我已经查看了许多页面,以尝试找到解决方案并在没有成功的情况下解决此问题。

我的项目是使用TypeScript和Jest的Nodejs。

//jest.config.js
export default {
  testEnvironment: 'node',
  preset: 'ts-jest/presets/default-esm',
  globals: {
    'ts-jest': {
      useESM: true,
    },
  },
  moduleNameMapper: {
    '^(\\.{1,2}/.*)\\.(m)?js$': '$1',
  },
  testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.(m)?ts$',
  coverageDirectory: 'coverage',
  collectCoverageFrom: [
    'src/**/*.ts',
    'src/**/*.mts',
    '!src/**/*.d.ts',
    '!src/**/*.d.mts',
  ],
};
//package.json
{
  "name": "node-typescript-boilerplate",
  "version": "0.0.0",
  "description": "Minimalistic boilerplate to quick-start Node.js development in TypeScript.",
  "type": "module",
  "engines": {
    "node": ">= 16.13 <17"
  },
  "devDependencies": {
    "@types/jest": "^28.1.4",
    "@types/node": "~16",
    "@typescript-eslint/eslint-plugin": "~5.26",
    "@typescript-eslint/parser": "~5.26",
    "eslint": "~8.16",
    "eslint-config-prettier": "~8.5",
    "eslint-plugin-jest": "~26.2",
    "jest": "^28.1.1",
    "prettier": "~2.6",
    "rimraf": "~3.0",
    "source-map-support": "^0.5.21",
    "ts-jest": "^28.0.5",
    "tsutils": "~3.21",
    "typescript": "~4.7"
  },
  "scripts": {
    "start": "node build/src/main.js",
    "gen": "node build/src/gen.js",
    "clean": "rimraf coverage build tmp",
    "prebuild": "npm run lint",
    "build": "tsc -p tsconfig.json",
    "build:watch": "tsc -w -p tsconfig.json",
    "build:release": "npm run clean && tsc -p tsconfig.release.json",
    "lint": "eslint . --ext .ts --ext .mts",
    "test": "jest --coverage",
    "prettier": "prettier --config .prettierrc --write .",
    "test:watch": "jest --watch"
  },
  "author": "Jakub Synowiec <[email protected]>",
  "license": "Apache-2.0",
  "dependencies": {
    "@types/clone-deep": "^4.0.1",
    "@types/underscore": "^1.11.4",
    "clone-deep": "^4.0.1",
    "time-span": "^5.1.0",
    "tslib": "~2.4",
    "underscore": "^1.13.4"
  },
  "volta": {
    "node": "16.13.0"
  }
}
//tsconfig.json
{
  "compilerOptions": {
    "target": "ES2022",
    "module": "node16",
    "lib": [
      "ES2022"
    ],
    "moduleResolution": "Node16",
    "rootDir": ".",
    "outDir": "build",
    "allowSyntheticDefaultImports": true,
    "importHelpers": true,
    "alwaysStrict": true,
    "sourceMap": true,
    "forceConsistentCasingInFileNames": true,
    "noFallthroughCasesInSwitch": true,
    "noImplicitReturns": true,
    "noUnusedLocals": false,
    "noUnusedParameters": true,
    "noImplicitAny": false,
    "noImplicitThis": false,
    "strictNullChecks": false,
    "allowJs": true
  },
  "include": [
    "src/**/*",
    "__tests__/**/*"
  ]
}

这是

//node_modules/covert-hrtime/index.d.ts
export interface HighResolutionTime {
    seconds: number;
    milliseconds: number;
    nanoseconds: bigint;
}
export default function convertHrtime(hrtime: bigint): HighResolutionTime;

我尝试过很多事情的有问题的文件,包括使用transformignorepatternstransform以匹配jest.config.js中的有问题的软件包。

有人可以发现问题吗?

谢谢

In my project I get the following from yarn test

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.

    Out of the box Jest supports Babel, which will be used to transform your files into valid JS based on your Babel configuration.

    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:

    /Users/danwt/Documents/work/interchain-security/diff-model-ts/node_modules/time-span/index.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){import convertHrtime from 'convert-hrtime';
                                                                                      ^^^^^^

    SyntaxError: Cannot use import statement outside a module

      1 | import * as fs from 'fs';
    > 2 | import timeSpan from 'time-span';
...

I have viewed many pages to try to find solutions and fix this without success.

My project is nodejs using typescript and jest.

//jest.config.js
export default {
  testEnvironment: 'node',
  preset: 'ts-jest/presets/default-esm',
  globals: {
    'ts-jest': {
      useESM: true,
    },
  },
  moduleNameMapper: {
    '^(\\.{1,2}/.*)\\.(m)?js
//package.json
{
  "name": "node-typescript-boilerplate",
  "version": "0.0.0",
  "description": "Minimalistic boilerplate to quick-start Node.js development in TypeScript.",
  "type": "module",
  "engines": {
    "node": ">= 16.13 <17"
  },
  "devDependencies": {
    "@types/jest": "^28.1.4",
    "@types/node": "~16",
    "@typescript-eslint/eslint-plugin": "~5.26",
    "@typescript-eslint/parser": "~5.26",
    "eslint": "~8.16",
    "eslint-config-prettier": "~8.5",
    "eslint-plugin-jest": "~26.2",
    "jest": "^28.1.1",
    "prettier": "~2.6",
    "rimraf": "~3.0",
    "source-map-support": "^0.5.21",
    "ts-jest": "^28.0.5",
    "tsutils": "~3.21",
    "typescript": "~4.7"
  },
  "scripts": {
    "start": "node build/src/main.js",
    "gen": "node build/src/gen.js",
    "clean": "rimraf coverage build tmp",
    "prebuild": "npm run lint",
    "build": "tsc -p tsconfig.json",
    "build:watch": "tsc -w -p tsconfig.json",
    "build:release": "npm run clean && tsc -p tsconfig.release.json",
    "lint": "eslint . --ext .ts --ext .mts",
    "test": "jest --coverage",
    "prettier": "prettier --config .prettierrc --write .",
    "test:watch": "jest --watch"
  },
  "author": "Jakub Synowiec <[email protected]>",
  "license": "Apache-2.0",
  "dependencies": {
    "@types/clone-deep": "^4.0.1",
    "@types/underscore": "^1.11.4",
    "clone-deep": "^4.0.1",
    "time-span": "^5.1.0",
    "tslib": "~2.4",
    "underscore": "^1.13.4"
  },
  "volta": {
    "node": "16.13.0"
  }
}
//tsconfig.json
{
  "compilerOptions": {
    "target": "ES2022",
    "module": "node16",
    "lib": [
      "ES2022"
    ],
    "moduleResolution": "Node16",
    "rootDir": ".",
    "outDir": "build",
    "allowSyntheticDefaultImports": true,
    "importHelpers": true,
    "alwaysStrict": true,
    "sourceMap": true,
    "forceConsistentCasingInFileNames": true,
    "noFallthroughCasesInSwitch": true,
    "noImplicitReturns": true,
    "noUnusedLocals": false,
    "noUnusedParameters": true,
    "noImplicitAny": false,
    "noImplicitThis": false,
    "strictNullChecks": false,
    "allowJs": true
  },
  "include": [
    "src/**/*",
    "__tests__/**/*"
  ]
}

Here is the problematic file

//node_modules/covert-hrtime/index.d.ts
export interface HighResolutionTime {
    seconds: number;
    milliseconds: number;
    nanoseconds: bigint;
}
export default function convertHrtime(hrtime: bigint): HighResolutionTime;

I have tried a lot of things including using transformIgnorePatterns and transform to match the problematic package in jest.config.js.

Can anybody spot the problem?

Thank you

: '$1', }, testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.(m)?ts


Here is the problematic file


I have tried a lot of things including using transformIgnorePatterns and transform to match the problematic package in jest.config.js.

Can anybody spot the problem?

Thank you

, coverageDirectory: 'coverage', collectCoverageFrom: [ 'src/**/*.ts', 'src/**/*.mts', '!src/**/*.d.ts', '!src/**/*.d.mts', ], };

Here is the problematic file

I have tried a lot of things including using transformIgnorePatterns and transform to match the problematic package in jest.config.js.

Can anybody spot the problem?

Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文