简体   繁体   English

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

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

After updating Angular 12 to 13 some of my Jest tests started failing.将 Angular 12 更新到 13 后,我的一些 Jest 测试开始失败。 It's always the same error:它总是同样的错误:

/Users/undsoft/projects/work/webui/node_modules/date-fns/esm/format/index.js:1. /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"; ({"Object.":function(module,exports,require,__dirname,__filename,jest){import isValid from "../isValid/index.js";
SyntaxError: Cannot use import statement outside a module SyntaxError: 不能在模块外使用 import 语句

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

My versions are:我的版本是:

"date-fns": "~2.28.0", “日期-fns”:“~2.28.0”,
"date-fns-tz": "~1.2.2", “日期-fns-tz”:“~1.2.2”,
"jest": "~27.4.5", “开玩笑”:“~27.4.5”,
"jest-preset-angular": "~11.0.1", “开玩笑预设角度”:“〜11.0.1”,

Jest config is:开玩笑的配置是:

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 || {}),
};

I've tried adding我试过添加

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

and date-fns-tz/date-fns separately.和 date-fns-tz/date-fns 分开。

Does adding是否添加

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

work for you?为你工作?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Angular v13 带 nx 测试的玩笑 - 语法错误:无法在 Runtime.createScriptFromCode 的模块外使用导入语句 - Angular v13 Jest with nx test - SyntaxError: Cannot use import statement outside a module at Runtime.createScriptFromCode 找不到模块“date-fns” - Cannot find module 'date-fns' 尝试在 Angular 项目中导入 date-fns 时,Jest 测试失败 - Jest tests fail when trying to import date-fns in Angular project SyntaxError:无法在 Angular/Nx/jest 应用程序中使用 @ionic-native/health 在模块外使用 import 语句 - SyntaxError: Cannot use import statement outside a module using @ionic-native/health in Angular/Nx/jest app 未捕获的语法错误:无法在 Angular 和 Firebase 中的模块外部使用导入语句 - Uncaught SyntaxError: Cannot use import statement outside a module in Angular & Firebase Angular Datatables Cannot use import statement outside a module 错误 - Angular Datatables Cannot use import statement outside a module Error WebdriverIO 和 Angular,在导入类的打字稿中编写 e2e 测试(不能在模块外使用 import 语句) - WebdriverIO and Angular, writing e2e tests in typescript that import classes (Cannot use import statement outside a module) angular-calender和date-fns无法在Angular中编译 - angular-calender and date-fns failed to compile in Angular 未捕获的语法错误:无法在 Angular 12 项目中的模块外部使用导入语句 - Uncaught SyntaxError: Cannot use import statement outside a module in Angular 12 project Angular.ts 和 Electron:SyntaxError:不能在模块外使用导入语句 - Angular.ts and Electron: SyntaxError: Cannot use import statement outside a module
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM