我想使用tsconfig的paths功能。但是当与jest
一起使用时,我遇到了一些问题。这是我实现以下内容的指南:https://medium.com/@fmoessle/typescript-paths-with-ts-node-ts-node-dev-and-jest-671deacf6428
我的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"
]
}
我的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
当我运行测试时,它失败的原因如下:
● 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
}
文件夹结构是:
src/
├─ entities/tests/utils/
├─ testUtils/
tsconfig.json
jest.config.ts
发布于 2022-11-24 10:35:09
它在移除./
之前的tsconfig
路径后工作,如下所示:
而不是这样:
"paths" : {
"@testUtils": ["./entities/tests/utils", "./testUtils"],
"@testUtils/*": ["./entities/tests/utils/*", "./testUtils/*"]
},
这
"paths" : {
"@testUtils": ["entities/tests/utils", "testUtils"],
"@testUtils/*": ["entities/tests/utils/*", "testUtils/*"]
},
https://stackoverflow.com/questions/74551940
复制相似问题