0

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.

undsoft
  • 789
  • 2
  • 12
  • 21
  • I just ran into the same problem with Jest after upgrading Angular from 12 to 13. None of my tests will run bc of import error `Runtime.createScriptFromCode`. Any luck figuring it out? – RcoderNY Jan 01 '22 at 01:41

2 Answers2

0

Does adding

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

work for you?

  • No, unfortunately I get the same error. – undsoft Jan 09 '22 at 13:34
  • See the accepted answer of https://stackoverflow.com/questions/70532386/jest-tests-fail-when-trying-to-import-date-fns-in-angular-project that worked for me – Eydrian Jan 27 '22 at 16:46
0

Got fixed in date-fns-tz v1.3.0

undsoft
  • 789
  • 2
  • 12
  • 21