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 entering w.

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.

Jest CLI ‘p’ match mode, with ‘many’ as the pattern match

The effect is that only the files with many in their name are run, which we can see from the following output:

Jest CLI test run output in ‘p’ match mode, with ‘many’ as the pattern match

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 entering w.

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:

Jest CLI ’t’ match mode, with ‘other’ as the pattern match

The effect is that only the files with other in their name are run, which we can see from the following output:

Jest CLI test run output with ’t’ match mode, ‘other’ as the pattern match

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:

Jest Watch output when no files have changed, no tests are run

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.

Jump to table of contents