找不到名称“描述”。您是否需要为测试运行程序安装类型定义?

Ron*_*nin 8 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`.
Run Code Online (Sandbox Code Playgroud)

类型已经安装。

我用:

    "@types/jest": "^23.3.12",
    "jest": "^23.6.0",
    "ts-jest": "^23.10.5",
    "typescript": "^3.1.6"
Run Code Online (Sandbox Code Playgroud)

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

Gre*_*Woz 53

解决此问题的简单清单(针对 TypeScript 和 Jest)。

  1. 确保您已安装@types/jest@types/node安装。
  2. 确保您已链接这些类型,tsconfig.json以便:types: ["jest", "node"]
  3. 确保您没有从属性中的tsconfig.json配置中排除测试或测试目录excluded

如果您转译为 JS,那么您将需要一个单独tsconfig.json的测试,其中包括您的测试文件以及必要的编译器选项和文件以构建适当的上下文。

  • 我认为这是最完整的答案,但是,重新启动 VSCode 非常重要。就我而言,我没有重新启动,这浪费了我大量的时间。 (8认同)
  • 我发现,如果我从 tsconfig.json 中的“排除”属性中删除 , "**/*.spec.ts" ,那么 VS Code 中的红色下划线就会消失,以表示 .spec 文件中的“描述”,但是那些文件现在作为包的一部分输出到 /lib 文件夹中。似乎没有办法从包中排除 .spec 文件,但包含部分文件以使“描述”错误消失? (6认同)
  • 对我来说,我还需要将“test/”目录添加到“tsconfig.json”的“include”部分,然后它就起作用了。这个答案让我觉得这可能是需要做的事情。 (5认同)

Ste*_*evo 24

以上都没有解决我的问题。我不得不添加"@types/jest"到文件中的types数组中tsconfig.json


Dan*_*red 21

您必须在测试文件中导入 jest:

import 'jest';
Run Code Online (Sandbox Code Playgroud)

另一种解决方法是在您的 tsconfig.json 文件中添加:

   "compilerOptions": {
     "types": [ "node", "jest" ],
     "moduleResolution": "node"
   }
Run Code Online (Sandbox Code Playgroud)

如果您使用的是 tslint,则问题可能是 tsconfig.json 末尾没有必要的逗号,例如:

{
  "compileOnSave": true,
  "include": [
    "src"
  ],  // remove this comma
}
Run Code Online (Sandbox Code Playgroud)


Jel*_*ier 20

我能够解决这个问题的唯一方法是将tests/文件夹添加到文件中的“包含”中tsconfig.json

"include": [
  "src/**/*.ts",
  "tests/*.ts"
] 
Run Code Online (Sandbox Code Playgroud)

对于那些也有eslint抱怨的人,您需要添加jest到您的.eslintrc.json文件中:

"env": {
  "es2020": true,
  "node": true,
  "jest": true
}
Run Code Online (Sandbox Code Playgroud)


小智 13

这对我有用:

import '@types/jest';
Run Code Online (Sandbox Code Playgroud)

  • 它给了我``无法导入类型声明文件。运行测试时考虑导入“jest”而不是“@types/jest”。``。 (16认同)
  • 不错的快速修复! (3认同)

Mel*_* Sy 10

我使用VSCode作为我的IDE,并且在我的Angular项目中,我不得不注释掉/删除tsconfig.json中的类型,并在tsconfig.spec.json中添加了类笑话。

tsconfig.json

{
  "compilerOptions": {
    // "types": []
  }
}
Run Code Online (Sandbox Code Playgroud)

tsconfig.spec.json

{
  "compilerOptions": {
    "types": ["jest", "node"]
  }
}
Run Code Online (Sandbox Code Playgroud)

  • 我还必须从我的 tsconfig 文件中删除“typeRoots”,这显然覆盖了“types”。 (15认同)
  • @Freekwalker——删除 `typeRoots` 为我解决了这个问题 (9认同)
  • 注意:如果 tsconfig 中已经定义了“jest”,则只需将“jest”添加到“compilerOptions.types”中。如果完全省略,该设置的默认行为是包含“node_modules/@jest”下的所有类型。我遇到这个问题是因为我的项目的“types: []”配置错误,但在完全删除它之后,我得到了正确输入的 Jest 内容。通过按照此解决方案中显示的方式定义它,您将失去在“node_modules/@types”下隐式包含其他类型(这还不错,只是需要注意的副作用)。 (4认同)

小智 10

您需要在 tsconfig.json 中包含您的测试路径。

示例:假设您将路径命名为tests/并将其放在根项目目录中,您需要在 tsconfig 中的“include”参数中指定以查找 testes 文件:

  1. 去: tsconfig.json
  2. 添加:
"include": [
    "tests/*.<file_test_extension>",
],
Run Code Online (Sandbox Code Playgroud)
  • <file_test_extension>:ts | js | 等等。

希望有所帮助

  • 但这将开始将测试文件复制到 lib 文件夹,该文件夹将被部署 (14认同)

小智 8

在我的情况(与代码,创建反应的应用程序内,纱线工作区,开玩笑@ 26,@类型/笑话,"types": ["node", "jest"]目前在tsconfig.json测试运行正常,但IDE是用红色下划线的所有describeit。在我尝试了很长时间后重新加载 VS Code 窗口之前,没有任何帮助???

  • 是的,我完全关闭并重新打开 VS Code,然后它就工作了 =) 谢谢 (2认同)

Jia*_*hen 7

格雷格·沃兹的答案是最完整的。就我而言,由于某种原因,初始tsconfig.json文件包含"exclude": ["node_modules", "**/__tests__/*"],这是根本原因。之后我删除了"**/__tests__/*". 并确保它还包括:"types": ["jest"]。有用。

此外,配置更改后重新启动 Visual Studio Code也很重要。它浪费了我几个小时的时间尝试所有不同的方法而不重新启动它。


小智 7

以下配置对我有用。我添加node_modules/@typestypeRoots.

{
  "compilerOptions": {
    // ...rest of my settings
    "typeRoots": ["node_modules/@types"],
    "types": ["jest", "node"]
  }
}
Run Code Online (Sandbox Code Playgroud)


小智 7

另一件可能出错的事情是,如果您在项目上方的父目录中打开了 Visual Studio Code。这发生在我身上,因为我们使用的是Visual\xc2\xa0Studio解决方案,并且我打开了整个解决方案,而不仅仅是项目。

\n

简而言之,确保 Visual Studio Code 对项目的根目录开放。

\n


wpr*_*prl 7

至少从 Jest 25 开始,就可以直接导入 Jest 全局变量了。这通常与injectGlobals: false配置选项或--injectGlobals=falseCLI 结合使用。

例如:

import { describe, expect, it, test } from '@jest/globals';


Ron*_*nin 6

经过tsconfig.json一段时间的摸索之后,我终于想出了,评论该注释"types": [],将起作用。

配置失败(之前)

// tsconfig.json
{
  "compilerOptions": {
    "types": []
  }
}
Run Code Online (Sandbox Code Playgroud)

工作配置

// tsconfig.json
{
  "compilerOptions": {
    // "types": []
  }
}
Run Code Online (Sandbox Code Playgroud)

  • 或在类型中添加笑话:`“ types”:[“ jest”]` (3认同)

Pol*_*eer 6

您可以tsconfig.json在测试文件夹中有一个单独的__tests__

{
    "extends": "../tsconfig.json",
    "compilerOptions": {
        "baseUrl": "./",
        "outDir": "../build",
        "noEmit": true,
        "rootDir": "../",
    },
    "exclude": ["node_modules"],
}
Run Code Online (Sandbox Code Playgroud)

它扩展了根文件夹中的那个:

{
  "compilerOptions": {
    "target": "es6",
    "module": "commonjs",
    "moduleResolution": "node",
    "outDir": "./lib",
    "rootDir": "./src",
    "strict": true,
    "noImplicitAny": true,
    "esModuleInterop": true,
  },
  "exclude": ["node_modules", "**/*.test.ts", "__tests__"]
}
Run Code Online (Sandbox Code Playgroud)

通过这种方式,您的测试文件仍然会被排除在公共构建之外,但仍然可以共享所有常见选项。


Aar*_*n_H 6

对于Lerna 整体存储库用户

我正在运行一个 Lerna 整体存储库,以下是我必须执行的修复操作:

  1. 确保位于根包以及目录中单个包的文件package.json"@types/jest"的 devDependencies 中,并且您已运行在目录中安装/链接这些包packages/lerna bootstrapnode_modules

  2. 确保该"types": ["node", "jest"]部分位于您的根 tsconfig.json 中。

  3. 我添加import 'jest';到了个人 *.test.ts 文件的顶部。

  • 我是 lerna monorepo 用户,我绝对拒绝在我的测试文件中添加“import jest”。幸运的是,前两个步骤似乎就是我所需要的。 (3认同)

Dev*_*Dev 6

Freewalker 在评论中建议的解决方案很容易被错过。从 tsconfig 文件中删除“typeRoots”(这显然覆盖了“types”)解决了问题。


小智 5

您需要在 tsconfig.json 中包含您的测试路径。

我通过在我的项目根目录中有 atsconfig.json和 a解决了这个问题tsconfig.build.jsontsconfig.json包含所有选项,包括

"include": ["src/**/*", "test/**/*"],
Run Code Online (Sandbox Code Playgroud)

tsconfig.build.json

{
  "extends": "./tsconfig.json",
  "include": ["src/**/*"]
}
Run Code Online (Sandbox Code Playgroud)

然后在package.json(干净的脚本可选):

"scripts": {
  "clean": "rm -rf dist",
  "build": "npm run clean && tsc --build tsconfig.prod.json,
  ...
}
Run Code Online (Sandbox Code Playgroud)