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` (packages/jest-config/src/index.ts:355) treats a `--config` value that looks like JSON (passes isJSONString) by running `JSON.parse` on it; if parsing throws, Jest rethrows a generic error. This means the string started with `{`/`[` (so it was classified as JSON) but contained a syntax error.

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 f49721c78e)

Solutions

  1. Use a config FILE (jest.config.js/ts/json) and pass its path instead of an inline JSON string.
  2. If inlining JSON, ensure valid JSON syntax: double-quoted keys and strings, no trailing commas, no comments.
  3. Shell-escape the JSON correctly (single-quote the whole string on POSIX shells).

Example fix

# before (invalid JSON: unquoted keys)
jest --config '{testMatch: "**/*.test.js"}'

# after (valid JSON)
jest --config '{"testMatch": ["**/*.test.js"]}'
# or, preferred:
jest --config ./jest.config.js
Defensive patterns

Strategy: try-catch

Validate before calling

function tryParseInlineConfig(cfg: string): object {
  try { return JSON.parse(cfg); }
  catch { throw new Error('--config is not valid JSON; use a config file or fix the JSON'); }
}

Type guard

const looksLikeJson = (s: string) => {
  const t = s.trim();
  return t.startsWith('{') || t.startsWith('[');
};

Try / catch

try {
  JSON.parse(argv.config);
} catch {
  // fall back to treating --config as a file path instead of inline JSON
  argv.config = resolveConfigFilePath(argv.config);
}

Prevention

When it happens

Trigger: `jest --config '{testMatch: "**/*.test.js"}'` (unquoted keys are invalid JSON), trailing commas, single-quoted strings, or a malformed inline JSON object passed via CLI/shell.

Common situations: Shell quoting that strips inner quotes; hand-writing a JSON config inline rather than using a file; passing a JS object literal where JSON is required.

Related errors


AI-assisted analysis of jestjs/jest@f49721c78e (2026-08-03). Data as JSON: /data/errors/149fcc143c07883f.json. Report an issue: GitHub.