简体   繁体   中英

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. It's always the same error:

/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)

My versions are:

"date-fns": "~2.28.0",
"date-fns-tz": "~1.2.2",
"jest": "~27.4.5",
"jest-preset-angular": "~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.

Does adding

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

work for you?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM