简体   繁体   English

TS-Jest 不解析 tsconfig 路径

[英]TS-Jest not resolving tsconfig paths

I have already added these paths to tsconfig.json :我已经将这些路径添加到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/**/*"
    ]
}

And in the jest.config.js I added this: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),
// ...

But I still have the same behaviour: the intellisense doesn't even load the possible custom paths like those listed above as you can see in the image below:但我仍然有相同的行为:intellisense 甚至不加载可能的自定义路径,如下图所示: 测试文件

That happens with this folder structure:这种情况发生在这个文件夹结构中:

文件夹结构

You can create a new tsconfig file just for building.您可以为构建创建一个新的 tsconfig 文件。 ex: copy your current tsconfig.json to tsconfig.build.json例如:将您当前的tsconfig.json复制到tsconfig.build.json

Remember to update esbuild options in serverless.ts记得更新 serverless.ts 中的serverless.ts选项

...
custom: {
    esbuild: {
        tsconfig: 'tsconfig.build.ts'
    }
}
...

then create a new tsconfig.json for IDE(vscode) and test:然后为 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/**/*"
  ]
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM