--watch flag. Tests automatically re-run when source files or test files change.
Basic usage
Enable watch mode:How it works
When a file changes, Bun:- Identifies which test files import the changed file
- Re-runs only the affected tests
- Displays results immediately
Watch mode features
Instant feedback
See test results as soon as you save:Smart test selection
Only affected tests run:Watch all tests
Re-run all tests on any change:a in watch mode to toggle between affected tests and all tests.
Watch mode commands
Interactive commands in watch mode:- Enter - Re-run tests
- a - Run all tests
- f - Run only failed tests
- t - Filter by test name pattern
- q - Quit watch mode
Filtering in watch mode
Filter by test name
Presst and type a pattern:
Run only failed tests
Pressf to run only tests that failed:
Watch with other flags
Combine watch mode with other test flags:Ignoring files
Configure which files to watch inbunfig.toml:
Watch specific files
Watch specific test files:Performance tips
Optimize test setup
Minimize expensive setup in test files:Use lazy imports
Import heavy dependencies lazily:Exclude slow tests
Skip slow tests in watch mode:Development workflow
Typical TDD workflow with watch mode:-
Start watch mode:
- Write a failing test
- Save the file - test runs automatically and fails
- Write implementation code
- Save - test runs automatically and passes
- Refactor with confidence
CI vs Development
Watch mode is for development only. In CI:Troubleshooting
Tests not re-running
Ensure files are being watched:Too many files being watched
Increase file watcher limit on Linux:Watch mode is slow
- Reduce the number of test files
- Optimize beforeAll/beforeEach hooks
- Use
test.concurrent()for parallel execution