错误 [ERR_REQUIRE_ESM]:不支持 ES 模块 node_modu les\@angular\core\fesm2015\testing.mjs 的 require()。Angular 13,开玩笑

Pet*_*nov 6 jestjs angular angular13

升级到 Angular 13 后,我的 Jest 单元测试失败,我不明白为什么以及如何修复它们。我\xc2\xb4已经搜索了一段时间,我找到了一些类似案例的信息。此类失败的原因似乎是 Angular 13 中的更改,这使得 Jest 有点不兼容,即使 I\xc2\xb4m 使用最新版本的 Jest (27.5)。

\n

这是我的 package.json:

\n
 {\n  "name": "app-angular",\n  "version": "6.67.1",\n  "scripts": {\n    "ng": "ng",\n    "start": "ng serve",\n    "build": "ng build",\n    "serve-high-memory": "node --max_old_space_size=8000 ./node_modules/@angular/cli/bin/ng serve",\n    "serve-open-high-memory": "node --max_old_space_size=8000 ./node_modules/@angular/cli/bin/ng serve --open",\n    "dev-high-memory": "node --max_old_space_size=8000 ./node_modules/@angular/cli/bin/ng build -c dev",\n    "dev-test-high-memory": "node --max_old_space_size=8000 ./node_modules/@angular/cli/bin/ng build -c dev-test",\n    "staging-high-memory": "node --max_old_space_size=8000 ./node_modules/@angular/cli/bin/ng build -c staging",\n    "release-high-memory": "node --max_old_space_size=8000 ./node_modules/@angular/cli/bin/ng build -c release",\n    "prod-high-memory": "node --max_old_space_size=8000 ./node_modules/@angular/cli/bin/ng build -c production",\n    "test-high-memory": "node --max_old_space_size=8000 ./node_modules/@angular/cli/bin/ng test App-Angular",\n    "test": "jest --config ./jest.config.ts",\n    "test:coverage": "yarn jest --coverage",\n    "lint": "ng lint App-Angular",\n    "e2e": "ng e2e",\n    "release": "standard-version",\n    "commit": "git-cz"\n  },\n  "husky": {\n    "hooks": {\n      "commit-msg": "commitlint -E HUSKY_GIT_PARAMS"\n    }\n  },\n  "private": true,\n  "dependencies": {\n    "@angular-builders/jest": "^13.0.3",\n    "@angular/animations": "^13.2.3",\n    "@angular/cdk": "^13.2.4",\n    "@angular/common": "^13.2.3",\n    "@angular/compiler": "^13.2.3",\n    "@angular/core": "^13.2.3",\n    "@angular/forms": "^13.2.3",\n    "@angular/localize": "^13.2.3",\n    "@angular/material": "^13.2.4",\n    "@angular/material-moment-adapter": "^13.2.4",\n    "@angular/platform-browser": "^13.2.3",\n    "@angular/platform-browser-dynamic": "^13.2.3",\n    "@angular/router": "^13.2.3",\n    "@digitalascetic/ngx-pica": "^10.1.0",\n    "@ngrx/data": "^13.0.2",\n    "@ngrx/effects": "^13.0.2",\n    "@ngrx/entity": "^13.0.2",\n    "@ngrx/store": "^13.0.2",\n    "@ngrx/store-devtools": "^13.0.2",\n    "@ngx-progressbar/core": "5.3.2",\n    "@ngx-progressbar/http-client": "3.0.2",\n    "@ngx-translate/core": "^14.0.0",\n    "@ngx-translate/http-loader": "^7.0.0",\n    "@progress/kendo-angular-buttons": "^7.0.2",\n    "@progress/kendo-angular-common": "^2.0.2",\n    "@progress/kendo-angular-dateinputs": "^6.0.0",\n    "@progress/kendo-angular-dialog": "^6.0.1",\n    "@progress/kendo-angular-dropdowns": "^6.0.1",\n    "@progress/kendo-angular-excel-export": "^4.0.3",\n    "@progress/kendo-angular-grid": "^6.0.1",\n    "@progress/kendo-angular-inputs": "^8.0.3",\n    "@progress/kendo-angular-intl": "^3.1.2",\n    "@progress/kendo-angular-l10n": "^3.0.3",\n    "@progress/kendo-angular-label": "^3.1.2",\n    "@progress/kendo-angular-layout": "^6.4.2",\n    "@progress/kendo-angular-pdf-export": "^3.0.3",\n    "@progress/kendo-angular-popup": "^4.0.4",\n    "@progress/kendo-angular-progressbar": "^2.0.3",\n    "@progress/kendo-angular-scheduler": "^3.0.1",\n    "@progress/kendo-angular-scrollview": "^4.1.3",\n    "@progress/kendo-angular-treeview": "^6.0.0",\n    "@progress/kendo-data-query": "^1.5.5",\n    "@progress/kendo-drawing": "^1.16.2",\n    "@progress/kendo-licensing": "^1.2.2",\n    "@progress/kendo-recurrence": "^1.0.2",\n    "@progress/kendo-theme-default": "^5.1.1",\n    "@progress/kendo-theme-material": "^5.1.1",\n    "angular-fittext": "^2.1.1",\n    "b64-to-blob": "^1.2.19",\n    "bootstrap": "^5.1.3",\n    "changedpi": "^1.0.4",\n    "exifr": "^7.1.3",\n    "guid-typescript": "^1.0.9",\n    "jest-localstorage-mock": "^2.4.18",\n    "jest-mock": "^27.5.1",\n    "messageformat": "^2.3.0",\n    "moment": "^2.29.1",\n    "ngx-toastr": "^14.2.1",\n    "ngx-translate-messageformat-compiler": "^5.0.1",\n    "npm-check-updates": "^12.5.0",\n    "pica": "^9.0.1",\n    "rxjs": "^7.5.4",\n    "rxjs-compat": "^6.6.7",\n    "secure-ls": "^1.2.6",\n    "subsink": "^1.0.2",\n    "tslib": "^2.3.1",\n    "typescript-string-operations": "^1.4.1",\n    "zone.js": "~0.11.4"\n  },\n  "devDependencies": {\n    "@angular-devkit/build-angular": "^13.2.4",\n    "@angular/cli": "13.2.4",\n    "@angular/compiler-cli": "^13.2.3",\n    "@angular/language-service": "^13.2.3",\n    "@commitlint/cli": "^16.2.1",\n    "@commitlint/config-conventional": "^16.2.1",\n    "@types/jest": "^27.4.1",\n    "@types/jquery": "^3.5.5",\n    "@types/node": "^17.0.21",\n    "codelyzer": "^6.0.0",\n    "cz-conventional-changelog": "^3.2.0",\n    "husky": "^4.3.6",\n    "jest": "^27.5.1",\n    "jest-preset-angular": "^11.1.1",\n    "protractor": "~7.0.0",\n    "standard-version": "^9.3.2",\n    "ts-node": "~10.6.0",\n    "tslint": "~6.1.3",\n    "typescript": "~4.5.5"\n  },\n  "config": {\n    "commitizen": {\n      "path": "./node_modules/cz-conventional-changelog"\n    }\n  }\n}\n
Run Code Online (Sandbox Code Playgroud)\n

我的 jest.config.ts 文件:

\n
    import type { Config } from '@jest/types';\nimport 'jest-preset-angular/setup-jest';\n\nconst config: Config.InitialOptions = {\n  verbose: true,\n  preset: "jest-preset-angular",\n  testEnvironment: "jsdom",\n  testRunner: "jest-jasmine2",\n  setupFilesAfterEnv: [\n    "<rootDir>/setupJest.ts"\n  ],\n  testPathIgnorePatterns: [\n    "<rootDir>/node_modules/",\n    "<rootDir>/dist/",\n    "<rootDir>/src/test.ts"\n  ],\n  globals: {\n    "ts-jest": {\n      tsconfig: "<rootDir>/src/tsconfig.spec.json",\n      stringifyContentPathRegex: "\\\\.html$",\n      astTransformers: {\n        before: [\n          "jest-preset-angular/build/InlineFilesTransformer",\n          "jest-preset-angular/build/StripStylesTransformer"\n        ]\n      }\n    }\n  },\n  roots: [\n    "<rootDir>",\n    "./src",\n    "./projects"\n  ],\n  modulePaths: [\n    "<rootDir>",\n    "./src",\n    "./projects"\n  ],\n  moduleDirectories: [\n    "node_modules"\n  ]\n}\n\nexport default config;\n
Run Code Online (Sandbox Code Playgroud)\n

当我运行纱线测试时收到的错误是:

\n
$ yarn test\nyarn run v1.22.5\n$ jest --config ./jest.config.ts\nError: Jest: Failed to parse the TypeScript config file C:\\Projects\\appAngularfrontend\\kendoUpdate\\jes\nt.config.ts\n  Error [ERR_REQUIRE_ESM]: require() of ES Module C:\\Projects\\appAngularfrontend\\kendoUpdate\\node_modu\nles\\@angular\\core\\fesm2015\\testing.mjs not supported.\nInstead change the require of C:\\Projects\\appAngularfrontend\\kendoUpdate\\node_modules\\@angular\\core\\fe\nsm2015\\testing.mjs to a dynamic import() which is available in all CommonJS modules.\n    at readConfigFileAndSetRootDir (C:\\Projects\\appAngularfrontend\\kendoUpdate\\node_modules\\jest-confi\ng\\build\\readConfigFileAndSetRootDir.js:136:13)\n    at async readConfig (C:\\Projects\\appAngularfrontend\\kendoUpdate\\node_modules\\jest-config\\build\\ind\nex.js:225:18)\n    at async readConfigs (C:\\Projects\\appAngularfrontend\\kendoUpdate\\node_modules\\jest-config\\build\\in\ndex.js:420:26)\n    at async runCLI (C:\\Projects\\appAngularfrontend\\kendoUpdate\\node_modules\\@jest\\core\\build\\cli\\inde\nx.js:132:59)\n    at async Object.run (C:\\Projects\\appAngularfrontend\\kendoUpdate\\node_modules\\jest-cli\\build\\cli\\in\ndex.js:155:37)\nerror Command failed with exit code 1.\ninfo Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.\n
Run Code Online (Sandbox Code Playgroud)\n

有人可以帮忙吗?

\n