使用Jest仅运行一次测试

jpe*_*nna 62 javascript unit-testing jestjs

很简单,我想用Jest运行一个测试.

我放it.onlydescribe.only但它仍然运行了很多测试.我认为自从我上一次提交以来它运行了所有测试,但它不应该only明确设置标志的这种行为,对吧?

导致此行为的原因以及如何运行单个测试?

hin*_*nok 69

Jest 并行化了测试运行,它不知道应该运行哪些测试以及哪些测试不应该运行.这意味着当您使用"fit"时,它只会在该文件中运行一个测试,但仍会运行项目中的所有其他测试文件.

fit,fdescribe并且it.only,describe.only有相同的目的,跳过其他的,只运行我.

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


使用jest过滤机制,当你运行你的测试时

jest --config=jest.config.json --watch
Run Code Online (Sandbox Code Playgroud)

您可以通过testname或过滤测试filename,只需按照终端中的说明操作即可

在此输入图像描述

p,然后键入文件名

在此输入图像描述

然后你可以使用describe.onlyit.only这将跳过从过滤,测试文件中的所有其他测试.

  • 即使跳过了测试,它似乎仍然在每个示例中评估被测代码。2019年真的没有办法只跑一次测试吗? (3认同)
  • 我正在按文件执行此过滤器,但很奇怪...所以`.only` 仅适用于同一文件的测试? (2认同)
  • 我更喜欢.only东西,因为我曾经忘记在源代码中撤消.only并提交。 (2认同)
  • 这正是 Jest 很糟糕而我喜欢摩卡的原因 (2认同)

rod*_*bbi 17

it.only并且describe.only仅适用于它们所在的模块.如果您在多个文件中过滤测试时遇到问题,可以使用jest -t name-of-spec,过滤与规范名称匹配的测试(与describe或test中的名称匹配).
资料来源:https://facebook.github.io/jest/docs/en/cli.html

例如,我关注我正在编写的测试(使用测试脚本package.json):
npm test -- -t "test foo"

  • 对于我来说,这仍然会运行每个测试,我不知道为什么 (4认同)

ton*_*nco 14

对我来说,如果我使用两个这样的参数,它会起作用:

yarn test --watch -f "src/...fullpath.../reducer.spec.js" -t "Name of the test"
Run Code Online (Sandbox Code Playgroud)

标志:

--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
Run Code Online (Sandbox Code Playgroud)


Hos*_*our 13

就像这样:

jest sometest.test.js -t "some expression to match a describe or a test"
Run Code Online (Sandbox Code Playgroud)

它将sometest.test.js根据 -t 选项测试具有名称和匹配的所有文件。如果你只想测试一个特定的文件,你可以这样做:

jest src/user/.../sometest.test.js
Run Code Online (Sandbox Code Playgroud)