套餐: Jest Jest @types/Jest
开发环境: 网络 Storm
当我使用 jest异步测试有一个我无法解决的 TS 错误 (TS2705)。但是这个提示不会影响 jest
命令。
我用最小测试的时候出现同样的错误:
import {} from 'jest';
test('async test',async ()=>{
});
jest.config.js
module.exports = {
"transform": {
"^.+\\.tsx?$": "ts-jest"
},
"testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$",
"moduleFileExtensions": [
"ts",
"tsx",
"js",
"jsx",
"json",
"node"
],
globals:{
'ts-jest':{
// I have the same behavior when I delete this line
"tsConfigFile": "./tsconfig.json"
}
}
};
tsconfig.json
{
"compilerOptions": {
"types": [
"node"
],
"module": "commonjs",
"target": "es2017",
"lib": [
"es2015",
"es2016",
"esnext.asynciterable"
],
"noImplicitAny": false,
"noImplicitThis": false,
"inlineSourceMap": true,
"rootDirs": ["."],
"outDir":"./dist",
"experimentalDecorators":true,
"emitDecoratorMetadata": true,
"moduleResolution": "node",
"watch":false
},
"include":[
"./app/**/*.ts",
"./config/**/*.ts",
"./app.ts",
"./preload.ts"
],
"exclude": [
"./dist/**/*.*"
]
}
如何解决此 IDE 错误提示?
最佳答案
如果在首选项中启用 TypeScript 服务 |语言与框架 | TypeScript,它使用最近的 tsconfig.json
包含当前文件,扫描从文件目录到项目根目录的文件夹。如果未找到包含当前文件的 tsconfig.*.json
文件,则使用默认配置进行文件 linting。
请注意根 tsconfig.json
中的“include”部分:
"include":[
"./app/**/*.ts",
"./config/**/*.ts",
"./app.ts",
"./preload.ts"
],
您的规范文件所在的“测试”目录不包含在您的 TypeScript 项目中。参见 tsconfig.json documentation :
如果“files”和“include”均未指定,则编译器默认将所有 TypeScript(.ts、.d.ts 和 .tsx)文件包含在包含目录和子目录中,使用排除的除外“排除”属性。如果指定了“files”或“include”属性,编译器 将改为包含这两个属性包含的文件的并集。
因此,没有找到适合您的规范文件的 tsconfig.json
文件(您一定已经在 typeScript 控制台中看到警告“找不到根 tsconfig.json”)。结果,使用了不包含 "es2015"
库的默认编译器首选项 - 因此出现了错误。
在根配置中包含测试文件或在“测试”目录中添加具有适当设置的单独 tsconfig.json
应该可以解决问题。
关于javascript - 当我使用 jest(ts-jest async/await test) 时,WebStorm 提示我 TS 错误 (TS2705),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50732633/