debugging jest: step through jest code with `--inspect-brk`

2021-04-29

 | 

~1 min read

 | 

195 words

In Debugging Node, I discussed how to use Node’s --inspect flag to debug a Node process.

This is also really helpful for when we want to step through a Jest process and debug it without inserting a lot of console statements.

Within the terminal (or adding a test:debug script to your package.json), we will start a Node process with the --inspect-brk flag, and then tell it which process to run (in our case, that’s jest):

node --inspect-brk ./node_modules/jest/bin/jest.js --runInBand --watch

So, what’s going on?

  1. ./node_modules/jest/bin/jest.js is where the Jest binary lives that we want to run.
  2. We use the --runInBand flag because Jest runs tests in parallel (which is great for making it a snappy test runner, but bad for our purposes here where we want to be able to step through the code).
  3. Finally, we used --watch because that will make it more dynamic and easier to see how changes affect the code.

That’s it! With that change, we’re able to debug our Jest code just like a normal process! Node will pause automatically on a debugger statement and the whole thing can be navigated within a Chromium browser.


Related Posts
  • jest-tips


  • Hi there and thanks for reading! My name's Stephen. I live in Chicago with my wife, Kate, and dog, Finn. Want more? See about and get in touch!