172

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
jpenna
  • 8,426
  • 5
  • 28
  • 36
  • 1
    Possible duplicate of [How do I run a single test using Jest?](https://stackoverflow.com/questions/42827054/how-do-i-run-a-single-test-using-jest) – YPCrumble Apr 11 '18 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 Apr 12 '18 at 09:37
  • @jpenna: just look at the original question. The same answers were given. – Dan Dascalescu Mar 22 '19 at 05:52

8 Answers8

193

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Dawid Karabin
  • 5,113
  • 1
  • 23
  • 31
  • 2
    I am doing this filter by file, but it's weird... So the `.only` only works for the tests of the same file? – jpenna Jun 08 '17 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 Mar 16 '18 at 16:05
  • 4
    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 Feb 08 '19 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. https://imgur.com/z9FlQEZ – Dawid Karabin Feb 09 '19 at 10:33
  • 1
    @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. – adaam Feb 18 '19 at 14:36
  • 2
    and this is exactly why Jest sucks and I like mocha – PositiveGuy Nov 03 '20 at 05:00
  • Watch out for if your config file is js this should be obvious but I simply copied `--config=jest.config.json` and couldn't figure out why I was getting "Error: Can't find a root directory while resolving a config file path" – eazy_g Aug 09 '22 at 16:47
45

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"

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
rodgobbi
  • 1,442
  • 14
  • 10
  • 9
    This still runs every single test in my case and I have no idea why – Kenny Jun 12 '18 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 Jun 13 '18 at 14:16
  • This answer seems much more suitable for another question: https://stackoverflow.com/questions/42827054/how-do-i-run-a-single-test-using-jest – 千木郷 Jul 25 '18 at 06:33
  • @Kenny Using `npm test -- -t 'test foo'` worked for me on windows. – beckersense Mar 02 '23 at 09:37
25

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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Hossein Alipour
  • 575
  • 5
  • 9
  • For those looking to implement this option in a VSCode `launch.json` config file that will trigger the tests at a specific test case: https://medium.com/better-programming/how-to-use-vs-code-to-debug-unit-test-cases-6aebfd7021bd#4b7d – Gus Oct 06 '20 at 19:18
  • 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 Apr 19 '21 at 05:28
  • the second way works for me as expected – Adwait Mathkari Aug 29 '22 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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
tonco
  • 1,281
  • 3
  • 16
  • 30
  • 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 – Croad Langshan Apr 01 '22 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.

Ádám Bozzay
  • 529
  • 7
  • 19
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.

webmedia
  • 341
  • 1
  • 3
  • 8
0

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

JRichardsz
  • 14,356
  • 6
  • 59
  • 94
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"
  }
}
tveal
  • 96
  • 4