[英]tsconfig paths with jest.config
I would like to use the paths functionality of the tsconfig.我想使用 tsconfig 的路径功能。 But when using it with
jest
I am getting problems.但是当
jest
地使用它时,我遇到了问题。 This is the guide I followed to implement this: https://medium.com/@fmoessle/typescript-paths-with-ts-node-ts-node-dev-and-jest-671deacf6428这是我遵循的指南来实现这个: https://medium.com/@fmoessle/typescript-paths-with-ts-node-ts-node-dev-and-jest-671deacf6428
My tsconfig.json
is我的
tsconfig.json
是
{
"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我的
jest.config.ts
是
/** @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
It works after removing the ./
in front of the paths in tsconfig
so:它在
tsconfig
中删除路径前面的./
后工作,因此:
Instead of this:而不是这个:
"paths" : {
"@testUtils": ["./entities/tests/utils", "./testUtils"],
"@testUtils/*": ["./entities/tests/utils/*", "./testUtils/*"]
},
This这个
"paths" : {
"@testUtils": ["entities/tests/utils", "testUtils"],
"@testUtils/*": ["entities/tests/utils/*", "testUtils/*"]
},
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.