【问题标题】:Jest Type definitions are not working on a Typescript projectJest 类型定义不适用于 Typescript 项目
【发布时间】:2021-01-19 22:05:19
【问题描述】:

我已经用GTS 开始了一个新的 Typescript 项目。常规 *.ts 文件的分型工作正常,但我没有尝试让 *.spec.ts 文件正常工作。

问题

每个 jest 函数在 VS 代码中都被 ESLint 标记为红色。消息将显示“(describe | it | expect) 未定义”。我尝试过包含和排除文件、添加类型以及从其他项目中复制。

这是我当前的 tsconfig:

{
  "compileOnSave": false,
  "extends": "./node_modules/gts/tsconfig-google.json",
  "compilerOptions": {
    "rootDir": "./src",
    "outDir": "./dist",
    "sourceMap": true,
    "declaration": true,
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "importHelpers": true,
    "target": "es2015",
    "module": "esnext",
    "typeRoots": ["node_modules/@types"],
    "lib": ["es2017", "dom"],
    "skipLibCheck": true,
    "skipDefaultLibCheck": true,
    "baseUrl": ".",
    "types": ["node", "jest"]
  },
  "exclude": ["**/*.spec.ts"],
  "include": ["src/**/*.ts", "tests/**/*.ts"]
}

根目录很简单

  • src(ts 文件)
  • 测试(所有规范文件)
  • tsconfig.json

相关包:

"@types/jest": "^26.0.14",
"@types/node": "^13.11.1",
"gts": "^2.0.2",
"jest": "^26.4.2",
"tslint": "^6.1.3",
"typescript": "^4.0.3"

我的配置中有什么明显的错误吗?我应该有 2 个配置,一个用于 ts,另一个用于规范?

【问题讨论】:

    标签: typescript jestjs tslint gts


    【解决方案1】:

    问题在于ESLint 配置。我将此添加到.eslintrc.json 文件中,错误消失了:

    "env": {
        "browser": true,
        "es6": true,
        "jest": true,
        "node": true
      },
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-18
      • 2021-03-26
      • 2020-08-09
      • 2023-02-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-07
      相关资源
      最近更新 更多