簡體   English   中英

當我使用jest(ts-jest異步/等待測試)時,WebStorm提示我TS錯誤(TS2705)

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

軟件包:jest ts-jest @ types / jest

IDE:WebStorm

當我使用開玩笑的異步測試時,出現無法解決的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當前文件,從文件dir到項目根目錄掃描文件夾。 如果未找到包含當前文件的tsconfig.*.json文件, tsconfig.*.json默認配置用於文件插入。 請注意您的根tsconfig.json的“ include ”部分:

"include":[
    "./app/**/*.ts",
    "./config/**/*.ts",
    "./app.ts",
    "./preload.ts"
  ],

您的規范文件所在的“測試”目錄未包含在TypeScript項目中。 請參閱tsconfig.json文檔

如果未指定“文件”和“包含”,則編譯器默認將所有TypeScript(.ts,.d.ts和.tsx)文件包括在包含目錄和子目錄中,但使用“ exclude”屬性排除的文件除外。 如果指定了“文件”或“包含”屬性,則編譯器將改為包含這兩個屬性所包含的文件的並集。

因此,找不到適合您的spec文件的tsconfig.json文件(您必須在typeScript控制台中看到警告“未找到根tsconfig.json”)。 結果,使用了不包含"es2015"庫的默認編譯器首選項-因此出錯。 在根配置中包含測試文件,或在“ tests”目錄中添加具有適當設置的單獨tsconfig.json此問題。

問題已經解決。

但是不了解原理。

在此處輸入圖片說明

我在tests目錄中添加了tsconfg.json文件並解決了該問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM