代码之家  ›  专栏  ›  技术社区  ›  small white

当我使用jest(ts jest async/await test)时,webtorm提示我ts错误(ts2705)

  •  1
  • small white  · 技术社区  · 8 年前

    包装: 开玩笑 TS笑话 @类型/笑话

    石斑鱼类: 网络风暴

    当我使用 jest 异步测试有一个TS错误(TS2705),我无法解决。但这个提示并不影响 jest 命令。

    image

    当我使用最小的测试时,也有相同的错误:

    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错误提示?

    2 回复  |  直到 8 年前
        1
  •  4
  •   lena    8 年前

    如果在中启用了typescript服务 首选项语言和框架类型脚本 ,它使用最近的 tsconfig.json 当前文件包含在中,正在扫描从文件目录到项目根目录的文件夹。如果没有 tsconfig.*.json 找到包含当前文件的文件,默认配置用于文件linting。 请注意“ 包括 “根中的节 tsconfig.json配置 :

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

    规范文件所在的“测试”目录不包含在typescript项目中。见 tsconfig.json documentation :

    如果“files”和“include”都未指定,编译器默认在包含目录和子目录中包括所有typescript(.ts、.d.ts和.tsx)文件,但使用“exclude”属性排除的文件除外。如果指定了“files”或“include”属性,则编译器 而是包含这两个属性包含的文件的并集。

    所以,不合适 tsconfig.json配置 找到了规范文件的文件(必须在typescript控制台中看到警告“未找到根tsconfig.json”)。因此,默认的编译器首选项不包括 "es2015" 库,已被使用-因此出错。 在根配置中包含测试文件或添加单独的 tsconfig.json配置 使用“tests”目录中的适当设置应该可以解决此问题。

        2
  •  0
  •   small white    8 年前

    问题已经解决了。

    但不明白原理。

    enter image description here

    我在 tests 目录并解决了它。