Code Reference

Watch Mode

Jest · Reference cheat sheet

Watch Mode

Jest · Reference cheat sheet


📋 Overview

jest --watch / --watchAll re-runs tests on file changes. Interactive filters focus on failed tests, filenames, and related source files — ideal for TDD.

🔧 Core concepts

ModeBehavior
--watchGit-aware changed files
--watchAllAll tests on any change
Interactive keysFilter, update snapshots
--onlyChangedNon-interactive changed

Requires a git/hg repo for --watch heuristics; else use --watchAll.

💡 Examples

Scripts:

{
  "scripts": {
    "test": "jest",
    "test:watch": "jest --watch",
    "test:watchAll": "jest --watchAll"
  }
}

Interactive keys (typical):

a  run all tests
f  run only failed
o  only changed (watch)
p  filter by filename regex
t  filter by test name regex
u  update snapshots
q  quit

CLI filters without UI:

npx jest --watch --testPathPattern=Button
npx jest -t "submits form" --watchAll

CI: never use watch — use --ci.

⚠️ Pitfalls

  • --watch outside git showing confusing empty sets — use --watchAll.
  • Updating snapshots with u without reviewing.
  • Watching huge monorepos without roots / projects — slow.
  • Editors saving continuously causing thrash — debounce/save properly.
  • Relying on watch caches after dependency upgrades — restart.

On this page