更新到 Angular 13 后,无法在 Jest 中使用具有 date-fns 的模块外部的 import 语句

Cannot use import statement outside a module with date-fns in Jest after updating to Angular 13

更新 Angular 12 到 13 后,我的一些 Jest 测试开始失败。总是一样的错误:

/Users/undsoft/projects/work/webui/node_modules/date-fns/esm/format/index.js:1. ({"Object.":function(module,exports,require,__dirname,__filename,jest){import isValid from "../isValid/index.js";
SyntaxError: Cannot use import statement outside a module

at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1728:14)
at Object. (node_modules/date-fns-tz/format/index.js:8:38)

我的版本是:

“日期-fns”:“~2.28.0”,
“日期-fns-tz”:“~1.2.2”,
“开玩笑”:“~27.4.5”,
"jest-preset-angular": "~11.0.1",

Jest 配置为:

require('jest-preset-angular/ngcc-jest-processor');

module.exports = {
  preset: 'jest-preset-angular',
  setupFilesAfterEnv: ['<rootDir>/src/setup-jest.ts'],
  collectCoverage: true,
  coverageReporters: ['html'],
  coverageDirectory: 'coverage/my-app',
  moduleDirectories: ['node_modules', 'src'],
  moduleNameMapper: pathsToModuleNameMapper(compilerOptions.paths || {}),
};

我试过添加

testPathIgnorePatterns: [
  "node_modules/(?!(date-fns-tz|date-fns))",
],

和date-fns-tz/date-fns分开。

是否添加

transformIgnorePatterns: ['<rootDir>/node_modules/(?!.*\.mjs$)'],

为你工作?

已在 date-fns-tz v1.3.0

中修复