13

I have many jest tests, within many test suites, within many test files.

I need to isolate and debug a single test.

I'm debugging via node --inspect-brk ./node_modules/jest/bin/jest, so other solutions involving watch mode are too complicated.

How can I skip all tests except the one I need to debug?

A-Sharabiani
  • 17,750
  • 17
  • 113
  • 128
ChaseMoskal
  • 7,151
  • 5
  • 37
  • 50
  • 2
    Possible duplicate of [Run only ONE test with Jest](https://stackoverflow.com/questions/44446626/run-only-one-test-with-jest) – blex May 23 '18 at 21:34
  • 1
    i clarified the question to differentiate it from the others — this question is about debugging, and requires a solution that is compatible with the `node --inspect-brk` method of debugging – ChaseMoskal May 24 '18 at 02:35

4 Answers4

9

jest handles this in two steps

  1. isolate your test file by running jest with the testPathPattern (jest docs) command line argument

    node --inspect-brk ./node_modules/jest/bin/jest --testPathPattern="integration.test"
    

    here integration.test is supplied as a regular expression to filter for the correct test file

  2. isolate your test function
    there are two ways to do this

    • one way is to use the testNamePattern (jest docs) command line argument

      node --inspect-brk ./node_modules/jest/bin/jest --testNamePattern="host events"
      

      here host events is supplied as a regular expression to filter for the correct test suite name

    • alternatively, you can add .only to your test function:

      • so that test("object works", async() => {/*...*/})

      • becomes test.only("object works", async() => {/*...*/})

ChaseMoskal
  • 7,151
  • 5
  • 37
  • 50
7

I am using VSCode for debugging and in launch.json add to runTimeArgs a testPathPattern as your folder name:

{
"version": "0.2.0",
"configurations": [{
    "name": "Debug Jest Tests",
    "type": "node",
    "request": "launch",
    "runtimeArgs": [
        "--inspect-brk",
        "${workspaceRoot}/node_modules/jest/bin/jest.js",
        "--runInBand",
        "--testPathPattern=<NameOfTheFolder-you-want-to-test-without-this-angle-brackets>"
    ],
    "console": "integratedTerminal",
    "internalConsoleOptions": "neverOpen",
    "port": 9229
}]
}
Binoy S Kumar
  • 289
  • 4
  • 9
1

https://marketplace.visualstudio.com/items?itemName=firsttris.vscode-jest-runner

Another extension. This one uses CodeLens so there are Run|Debug links above each test:

jest runner unit test

Hawkeye Parker
  • 7,817
  • 7
  • 44
  • 47
0

If you are using VSCode, Jest extension might come in handy.

https://marketplace.visualstudio.com/items?itemName=Orta.vscode-jest

Asanka Siriwardena
  • 871
  • 13
  • 18