ChatGPT解决这个技术问题 Extra ChatGPT

Run only ONE test with Jest

I want to run just one test with Jest.

I use it.only or describe.only, but 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 only flag explicitly set, right?

What causes this behavior and how can I run a single test?

If I accept the "duplicate flag" it "will mark your question as a duplicate, directing future readers to the original question and preventing further answers from being posted here." I don't think they are exactly the same, since the each question and answers are taking different approaches.
@jpenna: just look at the original question. The same answers were given.

P
Peter Mortensen

Jest parallelizes test runs and 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 it still runs all other test files in your project.

fit, fdescribe and it.only, describe.only have the same purpose: skip other tests and run only me.

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

Use the Jest filtering mechanism. When you run your tests like,

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

you can filter tests by a testname or filename. Just follow the instructions in the terminal.

https://i.stack.imgur.com/rBxQS.png

Press p, and then type a filename.

https://i.stack.imgur.com/ezrVi.png

Then you can use describe.only and it.only which will skip all other tests from the filtered, tested file.


I am doing this filter by file, but it's weird... So the .only only works for the tests of the same file?
I like that even more that .only thing, as I used to forget to undo .only in source code and commit that.
Even though the tests are skipped it still seems to evaluate the code under test in each example. Is there really not a way of only running a single test in 2019?
@adaam I'm not sure if I understood your question properly, so please correct me if I'm wrong. If tests are in separate files, only code in specific file is executed when you use pattern to run tests explicitly. When code for specific file is executed, the whole code in that file is parsed so even if you use it.only or it.skip for a single test that contains syntax error, all tests will fail. Of course code from files excluded by pattern will not be executed. imgur.com/z9FlQEZ
@hinok Stick a console.log in your Carousel component and rereun your test runner. For me, even if I use if.only it console.logs from the Carousel component as many times as there are skipped / unskipped tests for that Component in your watch patterns.
P
Peter Mortensen

it.only and describe.only work 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 specified name (match against the name in describe or test).

Source: Jest CLI Options

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"


This still runs every single test in my case and I have no idea why
The only explanation that I can think of is that you are passing a string argument that exists in every single test name (or test describe block). If that's the case you should narrow down to the exact test name you want to focus.
This answer seems much more suitable for another question: stackoverflow.com/questions/42827054/…
P
Peter Mortensen

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.js and matching based on -t option. If you only want to test a specific file, you can do this:

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

For those looking to implement this option in a VSCode launch.json config file that will trigger the tests at a specific test case: medium.com/better-programming/…
OMG, this is the only option that actually worked for me after spending two days trying to work around --testNamePattern option. Apparently, I needed to pass the test name as well!
P
Peter Mortensen

For me it works if I use two parameters like this:

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

Flags:

--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's -f switch is --onlyFailures, not filtering based on file paths ("Run tests that failed in the previous execution.") If you omit the -f this would work I think
w
webmedia

Trying to enter the full path results on 0 matches to that pattern, even though the file exists and has a couple of assertions.