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.
You can enable watch mode using the --watch
or -w
flags. If you have installed AVA globally:
$ ava --watch
If you've configured it in your package.json
like this:
{
"scripts": {
"test": "ava"
}
}
You can run:
$ npm test -- --watch
You could also set up a special script:
{
"scripts": {
"test": "ava",
"watch:test": "ava --watch"
}
}
And then use:
$ npm run watch:test
Finally you could configure AVA to always run in watch mode by setting the watch
key in the ava
section of your package.json
, or ava.config.*
file.
package.json
:
{
"ava": {
"watch": true
}
}
Please note that the TAP reporter is unavailable when using watch mode.
AVA uses chokidar
as the file watcher. Note that even if you see warnings about optional dependencies failing during install, it will still work fine. Please refer to the Install Troubleshooting section of chokidar
documentation for how to resolve the installation problems with chokidar.
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 ignoredByWatcher
key.
If your tests write to disk they may trigger the watcher to rerun your tests. Configuring additional ignore patterns helps avoid this.
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 required modules. Custom extensions and transpilers are supported, provided you added them in your package.json
or ava.config.*
file, and not from inside your test file. Files accessed using the fs
module are not tracked.
The .only
modifier disables watch mode's dependency tracking algorithm. When a change is made, all .only
tests will be rerun, regardless of whether the test depends on the changed file.
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.
You can quickly rerun all tests by typing r on the console, followed by Enter.
You can update failing snapshots by typing u on the console, followed by Enter.
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 npm test -- --watch --verbose
On Windows use:
$ set DEBUG=ava:watcher
$ npm test -- --watch --verbose
Watch mode is relatively new and there might be some rough edges. Please report any issues you encounter. Thanks!