TS-Jest 不解析 tsconfig 路径
TS-Jest not resolving tsconfig paths
我已经将这些路径添加到 tsconfig.json
:
{
"compilerOptions": {
"lib": ["ESNext"],
"moduleResolution": "node",
"noUnusedLocals": true,
"noUnusedParameters": true,
"removeComments": true,
"sourceMap": true,
"target": "ES2020",
"outDir": "lib",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"baseUrl": ".",
"paths": {
"@lambdas/*": ["src/lambdas/*"],
"@services/*": ["src/services/*"],
"@types/*": ["src/@types/*"],
"@configs/*": ["src/configs/*"],
"@database/*": ["src/database/*"],
"@entities/*": ["src/entities/*"],
"@validations/*": ["src/validations/*"],
"#serverless/*": ["serverless/*"]
}
},
"include": ["src/**/*.ts", "serverless.ts"],
"exclude": [
"node_modules/**/*",
".serverless/**/*",
".webpack/**/*",
"_warmup/**/*",
".vscode/**/*"
]
}
并且在 jest.config.js
我添加了这个:
const { pathsToModuleNameMapper } = require('ts-jest/utils');
// eslint-disable-next-line import/extensions
const { compilerOptions } = require('./tsconfig.json');
// module.exports = { ...
moduleNameMapper: pathsToModuleNameMapper(compilerOptions.paths),
// ...
但我仍然有相同的行为:intellisense 甚至不加载上面列出的可能的自定义路径,如下图所示:
这种文件夹结构会发生这种情况:
您可以创建一个新的 tsconfig 文件来构建。例如:将您当前的 tsconfig.json
复制到 tsconfig.build.json
记得更新 serverless.ts
中的 esbuild 选项
...
custom: {
esbuild: {
tsconfig: 'tsconfig.build.ts'
}
}
...
然后为 IDE(vscode) 创建一个新的 tsconfig.json
并测试:
{
"extends": "./tsconfig.paths.json", // extend setting from build config
"include": [
"src/**/*.ts",
"tests/**/*.ts" // include tests directory
],
"exclude": [
"node_modules/**/*",
".serverless/**/*",
".webpack/**/*",
"_warmup/**/*",
".vscode/**/*"
]
}
我已经将这些路径添加到 tsconfig.json
:
{
"compilerOptions": {
"lib": ["ESNext"],
"moduleResolution": "node",
"noUnusedLocals": true,
"noUnusedParameters": true,
"removeComments": true,
"sourceMap": true,
"target": "ES2020",
"outDir": "lib",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"baseUrl": ".",
"paths": {
"@lambdas/*": ["src/lambdas/*"],
"@services/*": ["src/services/*"],
"@types/*": ["src/@types/*"],
"@configs/*": ["src/configs/*"],
"@database/*": ["src/database/*"],
"@entities/*": ["src/entities/*"],
"@validations/*": ["src/validations/*"],
"#serverless/*": ["serverless/*"]
}
},
"include": ["src/**/*.ts", "serverless.ts"],
"exclude": [
"node_modules/**/*",
".serverless/**/*",
".webpack/**/*",
"_warmup/**/*",
".vscode/**/*"
]
}
并且在 jest.config.js
我添加了这个:
const { pathsToModuleNameMapper } = require('ts-jest/utils');
// eslint-disable-next-line import/extensions
const { compilerOptions } = require('./tsconfig.json');
// module.exports = { ...
moduleNameMapper: pathsToModuleNameMapper(compilerOptions.paths),
// ...
但我仍然有相同的行为:intellisense 甚至不加载上面列出的可能的自定义路径,如下图所示:
这种文件夹结构会发生这种情况:
您可以创建一个新的 tsconfig 文件来构建。例如:将您当前的 tsconfig.json
复制到 tsconfig.build.json
记得更新 serverless.ts
...
custom: {
esbuild: {
tsconfig: 'tsconfig.build.ts'
}
}
...
然后为 IDE(vscode) 创建一个新的 tsconfig.json
并测试:
{
"extends": "./tsconfig.paths.json", // extend setting from build config
"include": [
"src/**/*.ts",
"tests/**/*.ts" // include tests directory
],
"exclude": [
"node_modules/**/*",
".serverless/**/*",
".webpack/**/*",
"_warmup/**/*",
".vscode/**/*"
]
}