1

I upgraded my project to Angular 13 and jest needed some changes too.

Now any reference to 'src' can not be resolved.

For example:

Cannot find module 'src/app/app.component/app.component.test' from 'src/dashboard/reports/modules/tables/matrix-table/matrix-table.test.ts'

This is my jest.config.js

const { compilerOptions } = require('./tsconfig.test.json');
const { pathsToModuleNameMapper } = require('ts-jest/utils');

module.exports = {
  preset: "jest-preset-angular",
  testMatch: ["**/*.test.ts"],
  coverageReporters: ["json", "lcov", "text", "clover", "cobertura"],
  moduleNameMapper: pathsToModuleNameMapper(compilerOptions.paths, { prefix: '<rootDir>/' }),
  globals: {
    "ts-jest": {
      tsconfig: "tsconfig.test.json"
    }
  }
};

What should I change in my jest config in order for it to resolve the root to src?

misha130
  • 5,457
  • 2
  • 29
  • 51
  • There are several new config options missing. The [jest-preset-angular migration guide](https://thymikee.github.io/jest-preset-angular/docs/guides/angular-13+/) and [ts-jest ESM support doc](https://kulshekhar.github.io/ts-jest/docs/guides/esm-support/#use-esm-presets) were really helpful with this upgrade. – stealththeninja Jan 15 '22 at 06:34

2 Answers2

1

The error seems to hint at Typescript and Jest builder not transpiling the tests to ESM, a recent change with the Angular v13 upgrade. From the ts-jest ESM support doc:

// jest.config.js
module.exports = {
  // [...]
  extensionsToTreatAsEsm: ['.ts'],
  globals: {
    'ts-jest': {
      useESM: true,
    },
  },
  moduleNameMapper: {
    '^(\\.{1,2}/.*)\\.js$': '$1',
  },
}
stealththeninja
  • 3,576
  • 1
  • 26
  • 44
0

Have you tried adding

"moduleDirectories": [
    "node_modules",
    "src"
]

to your jest.config.js

This looks like a similar issue