在 TypeScript 中找不到名称“描述”错误
Cannot find name ‘describe’ Error in TypeScript
要解决“无法找到名称‘describe’”错误,请为您的测试运行器安装类型定义,例如npm i -D @types/jest
ornpm i -D @types/jasmine
并确保types
在文件的数组中
添加包的类型tsconfig.json
。
下面是错误如何发生的示例。
// ⛔️ 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
{ "compilerOptions": { "types": [ "reflect-metadata", "jest", // ... your other types ], // ... your other options } }
types
如果您使用jasmine
.
{ "compilerOptions": { "types": [ "jasmine", // ... your other types ], // ... your other options } }
如果在使用 中添加类型后仍然出现错误,请jasmine
尝试在测试文件的开头添加导入:
import 'jasmine';
types
如果您使用mocha
.
{ "compilerOptions": { "types": [ "mocha", // ... your other types ], // ... your other options } }
如果错误仍未解决,请确保 TypeScript 正在选取您的测试所在的目录。如果您include
在文件中设置了数组tsconfig.json
,它还应该包括您的测试所在的目录。
例如,如果您的测试在一个src
目录中,则以下配置就可以了:
{ "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.
{ "compilerOptions": {}, "include": [ "src/**/*", "tests/**/*" ], }
You can also use glob patterns. Here is an example that includes files ending in
.spec.ts
and .test.ts
.
{ "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:
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"
.
{ "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.
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.