jestjs/jest · error · Error

Both -- and --watchAll were specified, but cannot be used…

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

check() loops over the git/change-detection flags (onlyChanged, lastCommit, changedFilesWithAncestor, changedSince) and rejects each when combined with --watchAll (args.ts:22-35). --watchAll reruns ALL tests on any file change, which would defeat the purpose of the change-scoped flags; Jest suggests --watch instead, which reruns only tests related to changed files.

Solutions

  1. Replace --watchAll with --watch (keeps the change-scoped behavior and re-runs only affected tests).
  2. Or drop the change-detection flag and keep --watchAll to rerun everything on save.
  3. Review your IDE/watch plugin settings so they don't inject --watchAll on top of an --onlyChanged script.

Example fix

// before
jest --onlyChanged --watchAll
// after
jest --onlyChanged --watch
Defensive patterns

Strategy: validation

Validate before calling

const CHANGE_FLAGS = ['onlyChanged','lastCommit','changedFilesWithAncestor','changedSince'] as const;
function validateWatch(argv: Record<string, any>): void {
  if (!argv.watchAll) return;
  for (const k of CHANGE_FLAGS) {
    if (argv[k]) throw new Error(`--${k} cannot be combined with --watchAll; use --watch`);
  }
}

Prevention

When it happens

Trigger: Running jest --onlyChanged --watchAll; jest -o --watchAll; jest --changedSince=main --watchAll; jest --lastCommit --watchAll; jest --changedFilesWithAncestor --watchAll.

Common situations: Combining a workflow that filters by git changes with a watch-all habit; migrating from --watch to --watchAll without dropping the change flag; an editor plugin passing --watchAll over a script that already sets --onlyChanged.

Related errors


AI-assisted analysis of jestjs/jest@8e6d128e4a (2026-08-10). Data as JSON: /api/errors/5349ed6a0dd967ea. Report an issue: GitHub.

Appendix: 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 8e6d128e4a)