Skip to content

Files

Latest commit

 

History

History
96 lines (60 loc) · 4.07 KB

watch-mode.md

File metadata and controls

96 lines (60 loc) · 4.07 KB

Watch mode

Translations: Français, Italiano, Русский, 简体中文

AVA comes with an intelligent watch mode. It watches for files to change and runs just those tests that are affected.

Running tests with watch mode enabled

You can enable watch mode using the --watch or -w flags:

$ npx ava --watch

Please note that integrated debugging and the TAP reporter are unavailable when using watch mode.

Requirements

AVA uses fs.watch(). Support for recursive mode is required. Note that this has only become available on Linux since Node.js 20. Other caveats apply, for example this won't work well on network filesystems and Docker host mounts.

Ignoring changes

By default AVA watches for changes to all files, except for those with a .snap.md extension, ava.config.* and files in certain directories as provided by the ignore-by-default package.

You can configure additional patterns for files to ignore in the ava section of your package.json, or ava.config.* file, using the ignoreChanges key within the watchMode object:

export default {
	watchMode: {
		ignoreChanges: ['coverage'],
	},
};

If your tests write to disk they may trigger the watcher to rerun your tests. Configuring additional ignore patterns helps avoid this.

Filter tests while watching

You may also filter tests while watching by using the CLI. For example, after running

npx ava --watch

You will see a prompt like this:

 Type `g` followed by enter to filter test files by a glob pattern
 Type `m` followed by enter to filter tests by their title
 Type `r` followed by enter to rerun tests
 Type `u` followed by enter to update snapshots in selected tests
> 

So, to run only tests numbered like

  • foo23434
  • foo4343
  • foo93823

You can type m and press enter, then type foo* and press enter. This will then run all tests that match that glob.

Afterwards you can use the r command to run the matched tests again, or a command to run all tests.

Dependency tracking

AVA tracks which source files your test files depend on. If you change such a dependency only the test file that depends on it will be rerun. AVA will rerun all tests if it cannot determine which test file depends on the changed source file.

Dependency tracking works for require() and import syntax, as supported by @vercel/nft. import() is supported but dynamic paths such as import(myVariable) are not.

Files accessed using the fs module are not tracked.

Watch mode and CI

If you run AVA in your CI with watch mode, the execution will exit with an error (Error : Watch mode is not available in CI, as it prevents AVA from terminating.). AVA will not run with the --watch (-w) option in CI, because CI processes should terminate, and with the --watch option, AVA will never terminate.

Manually rerunning all tests

You can quickly rerun all tests by typing r on the console, followed by Enter.

Updating snapshots

You can update failing snapshots by typing u on the console, followed by Enter.

Debugging

Sometimes watch mode does something surprising like rerunning all tests when you thought only a single test would be run. To see its reasoning you can enable a debug mode. This will work best with the verbose reporter:

$ DEBUG=ava:watcher npx ava --watch