只用 Jest 运行一次测试

我只想和 Jest 做一个测试。

我使用 it.onlydescribe.only,但它仍然运行大量的测试。我认为它运行了自上次提交以来的所有测试,但是它不应该显式设置 only标志的这种行为,对吗?

是什么导致了这种行为以及如何运行单个测试?

164550 次浏览

取笑 并行化测试运行,它不知道前面哪个测试它 应该运行和不应该运行。 这意味着当您使用“ fit”时,它将只运行该文件中的一个测试。 但是它仍然运行项目 .

中的所有其他测试文件

fitfdescribeit.onlydescribe.only有相同的用途: 跳过其他测试,只运行我。

资料来源: https://github.com/facebook/jest/issues/698#issuecomment-177673281


使用 Jest 过滤机制,

jest --config=jest.config.json --watch

你可以通过 testname或者 filename来过滤测试。只要按照终端中的说明去做就可以了。

Enter image description here

p,然后键入文件名。

Enter image description here

然后,您可以使用 describe.onlyit.only,它们将跳过所有其他测试,从筛选,测试文件。

it.onlydescribe.only只为它们所在的模块工作。如果在筛选多个文件中的测试时遇到问题,可以使用 jest -t name-of-spec,筛选匹配指定名称的测试(匹配描述或测试中的名称)。

资料来源: 一个 href = “ https://jestjs.io/docs/en/CLI.html”rel = “ noReferrer”> Jest CLI Options

例如,我像这样编写测试(使用 package.json中的测试脚本) :
npm test -- -t "test foo"

对我来说,如果我使用这样的两个参数,它就可以工作:

yarn test --watch -f "src/...fullpath.../reducer.spec.js" -t "Name of the test"

旗帜:

--watch: is optional


-f: will do filtering of your files, so if you have a lot of tests with the same name, specify the full path to the exact file


-t: works with 'describe' name or 'it' name of your test

是这样的:

jest sometest.test.js -t "some expression to match a describe or a test"

它将测试所有名称为 sometest.test.js并基于 -t 选项进行匹配的文件。如果您只想测试一个特定的文件,您可以这样做:

jest src/user/.../sometest.test.js

尝试在0上输入完整路径的结果与该模式匹配,即使该文件存在并且有两个断言。