jestjs/jest · error · Error

Both --runInBand and --maxWorkers were specified, only one…

Error message

Both --runInBand and --maxWorkers were specified, only one is allowed.

What it means

The check() validator in jest-cli/args.ts rejects combining --runInBand (run all tests in the parent process) with --maxWorkers (spawn N worker processes), because they are two contradictory ways of controlling parallelism (args.ts:13-20). Jest cannot honor both, so it fails fast with a clear message rather than silently picking one.

Solutions

  1. Remove one of the flags: use --runInBand for serial in-process debugging, OR --maxWorkers=N for parallelism.
  2. If using --detectOpenHandles (which forces runInBand), drop the explicit --maxWorkers.
  3. Audit your package.json scripts and CI matrix for inherited flags that collide.

Example fix

// before
"scripts": { "test": "jest --runInBand --maxWorkers=2" }
// after
"scripts": { "test": "jest --runInBand" }
Defensive patterns

Strategy: validation

Validate before calling

function buildJestArgs(flags: { runInBand?: boolean; maxWorkers?: number | string }): string[] {
  if (flags.runInBand && flags.maxWorkers !== undefined) {
    throw new Error('Cannot use both runInBand and maxWorkers');
  }
  const args: string[] = [];
  if (flags.runInBand) args.push('--runInBand');
  else if (flags.maxWorkers !== undefined) args.push('--maxWorkers', String(flags.maxWorkers));
  return args;
}

Type guard

function isExclusiveConcurrency(flags: { runInBand?: boolean; maxWorkers?: number | string }): boolean {
  return Boolean(flags.runInBand) !== Boolean(flags.maxWorkers !== undefined);
}

Prevention

When it happens

Trigger: Running jest -i --maxWorkers=2; setting both in a script (e.g. jest --runInBand --maxWorkers=50%); combining a CI flag preset (runInBand) with a locally-tuned maxWorkers in npm scripts; detectOpenHandles (which implies runInBand) combined with an explicit maxWorkers.

Common situations: Copy-pasting flags from two different Stack Overflow answers into one npm script; a base script sets --runInBand for debugging and an override adds --maxWorkers; CI config sets runInBand=true while the developer adds -w for speed.

Related errors


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

Appendix: source

Thrown at packages/jest-cli/src/args.ts:17

/**
 * Copyright (c) Meta Platforms, Inc. and affiliates.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */

import type {Options} from 'yargs';
import type {Config} from '@jest/types';
import {constants, isJSONString} from 'jest-config';

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.',
      );
    }
  }

View on GitHub (pinned to 8e6d128e4a)