Javascript 使用 Jest 仅运行一项测试

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44446626/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 02:31:55  来源:igfitidea点击:

Run only ONE test with Jest

javascriptunit-testingjestjs

提问by jpenna

Very simple, I want to run just one test with Jest.

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

I put it.onlyor describe.onlybut it still runs a whole lot of tests. I think it runs all the tests since my last commit, but it shouldn't have this behavior with the onlyflag explicitly set, right?

我把it.onlyordescribe.only但它仍然运行了很多测试。我认为它运行了自我上次提交以来的所有测试,但它不应该在only显式设置标志的情况下具有这种行为,对吗?

What causes this behavior and how to run a single test?

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

回答by hinok

Jest parallelizes test runsand it doesn't know upfront which tests it should run and which it shouldn't run. This means when you use "fit", it will only run one test in that file but still run all other test files in your project.

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

fit, fdescribeand it.only, describe.onlyhave the same purpose, skip other tests, run only me.

fit,fdescribeit.only,describe.only有相同的目的,跳过其他测试,只运行我。

Source: https://github.com/facebook/jest/issues/698#issuecomment-177673281

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



Use jestfiltering mechanism, when you run your tests like

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

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

You can filter tests by a testnameor filename, just follow instructions in the terminal

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

enter image description here

在此处输入图片说明

Press p, then type a filename

p,然后键入文件名

enter image description here

在此处输入图片说明

Then you can use describe.onlyand it.onlywhich will skip all other tests from filtered, tested file.

然后您可以使用describe.onlyandit.only它将跳过过滤后的测试文件中的所有其他测试。

回答by rodgobbi

it.onlyand describe.onlywork only for the module they are in. If you are having problems to filter tests in multiple files, you can use jest -t name-of-spec, filtering tests that match the spec name (match against the name in describe or test).
Source: https://facebook.github.io/jest/docs/en/cli.html

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

For example, I focus the test which I'm currently writing like this (with the test script in the package.json):
npm test -- -t "test foo"

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

回答by tonco

For me it works if I use 2 params like this:

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

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

--watch: is optional

--watch:是可选的

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

-f: 将过滤你的文件,所以如果你有很多同名的测试,指定精确文件的完整路径

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

-t:与测试的“描述”名称或“它”名称一起使用

回答by hossein alipour

it's like this:

就像这样:

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

it will test all files with the name sometest.test.jsand matching based on -t option, if you only want to test a specific file you can do this:

它将sometest.test.js根据 -t 选项测试具有名称和匹配的所有文件,如果您只想测试特定文件,您可以执行以下操作:

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