当我使用 jest(ts-jest async/await test) 时,WebStorm 提示我 TS 错误 (TS2705)

WebStorm hints me the TS error (TS2705) when I use jest(ts-jest async/await test)

套餐: 笑话 笑话 @types/jest

IDE: 网络风暴

当我使用 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 错误提示?

问题已解决

但不明白其中的原理

我在tests目录下添加了一个tsconfg.json文件,解决了

如果在 首选项中启用了 TypeScript 服务 |语言与框架 | TypeScript,它使用最近的 tsconfig.json 当前文件所在的位置,扫描从文件目录到项目根目录的文件夹。如果没有找到包含当前文件的 tsconfig.*.json 个文件,则使用默认配置进行文件 linting。 请注意根 tsconfig.json:

中的“include”部分
"include":[
    "./app/**/*.ts",
    "./config/**/*.ts",
    "./app.ts",
    "./preload.ts"
  ],

"tests" 规范文件所在的目录未包含在您的 TypeScript 项目中。见 tsconfig.json documentation:

如果 "files" 和 "include" 都未指定,编译器默认包含所有 TypeScript(.ts、.d.ts 和 .tsx)文件在包含目录和子目录中,使用 "exclude" 属性 排除的除外。如果指定了 "files" 或 "include" 属性,编译器 将改为包含这两个属性包含的文件的并集。

因此,没有找到适合您的规范文件的 tsconfig.json 文件(您一定在 typeScript 控制台中看到了警告 'no root tsconfig.json found')。结果,使用了不包含 "es2015" 库的默认编译器首选项 - 因此出现了错误。 在根配置中包含测试文件或在 "tests" 目录中添加具有适当设置的单独 tsconfig.json 应该可以解决问题。