1.4 Debugging with Interactive watch mode
The previous section used .skip
and .only
modifiers to skip/ignore tests. It’s possible to skip and ignore tests by filtering with the Jest CLI in interactive watch mode.
To run the Jest CLI in interactive or watch mode, run:
yarn jest --watch
# or
npx jest --watch
Run only files whose name match a regex
Note: before running these instructions, clear any filters by entering
c
, more information about interactive commands by enteringw
.
By entering the p
mode, you can filter the filenames.
In the example repository, after running yarn jest
and pressing p
you’ll see the following prompt.
The effect is that only the files with many
in their name are run, which we can see from the following output:
Note, the files whose names don’t match the regex are not run at all, they’re not skipped they’re completely ignored.
Run only tests whose name match a regex
Before running these instructions, clear any filters by entering
c
, more information about interactive commands by enteringw
.
By entering the t
mode, you can filter the tests by their name.
In the example repository, after running yarn jest
and pressing t
you’ll see the following prompt:
The effect is that only the files with other
in their name are run, which we can see from the following output:
Note: the tests whose names don’t match the regex are skipped.
Run only changed tests/files
Watch mode is quite clever and by default tries to only run files that have changed since the last commit, so you’ll see output like so:
Run only failed tests
By using the f
option, you can run only failed tests.
Conclusion
We’ve done a thorough overview of Jest’s CLI and how one might go about selecting which tests to run both with modifiers and by using the interactive watch mode.
Next we’ll look at using spies, stubs and assertions over functions in order to test more complex JavaScript functionality.