typescript - 找不到名称 'describe'。您需要为测试运行器安装类型定义吗?

标签 typescript jestjs

将 TypeScript 与 Jest 结合使用时,我的规范会失败并显示如下错误消息:

test/unit/some.spec.ts:1:1 - error TS2582: Cannot find name 'describe'. Do you need to install type definitions for a test runner? Try `npm i @types/jest` or `npm i @types/mocha`.
test/unit/some.spec.ts:2:3 - error TS2582: Cannot find name 'it'. Do you need to install type definitions for a test runner? Try `npm i @types/jest` or `npm i @types/mocha`.
test/unit/some.spec.ts:3:7 - error TS2304: Cannot find name 'expect'.
test/unit/some.spec.ts:7:1 - error TS2582: Cannot find name 'test'. Do you need to install type definitions for a test runner? Try `npm i @types/jest` or `npm i @types/mocha`.

类型已经安装。

我使用:

    "@types/jest": "^23.3.12",
    "jest": "^23.6.0",
    "ts-jest": "^23.10.5",
    "typescript": "^3.1.6"

我使用 jest --forceExit --coverage --verbose 运行测试

最佳答案

我在我的 Angular 项目和我的 IDE 中使用 Visual Studio Code,我不得不注释掉/删除文件 tsconfig.json 中的类型并在 中的类型中添加“jest” em>tsconfig.spec.json.

文件 tsconfig.json

{
  "compilerOptions": {
    // "types": []
  }
}

文件 tsconfig.spec.json

{
  "compilerOptions": {
    "types": ["jest", "node"]
  }
}

关于typescript - 找不到名称 'describe'。您需要为测试运行器安装类型定义吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54139158/

相关文章:

html - 在异步管道中设置 img src 对象 url 时防止图像闪烁

javascript - 为什么一个文件发送到服务器

reactjs - Jest/Enzyme 中的异常处理

javascript - Enzyme/Jest Prop 测试失败

javascript - JEST Received 函数没有抛出,但是抛出了 HTTPError

javascript - 如何检查多个 Jest spy 调用的多个参数?

typescript - 如何键入 MongoDB 投影结果

typescript - 如何使用 typescript 将对象插入数组

typescript - TypeScript 中任意数量类型的非析取联合

javascript - NodeJS 在 Jest 测试中写入标准输入