Jest Watcher
Jest Watcher is a core sub-package of the Jest testing framework, providing the interactive command-line interface (CLI) for running tests in 'watch' mode. It enables developers to receive instant feedback on code changes by automatically re-running relevant tests, filtering by filename or test name, and focusing on failed tests. The current stable version is 30.3.0. While Jest v30's major release cycle was notably long (three years), the project aims for more frequent major releases going forward, with minor and patch versions released regularly. Key differentiators include its tight integration with the broader Jest ecosystem for features like snapshot testing and code coverage, offering a dynamic and responsive development workflow compared to static, manual test execution.
Common errors
-
Jest encountered an unexpected token
cause Jest's transformer (e.g., Babel, ts-jest) is not processing a file, often in `node_modules`, or a non-JavaScript file is being imported.fixAdjust `transformIgnorePatterns` in `jest.config.js` to transpile the problematic module, or use `moduleNameMapper` to mock non-JS assets. Ensure Babel/TypeScript configuration is correct. -
Determining Test Suites to Run (Watcher hangs or fails to find tests)
cause Unresolvable `import`/`export` paths in modules, potentially due to bad merges or misconfigured module resolution.fixInspect module resolution paths and ensure all imports are correct. Adding `console.error` in the resolver logic might help pinpoint the exact file causing the issue. -
Error: Cross origin http://localhost forbidden
cause Jest's test environment is misconfigured, leading to unexpected browser-like security errors in Node.js tests.fixExplicitly set the `testEnvironment` in `jest.config.js` to `'node'` if testing server-side code, or ensure `'jsdom'` is correctly configured for browser environments. -
Jest did not exit one second after the test run completed. This usually means that there are asynchronous operations that weren't stopped in your tests.
cause Open handles (timers, network connections, etc.) are left active after tests complete, preventing Jest from exiting cleanly.fixRun Jest with `--detectOpenHandles` to identify the source of the open handles. Ensure all asynchronous operations are properly awaited or cleaned up in your tests or `setupFilesAfterEnv`.
Warnings
- breaking Jest 30.x has dropped support for older Node.js versions. Ensure your environment is running Node.js 18.14.0, 20.x, 22.x, or 24.x and above.
- breaking The minimum compatible TypeScript version is now 5.4. Projects using Jest's type definitions must update their TypeScript installation.
- breaking Several deprecated `expect` matcher aliases (e.g., `toBeCalled` for `toHaveBeenCalled`) have been fully removed. These were deprecated since Jest 26.
- breaking Snapshot header formats have changed in Jest 30, requiring all existing snapshots to be regenerated.
- breaking The `--testPathPattern` CLI option has been renamed to `--testPathPatterns` for consistency.
- gotcha Jest 30 bundles its modules into single files, which might break tools that relied on reaching into Jest's internal file structure.
- gotcha Jest 30 introduces `globalsCleanup` to detect and report global variables not cleaned up between test files, which can cause memory leaks. Default is 'soft'.
Install
-
npm install jest-watcher -
yarn add jest-watcher -
pnpm add jest-watcher
Imports
- BaseWatchPlugin
import { BaseWatchPlugin } from 'jest-watcher'; - Config
import type { Config } from 'jest'; - defineConfig
import { defineConfig } from 'jest';import { defineConfig } from 'jest-config';
Quickstart
import type { Config } from 'jest';
import { BaseWatchPlugin } from 'jest-watcher';
import type {
JestHookSubscriber,
WatchPlugin,
UsageInfo,
GlobalConfig,
UpdateConfigCallback,
} from 'jest-watcher';
// A minimal custom watch plugin that just logs a message
class MyCustomWatchPlugin extends BaseWatchPlugin implements WatchPlugin {
is);