184

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?
    – YPCrumble
    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.
    – jpenna
    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

202

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.

7
  • 3
    I am doing this filter by file, but it's weird... So the .only only works for the tests of the same file?
    – jpenna
    Commented Jun 8, 2017 at 23:03
  • 5
    I like that even more that .only thing, as I used to forget to undo .only in source code and commit that.
    – Dziamid
    Commented Mar 16, 2018 at 16:05
  • 5
    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
    Commented Feb 8, 2019 at 17:29
  • 1
    @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 Commented Feb 9, 2019 at 10:33
  • 2
    and this is exactly why Jest sucks and I like mocha Commented Nov 3, 2020 at 5:00
51

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
  • 9
    This still runs every single test in my case and I have no idea why
    – Kenny
    Commented Jun 12, 2018 at 16:55
  • 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.
    – rodgobbi
    Commented Jun 13, 2018 at 14:16
  • This answer seems much more suitable for another question: stackoverflow.com/questions/42827054/…
    – 千木郷
    Commented Jul 25, 2018 at 6:33
  • @Kenny Using npm test -- -t 'test foo' worked for me on windows. Commented Mar 2, 2023 at 9:37
29

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
  • 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/…
    – Gus
    Commented Oct 6, 2020 at 19:18
  • 1
    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!
    – Mykola
    Commented Apr 19, 2021 at 5:28
  • the second way works for me as expected Commented Aug 29, 2022 at 15:11
18

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
  • 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 Commented Apr 1, 2022 at 12:42
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.

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

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.

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"
  }
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.