防止“测试/期望/等未定义”使用Jest时出错 [英] Prevent "test/expect/etc is not defined" errors when using Jest
问题描述
Facebook的Jest测试框架很容易
类似,尝试直接使用 node
运行测试文件将失败并显示 ReferenceError:test未定义
。
需要
/ import
需要为那些语句添加语句错误消失?
节点
如果要直接通过节点运行它们,请尝试要求 同时给出 jest
和/或 jest-runtime
。 @ types / jest
尝试一下。
检查编辑2以获取有关此内容的新信息
编辑
@ types / jest
( jest-DefinitelyTyped
)绝对需要(或只是一个解决方案)。如果您安装(例如,dev依赖项),则IDE错误应该消失。
我刚刚在Webstorm上试过它,它可以工作。
编辑2
新的 Jest @ 20 Matchers (例如, .resolves
和 .rejects
)仍未在 @ types / jest
中定义。您可以在以下链接中跟踪其状态:
https://github.com / DefinitelyTyped / DefinitelyTyped / pull / 16645
https://github.com / DefinitelyTyped / DefinitelyTyped / issues / 16803
尽管如此,它应该可用!
此外,似乎无法直接通过节点运行它。昨晚我尝试了很多不同的东西,但是使用jest是要走的路 - 它确实使用了引擎盖下的节点,所以我认为它也是可能的。 @thymikee 对你在GitHub上发布的问题明确表示不是。
编辑3
新版本( 20.0.1 )包含最新的Jest定义。
Lint
这不在这个特定问题的范围内,但它也有帮助
您使用的是 ESLint 吗?如果是这样,你需要 eslint-plugin-jest
按照本页描述的步骤: https://www.npmjs.com/package/eslint-plugin-jest ,你基本上需要将它添加为ESLint插件并在ESLint配置中设置jest globals:
{
env :{
jest / globals:true
}
}
如果您打算支持 ES6测试,您还需要 Babel
和 babel-jest
具有以下jest配置的插件:
transform:{
^。+ \ \ .js $:babel-jest
}
最后,对于 Typescript 测试你需要 @ types / jest
和 ts-jest
包
Facebook's Jest testing framework is easy to get started with, but the documentation overlooks an annoying aspect: test statements will be highlighted as errors by any editor that tries to warn of undefined symbols, because test
, expect
, and all matcher methods are not defined.
Similary, attempting to run a test file with node
directly will fail with ReferenceError: test is not defined
.
What require
/import
statement(s) need to be added for those errors to go away?
Node
If you want to run them directly through node, try to require Also give jest
and/or jest-runtime
.@types/jest
a try as well.
Check Edit 2 for new info about this
Edit
@types/jest
(jest-DefinitelyTyped
) is definitely needed (or just one solution). If you install it (e.g., dev dependency), the IDE errors should go away.
I just tried it on Webstorm, and it works.
Edit 2
The new Jest@20 Matchers (e.g., .resolves
and .rejects
) are still not defined in @types/jest
. You can keep track of its status on the links below:
https://github.com/DefinitelyTyped/DefinitelyTyped/pull/16645
https://github.com/DefinitelyTyped/DefinitelyTyped/issues/16803
It should be available soon, though!
Also, it doesn't seem possible to run it directly through node. Last night I tried a bunch of different things, but using jest is the way to go - it really uses node under the hood, so I thought it would be possible as well. @thymikee over your opened issue at GitHub made clear that it's not.
Edit 3
The new release (20.0.1) includes the newest Jest definitions.
Lint
this isn't in the scope of this specific problem, but it also helps
Are you using something like ESLint? If so, you'll need eslint-plugin-jest
Following the steps described in this page: https://www.npmjs.com/package/eslint-plugin-jest, you will basically need to add it as an ESLint plugin and set jest globals in the ESLint configuration:
{
"env": {
"jest/globals": true
}
}
If you plan on supporting ES6 tests, you'll also need Babel
and babel-jest
plugin with the following jest configuration:
"transform": {
"^.+\\.js$": "babel-jest"
}
Finally, for Typescript tests you'd need the @types/jest
and ts-jest
packages as well
这篇关于防止“测试/期望/等未定义”使用Jest时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!