编写用Flow验证的Jest测试的正确方法是什么? [英] What's the right way to write Jest tests verified with Flow?
问题描述
我想象人们通常一起使用Flow和Jest(和React),但是Flow似乎并不了解Jest(或Jasmine)全局变量.当我在测试中添加// @flow
时,会出现类似以下的Flow错误:
I imagine people commonly use Flow and Jest (and React) together, but Flow doesn't seem to know about Jest (or Jasmine) globals. When I add // @flow
to my tests, I get Flow errors like this:
src/__tests__/Thing-test.js:3
3: jest.unmock('../Thing')
^^^^ identifier `jest`. Could not resolve name
src/__tests__/Thing-test.js:7
7: describe('Thing', () => {
^^^^^^^^ identifier `describe`. Could not resolve name
src/__tests__/Thing-test.js:8
8: it('does stuff', () => {
^^ identifier `it`. Could not resolve name
我可以为Jest/Jasmine编写一个Flow接口,但这似乎很冗长,就像我必须丢失一些东西一样.让Flow处理node_modules/jest-cli
似乎无济于事.
I could write a Flow interface for Jest/Jasmine, but that seems lengthy and like I must be missing something. Letting Flow process node_modules/jest-cli
doesn't seem to help.
推荐答案
尽管Jest是用流注释编写的,但它们剥离了npm版本的类型,因此我们不需要babel来运行它.幸运的是,这些类型已经在 flow-type 中,因此解决方案非常简单(就像评论中提到的那样):
Although Jest is written with flow annotations they strip types for the npm version so we don't need babel to run it. Fortunately the types are already in flow-type so the solution is quite easy (just as mentioned in the comment):
npm install -g flow-typed
flow-typed install jest@22.x.x # <-- replace the version with the latest
尽管我必须将此行也添加到我的.eslintrc.json
:
Although I had to add this line as well to my .eslintrc.json
:
{
"env": {
"jest": true
}
}
这篇关于编写用Flow验证的Jest测试的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!