~2 min read|
To run Jest in watch mode, pass the
% jest --watch
By default, Jest will run tests against all changed files since the last commit, but watch mode also exposes many options. By hitting the
w key, you can see the different ways to operate Jest in watch mode.
The options are dynamic, so it’s worth playing around within watch mode to see what’s available. A summary of some of the common options:
a is equivalent to running
jest --watchAll, which means it runs all tests, not just those since last commit.
f is useful for focusing only on broken tests
p is a way to filter quickly to specific test files
t is similar to
p but looks at the test title, i.e., the part that’s in quotes after
q exits watch mode
enter will trigger a re-run (useful, though the point of watch is that Jest will re-run any time there’s a meaningful change to files within scope of the current filters).
› Press a to run all tests.
› Press f to run only failed tests.
› Press p to filter by a filename regex pattern.
› Press t to filter by a test name regex pattern.
› Press q to quit watch mode.
› Press Enter to trigger a test run.
There are two other options that pop up if you had failing snapshots:
u will update all of the snapshots while
i is interactive and will run each test with a failing snapshot one at a time and give you the option to update it.
Overall, Jest’s watch mode is a great way to keep an eye on your tests and speed up the workflow. Not only does it save you the hassle of rerunning tests frequently, but the filter options make it much simpler ot run only the subset of tests that are relevant to your changes, probably an even bigger savings in terms of the REPL cycle time by avoiding unnecessary tests.
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!