sma*_*ite 1 javascript webstorm typescript jestjs
包: jest ts-jest @types/jest
IDE:WebStorm
当我使用jest异步测试时,有一个我无法解决的 TS 错误 (TS2705)。但是这个提示不影响jest命令。

当我使用最小的测试时也有同样的错误:
import {} from 'jest';
test('async test',async ()=>{
});
Run Code Online (Sandbox Code Playgroud)
开玩笑的配置文件
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"
}
}
};
Run Code Online (Sandbox Code Playgroud)
配置文件
{
"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/**/*.*"
]
}
Run Code Online (Sandbox Code Playgroud)
如何解决此 IDE 错误提示?
如果在首选项中启用了 TypeScript 服务| 语言和框架 | TypeScript,它使用最近tsconfig.json包含的当前文件,扫描从文件目录到项目根目录的文件夹。如果没有tsconfig.*.json找到包含当前文件的文件,则使用默认配置进行文件 linting。请注意根目录中的“包含”部分tsconfig.json:
"include":[
"./app/**/*.ts",
"./config/**/*.ts",
"./app.ts",
"./preload.ts"
],
Run Code Online (Sandbox Code Playgroud)
您的规范文件所在的“tests”目录不包含在您的 TypeScript 项目中。请参阅tsconfig.json 文档:
如果“文件”和“包含”都未指定,编译器默认将所有 TypeScript(.ts、.d.ts 和 .tsx)文件包含在包含目录和子目录中,但使用“排除”属性排除的文件除外。如果指定了“files”或“include”属性,编译器将改为包含这两个属性包含的文件的联合。
因此,找不到适合tsconfig.json您的规范文件的文件(您一定在 typeScript 控制台中看到了警告“找不到根 tsconfig.json”)。因此,使用了不包含"es2015"库的默认编译器首选项- 因此出现错误。在根配置中包含测试文件或tsconfig.json在“tests”目录中添加具有适当设置的单独文件应该可以解决问题。