angularjestjsts-jestjest-preset-angular

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.


Solution

  • Got fixed in date-fns-tz v1.3.0