typescript 找不到名称“描述”。您是否需要为测试运行程序安装类型定义?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/54139158/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cannot find name 'describe'. Do you need to install type definitions for a test runner?
提问by Ronin
When using TypeScript in conjunction with Jest, my specs would fail with error messages like:
将 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`.
The types are already installed.
类型已经安装。
I use:
我用:
"@types/jest": "^23.3.12",
"jest": "^23.6.0",
"ts-jest": "^23.10.5",
"typescript": "^3.1.6"
I run tests using jest --forceExit --coverage --verbose
我运行测试使用 jest --forceExit --coverage --verbose
采纳答案by Ronin
After fiddling with the tsconfig.json
for a while I finally figured, that commenting the "types": [],
will work.
在摆弄了tsconfig.json
一段时间后,我终于想通了,评论"types": [],
将起作用。
failing config (before)
失败的配置(之前)
// tsconfig.json
{
"compilerOptions": {
"types": []
}
}
working config
工作配置
// tsconfig.json
{
"compilerOptions": {
// "types": []
}
}
回答by Melvin Sy
I'm using VSCode as my IDE and in my Angular project, I had to comment-out/remove types in tsconfig.json and add jest in types at tsconfig.spec.json.
我使用 VSCode 作为我的 IDE,在我的 Angular 项目中,我不得不注释掉/删除 tsconfig.json 中的类型,并在 tsconfig.spec.json 中的类型中添加 jest。
tsconfig.json
配置文件
{
"compilerOptions": {
// "types": []
}
}
tsconfig.spec.json
tsconfig.spec.json
{
"compilerOptions": {
"types": ["jest", "node"]
}
}
回答by Victor Jatobá
You need to include in tsconfig.json your test path.
您需要在 tsconfig.json 中包含您的测试路径。
Example: suppose you named your path to tests/
and put it in root project directory, you need to specify in "include" param from tsconfig to look out for testes files:
示例:假设您将路径命名为tests/
并将其放在根项目目录中,您需要在 tsconfig 中的“include”参数中指定以查找 testes 文件:
- Go to:
tsconfig.json
- Add:
- 去:
tsconfig.json
- 添加:
"include": [
"tests/*.<file_test_extension>",
],
<file_test_extension>:
ts | js | etc.
<file_test_extension>:
ts | js | 等等。
Hope to help
希望有所帮助
回答by Daniel Manfred
You have to import jest in your test file:
您必须在测试文件中导入 jest:
import 'jest';
Another way to solve is adding in your tsconfig.json file:
另一种解决方法是在您的 tsconfig.json 文件中添加:
"compilerOptions": {
"types": [ "node", "jest" ],
"moduleResolution": "node"
}
回答by Polor Beer
You can have a separate tsconfig.json
in the test folder __tests__
:
您可以tsconfig.json
在测试文件夹中有一个单独的__tests__
:
{
"extends": "../tsconfig.json",
"compilerOptions": {
"baseUrl": "./",
"outDir": "../build",
"noEmit": true,
"rootDir": "../",
},
"exclude": ["node_modules"],
}
which extends the one in the root folder:
它扩展了根文件夹中的那个:
{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"moduleResolution": "node",
"outDir": "./lib",
"rootDir": "./src",
"strict": true,
"noImplicitAny": true,
"esModuleInterop": true,
},
"exclude": ["node_modules", "**/*.test.ts", "__tests__"]
}
This way you tests files still get excluded from the public build, but still get to share all the common options.
通过这种方式,您的测试文件仍然会被排除在公共构建之外,但仍然可以共享所有常见选项。
回答by Greg Woz
Simple checklist to solve this (for TypeScript and Jest).
解决此问题的简单清单(针对 TypeScript 和 Jest)。
- Make sure you have
@types/jest
and@types/node
installed. - Make sure you have linked these types in
tsconfig.json
so that:types: ["jest", "node"]
- Make sure you don't have your tests or the tests directory excludedfrom
tsconfig.json
configuration inexcluded
property.
- 确保您已安装
@types/jest
并@types/node
安装。 - 确保您已链接这些类型,
tsconfig.json
以便:types: ["jest", "node"]
- 确保您没有从属性中的
tsconfig.json
配置中排除测试或测试目录excluded
。
If you transpile to JS then you will need a separate tsconfig.json
in your tests that includes your test files along with the necessary compiler options and files to build the appropriate context.
如果您转译为 JS,那么您将需要tsconfig.json
在您的测试中单独包含您的测试文件以及必要的编译器选项和文件以构建适当的上下文。
回答by Fyodor
In my case, the problem was in one particular file. I hadn't found the issue itself but it was cured by adding import {} from 'jest'
to file's imports.
就我而言,问题出在一个特定文件中。我还没有发现问题本身,但它通过添加import {} from 'jest'
到文件的导入来解决。
No other way from jest issue tracker or SO or wherenot did help. Just some crazy bug fixed by some crazy workaround ?♂?
从玩笑问题跟踪器或 SO 或 wherenot 没有其他帮助。只是通过一些疯狂的解决方法修复了一些疯狂的错误?♂?
Yes, and I added the latest jest
, ts-jest
and @types/jest
to package.json of course
是的,我当然添加了最新的jest
,ts-jest
和@types/jest
package.json
回答by Ritesh
I am using mocha
, chai
and chai-http
for testing node express js project. I was not using types
in compilerOptions
earlier, but adding below setting in tsconfig.json
made it work for me:
我正在使用mocha
,chai
并chai-http
用于测试 node express js 项目。我没有用types
在compilerOptions
前面,但加入低于设定tsconfig.json
使得它对我来说有效:
{
"compilerOptions": {
// ...rest of my settings
"types": ["mocha", "chai", "chai-http"]
}
}
I hope it'll help someone..!!
我希望它会帮助某人.. !!