jestjs/jest · error · Error
Both --${key} and --watchAll were specified, but cannot be u
Error message
Both --${key} and --watchAll were specified, but cannot be used together. Try the --watch option which reruns only tests related to changed files. What it means
The `check` function loops over the change-detection flags (`onlyChanged`, `lastCommit`, `changedFilesWithAncestor`, `changedSince`) and throws if any is combined with `--watchAll` (packages/jest-cli/src/args.ts:22). `--watchAll` reruns every test on any file change, which contradicts the 'only run tests for changed files' semantics. The message points users to `--watch`, which reruns only tests related to changed files.
Source
Thrown at packages/jest-cli/src/args.ts:29
export function check(argv: Config.Argv): true {
if (
argv.runInBand &&
Object.prototype.hasOwnProperty.call(argv, 'maxWorkers')
) {
throw new Error(
'Both --runInBand and --maxWorkers were specified, only one is allowed.',
);
}
for (const key of [
'onlyChanged',
'lastCommit',
'changedFilesWithAncestor',
'changedSince',
]) {
if (argv[key] && argv.watchAll) {
throw new Error(
`Both --${key} and --watchAll were specified, but cannot be used ` +
'together. Try the --watch option which reruns only tests ' +
'related to changed files.',
);
}
}
if (argv.onlyFailures && argv.watchAll) {
throw new Error(
'Both --onlyFailures and --watchAll were specified, only one is allowed.',
);
}
if (argv.findRelatedTests && argv._.length === 0) {
throw new Error(
'The --findRelatedTests option requires file paths to be specified.\n' +
'Example usage: jest --findRelatedTests ./src/source.js ' +
'./src/index.js.',View on GitHub (pinned to f49721c78e)
Solutions
- Replace `--watchAll` with `--watch` — `--watch` already implies change-detection scoping and composes with onlyChanged/changedSince.
- If you truly want to rerun ALL tests on every change, drop the changed-files flag instead.
Example fix
# before jest --onlyChanged --watchAll # after jest --onlyChanged --watch
Defensive patterns
Strategy: validation
Validate before calling
const CHANGED_FLAGS = ['onlyChanged','lastCommit','changedFilesWithAncestor','changedSince'] as const;
function validateWatchFlags(argv: any): void {
if (argv.watchAll && CHANGED_FLAGS.some(k => argv[k])) {
throw new Error('Use --watch (not --watchAll) with change-detection flags');
}
} Type guard
const usesChangedFiles = (a: any) => Boolean(a.onlyChanged || a.lastCommit || a.changedFilesWithAncestor || a.changedSince);
Prevention
- Prefer `--watch` whenever you also pass a changed-files flag.
- Centralize watch-mode scripts to avoid ad-hoc flag combos.
- Teammates: `-o`/`--onlyChanged` pairs with `--watch`, not `--watchAll`.
When it happens
Trigger: `jest --onlyChanged --watchAll`, `jest -o --watchAll`, `jest --changedSince=main --watchAll`, or `jest --lastCommit --watchAll`.
Common situations: Wanting watch mode plus change-scoping and reaching for the broader `--watchAll`; copy-pasting a watch script and adding a changed-files flag; IDE integrations that default to watchAll.
Related errors
- Both --onlyFailures and --watchAll were specified, only one
- The --maxWorkers (-w) option requires a number or string to
- Both --runInBand and --maxWorkers were specified, only one i
- The --findRelatedTests option requires file paths to be spec
- The --selectProjects option requires the name of at least on
AI-assisted analysis of jestjs/jest@f49721c78e (2026-08-03).
Data as JSON: /data/errors/5349ed6a0dd967ea.json.
Report an issue: GitHub.