I would like to use the paths functionality of the tsconfig. But when using it with jest
I am getting problems. This is the guide I followed to implement this: https://medium.com/@fmoessle/typescript-paths-with-ts-node-ts-node-dev-and-jest-671deacf6428
My tsconfig.json
is
{
"compilerOptions": {
"target": "es2017",
"module": "commonjs",
"lib": [
"dom",
"es6",
"es2017",
"esnext.asynciterable"
],
"skipLibCheck": true,
"sourceMap": true,
"outDir": "./dist",
"moduleResolution": "node",
"removeComments": true,
"noImplicitAny": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"noImplicitThis": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"allowSyntheticDefaultImports": true,
"esModuleInterop": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"resolveJsonModule": true,
"baseUrl": "./src",
"paths" : {
"@testUtils": ["./entities/tests/utils", "./testUtils"],
"@testUtils/*": ["./entities/tests/utils/*", "./testUtils/*"]
},
"typeRoots": [
"./src/custom_typings",
"./node_modules/@types"
]
},
"exclude": [
"node_modules"
],
"include": [
"src/**/*.ts"
]
}
My jest.config.ts
is
/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */
import { JestConfigWithTsJest, pathsToModuleNameMapper } from "ts-jest";
import { compilerOptions } from "./tsconfig.json";
const jestConfig: JestConfigWithTsJest = {
preset: "ts-jest",
testEnvironment: "node",
testPathIgnorePatterns: ["dist/", "node_modules/"],
// this enables us to use tsconfig-paths with jest
modulePaths: [compilerOptions.baseUrl],
moduleNameMapper: pathsToModuleNameMapper(compilerOptions.paths),
};
export default jestConfig
When I run the tests, it fails with:
● Test suite failed to run
Configuration error:
Could not locate module @testUtils mapped as:
[
"./entities/tests/utils",
"./testUtils"
].
Please check your configuration for these entries:
{
"moduleNameMapper": {
"/^@testUtils$/": "[
"./entities/tests/utils",
"./testUtils"
]"
},
"resolver": undefined
}
The folder structure is:
src/
├─ entities/tests/utils/
├─ testUtils/
tsconfig.json
jest.config.ts