37

Attempting to convert this project over to jest using these instructions. I have everything working except for the files that use the paths configuration:

"paths": {
      "@fs/*": ["./src/*"], 
      "@test/*": ["./test/*"]
    }

It looks as if when the tests are run the import statements do not resolve and this is logged:

Cannot find module '@fs/container/validation/ValidationContext' from 'Core.spec.ts'

  1 | import { ValidationOptions } from "@fs/container/validation/ValidationOptions";
> 2 | import { ValidationContext } from "@fs/container/validation/ValidationContext";
    | ^
  3 | import { ValidationContainer } from "@fs/container/validation/ValidationContainer";
  4 | 
  5 | import { Core1 } from "@test/core/Core1";

  at Resolver.resolveModule (node_modules/jest-resolve/build/index.js:221:17)
  at Object.<anonymous> (test/core/Core.spec.ts:2:1)

Is there a way to get jest/ts-jest include the @paths while resolving imports?

skyboyer
  • 22,209
  • 7
  • 57
  • 64
Ole
  • 41,793
  • 59
  • 191
  • 359

4 Answers4

52

I wanted to resolve modules paths starting with ~/ to my <baseUrl>/<moduleName>.

Thank to OJ Kwon link I solved it with (give him point).

tsconfig.json

see module-resolution path-mapping doc

{
  "compilerOptions": {
    "baseUrl": "src",
    "paths": {
      "~/*": ["*"]
    }
  },
}

jest config

Then we need to tell jest to resolve the paths too. It's done with the following config:

"moduleNameMapper": {
  "~/(.*)": "<rootDir>/src/$1"
},
Édouard Lopez
  • 40,270
  • 28
  • 126
  • 178
  • 7
    It helped me a lot in my case my ts path was "@data/*": ["src/data/*"] then in my jest config i added: '@data/(.*)': '/src/data/$1', – Abner Feb 02 '22 at 00:53
25

Add jest.config.js on root project folder with the content below.

const { pathsToModuleNameMapper } = require('ts-jest')
const { compilerOptions } = require('./tsconfig')
module.exports = {
    preset: 'ts-jest',
    testEnvironment: 'node',
    moduleNameMapper: pathsToModuleNameMapper(compilerOptions.paths),
    modulePaths: [
        '<rootDir>'
    ],
}

Also make sure that your tsconfig.json file has a paths field, like this:

{
  "compilerOptions": {
    ....
    "paths": {
      "src/*": ["src/*"]
    }
  },
}
darcher
  • 3,052
  • 3
  • 24
  • 29
FranSanchis
  • 1,455
  • 14
  • 15
19

jest can't honor tsconfig's path mapping as it's ts compiler time, so jest configuration should have corresponding modulenamemapper to resolve aliased paths.

OJ Kwon
  • 4,385
  • 1
  • 20
  • 24
2

Answer By @FranSanchis did fix the issue for me, I have just done a lot bit of changes to meet with 2023 jest changes

in jest.config.js

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

module.exports = {
  
  preset: 'ts-jest',
.
.
.
moduleNameMapper: pathsToModuleNameMapper(compilerOptions.paths),
  modulePaths: ['<rootDir>'],
}

And in tsconfig

"compilerOptions": {
    "target": "es6",
    "lib": ["dom", "dom.iterable", "esnext"],
    "allowJs": true,
    "skipLibCheck": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "noEmit": true,
    "esModuleInterop": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "jsx": "preserve",
    "incremental": true,
    "baseUrl": ".",
    "paths": {
      "@/*": ["src/*"]
    }
Aram Zuhairi
  • 175
  • 2
  • 5