在 TypeScript 中找不到名称“描述”错误

在 TypeScript 中找不到名称“描述”错误

Cannot find name ‘describe’ Error in TypeScript

要解决“无法找到名称‘describe’”错误,请为您的测试运行器安装类型定义,例如npm i -D @types/jestornpm i -D @types/jasmine
并确保
types在文件的数组中
添加包的类型
tsconfig.json

找不到名称描述

下面是错误如何发生的示例。

索引.ts
// ⛔️ Error: Cannot find name 'describe'. Do you // need to install type definitions for a test runner? // Try `npm i --save-dev @types/jest` or // `npm i --save-dev @types/mocha` and then // add 'jest' or 'mocha' to the types field in your tsconfig.ts(2593) describe('example', () => { it('does work', () => { expect(2).toBe(2); }); });

在项目的根目录中打开终端并为测试运行器安装类型。以下是jest,mocha和的评论jasmine

# 👇️ if you use jest npm i -D @types/jest ts-jest jest # 👇️ if you use mocha npm i -D @types/mocha mocha # 👇️ if you use jasmine npm i -D @types/jasmine jasmine

安装类型后,您必须将它们添加到文件中的types数组中tsconfig.json

如果您使用测试运行器,这应该是您的types数组的样子。jest

tsconfig.json文件
{ "compilerOptions": { "types": [ "reflect-metadata", "jest", // ... your other types ], // ... your other options } }

types如果您使用jasmine.

tsconfig.json文件
{ "compilerOptions": { "types": [ "jasmine", // ... your other types ], // ... your other options } }

如果在使用 中添加类型后仍然出现错误,请jasmine尝试在测试文件的开头添加导入:

索引.ts
import 'jasmine';

types如果您使用mocha.

tsconfig.json文件
{ "compilerOptions": { "types": [ "mocha", // ... your other types ], // ... your other options } }

如果错误仍未解决,请确保 TypeScript 正在选取您的测试所在的目录。如果您include在文件中设置了数组tsconfig.json,它还应该包括您的测试所在的目录。

例如,如果您的测试在一个src目录中,则以下配置就可以了:

tsconfig.json文件
{ "compilerOptions": {}, "include": ["src/**/*"], }

However, if your tests are in a tests directory next to your src directory,
TypeScript won’t pick them up with the config above.

tsconfig.json
{ "compilerOptions": {}, "include": [ "src/**/*", "tests/**/*" ], }

You can also use glob patterns. Here is an example that includes files ending in
.spec.ts and .test.ts.

tsconfig.json
{ "compilerOptions": {}, "include": [ "src/**/*", "**/*.spec.ts", "**/*.test.ts" ], }

The example above makes sure to also include your test files in your project. If
you need a way to exclude your test files from compilation, but still have them
type checked, check out my other article –
Exclude test files from Compilation in TypeScript.

If the error persists and your runtime is Node.js, make sure to install the
typings for node, by opening your terminal in your project’s root directory and
running the following command:

shell
npm i -D @types/node

Also if running your code using Node.js, make sure the types array in your
tsconfig.json file contains "node".

tsconfig.json
{ "compilerOptions": { "types": [ // ... your other types "node" ], }, }

If the error is not resolved, try to delete your node_modules and
package-lock.json files, re-run npm install and restart your IDE.

shell
rm -rf node_modules package-lock.json npm install

Make sure to restart your IDE if the error persists. VSCode glitches often and a
reboot solves things sometimes.

Conclusion #

To solve the “Cannot find name ‘describe'” error, install the type definitions
for your test runner, e.g. npm i -D @types/jest or npm i -D @types/jasmine
and make sure to add the typings for the package in the types array in your
tsconfig.json file.