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?
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.
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"
describe
block). If that's the case you should narrow down to the exact test name you want to focus.
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
launch.json
config file that will trigger the tests at a specific test case: medium.com/better-programming/…
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
-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
Trying to enter the full path results on 0 matches to that pattern, even though the file exists and has a couple of assertions.
Success story sharing
.only
only works for the tests of the same file?it.only
orit.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/z9FlQEZif.only
it console.logs from the Carousel component as many times as there are skipped / unskipped tests for that Component in your watch patterns.