jestjs/jest · error · Error

There was an error while parsing the `--config` argument as…

Error message

There was an error while parsing the `--config` argument as a JSON string.

What it means

readInitialOptions treats a --config value that looks like JSON (isJSONString returns true) as an inline JSON config and runs JSON.parse on it (index.ts:355-368). If parsing throws, Jest wraps the failure in this explicit message so the user knows the issue is malformed JSON rather than a missing file.

Solutions

  1. Validate the JSON with a linter or JSON.parse in a REPL before passing it.
  2. Use double-quoted keys and string values; remove trailing commas and comments.
  3. If the config is large or needs comments, put it in a jest.config.js/.ts file and pass the path instead.

Example fix

// before
jest --config "{testMatch: ['**/*.test.js']}"
// after
jest --config "{\"testMatch\":[\"**/*.test.js\"]}"
Defensive patterns

Strategy: validation

Validate before calling

function tryParseJsonConfig(cfg: string): object {
  try { return JSON.parse(cfg); }
  catch (e) { throw new Error(`Invalid JSON for --config: ${(e as Error).message}`); }
}

Type guard

function isStrictJson(s: string): boolean {
  try { JSON.parse(s); return true; } catch { return false; }
}

Prevention

When it happens

Trigger: jest --config '{testMatch: ...}' (unquoted keys, not valid JSON); jest --config '{"a":' (truncated); trailing commas; single quotes; comments in the JSON string; a copy-paste where a JS object literal was passed instead of JSON.

Common situations: Hand-writing an inline config and using JS object syntax instead of strict JSON; CI variable expansion that truncated the string; quoting bugs across shells; assuming JSONC (comments/trailing commas) is accepted.

Related errors


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

Appendix: source

Thrown at packages/jest-config/src/index.ts:364

        ? replaceRootDirInPath(parentConfigDirname, rawOptions.rootDir)
        : parentConfigDirname;
      return {config: rawOptions, configPath: null};
    } else {
      throw new Error(
        'Jest: Cannot use configuration as an object without a file path.',
      );
    }
  }
  if (isJSONString(config)) {
    try {
      // A JSON string was passed to `--config` argument and we can parse it
      // and use as is.
      const initialOptions = JSON.parse(config);
      // NOTE: we might need to resolve this dir to an absolute path in the future
      initialOptions.rootDir = initialOptions.rootDir || packageRootOrConfig;
      return {config: initialOptions, configPath: null};
    } catch {
      throw new Error(
        'There was an error while parsing the `--config` argument as a JSON string.',
      );
    }
  }
  if (!readFromCwd && typeof config == 'string') {
    // A string passed to `--config`, which is either a direct path to the config
    // or a path to directory containing `package.json`, `jest.config.js` or `jest.config.ts`
    const configPath = resolveConfigPath(
      config,
      process.cwd(),
      skipMultipleConfigError,
    );
    return {config: await readConfigFileAndSetRootDir(configPath), configPath};
  }
  // Otherwise just try to find config in the current rootDir.
  const configPath = resolveConfigPath(
    packageRootOrConfig,
    process.cwd(),

View on GitHub (pinned to 8e6d128e4a)