193

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?

3
  • 1
    Possible duplicate of How do I run a single test using Jest? Commented Apr 11, 2018 at 14:56
  • 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. Commented Apr 12, 2018 at 9:37
  • @jpenna: just look at the original question. The same answers were given. Commented Mar 22, 2019 at 5:52

8 Answers 8

206

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.

Enter image description here

Press p, and then type a filename.

Enter image description here

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

Sign up to request clarification or add additional context in comments.

7 Comments

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
and this is exactly why Jest sucks and I like mocha
|
53

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"

4 Comments

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/…
@Kenny Using npm test -- -t 'test foo' worked for me on windows.
31

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

3 Comments

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!
the second way works for me as expected
19

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

1 Comment

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
2

I created a command that makes Jest work like you (and I) expect. This is work in progress, but should work like this:

Run tests from files that have .only tests/describes or run all of them if there's no .only.

grep --exclude-dir=node_modules -rl . -e 'test.only\|it.only\|describe.only' --null | tr '\n' ' ' | xargs -0 npx jest | grep . || npx jest

Even if this does not work in some edge cases you can take it as a reference. I might keep this more up-to-date here.

You can create an alias for this, put it in a shell script or in a package.json script.

Update: The | grep . || npx jest at the end is a workaround because xargs should run once with no input, but it does not on my machine. Maybe it's not needed normally.

Comments

2

According to https://stackoverflow.com/a/44446669/3957754 .only is and will be ignored in jest

This worked for me, only for LINUX users:

  • create a bash script
  • using grep to find files containing the word "only"
  • pass these files: jest foo.test.js bar.test.js

test.sh

filtered_test=$(grep -rnwl ./src/test -e "test.only\|it.only\|describe.only" --include \*.js | tr '\n' ' ')
jest --coverage --silent --runInBand --detectOpenHandles $filtered_test

package.json

"scripts": {
  "start": "...",
  "dev": "...",
  "test": "./src/test/test.sh"
},

Result: Only tests with only are executed

enter image description here

Note1: If your tests are not in src/test change that value in the bash script and package.json

Note2: Create simple npm module to do the same should be easy

Comments

0

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

Comments

0

A simple automated workaround is to use a bash script to create-and-run the jest command:

tools/runJest.sh

#!/bin/bash

function main() {
  local testDir="test"
  local testFiles="$(grep -rl '\.only(' $testDir | grep '\.test\.js$' | xargs)"

  if [ -z "$testFiles" ]; then
    runJest "--testPathPattern=test/unit"
  else
    runJest "$testFiles"
  fi
}

function runJest() {
  local testFiles="$1"
  local cmd="jest --logHeapUsage -c jest.config.js $testFiles"

  echo "Running cmd: $cmd"
  $cmd
}

main "$@"

What it does:

  • find all test files that have .only( in them
  • if there are .only( hits, run jest only on the files with hits
  • otherwise, run regular jest config

Make sure to add executable permissions to the bash script

chmod +x tools/runJest.sh

Also modify the script's search criteria (folder, filename patterns) to your specific setup.

package.json scripts.test

Instead of

{
  "scripts": {
    "test": "jest --logHeapUsage --testPathPattern=test/unit -c jest.config.js"
  }
}

Do this:

{
  "scripts": {
    "test": "tools/runJest.sh"
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.