jestjs/jest · error · Error

Jest: Cannot use configuration as an object without a file p

Error message

Jest: Cannot use configuration as an object without a file path.

What it means

Inside `readInitialOptions` (packages/jest-config/src/index.ts:342), if the caller passes a config OBJECT (not a string path) but no `parentConfigDirname`, Jest throws because it cannot resolve a `rootDir` for an inline object without a reference directory. The object form is only valid when resolving a sub-config whose parent directory is known.

Source

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

 */
export async function readInitialOptions(
  config?: string,
  {
    packageRootOrConfig = process.cwd(),
    parentConfigDirname = null,
    readFromCwd = false,
    skipMultipleConfigError = false,
  }: ReadJestConfigOptions = {},
): Promise<{config: Config.InitialOptions; configPath: string | null}> {
  if (typeof packageRootOrConfig !== 'string') {
    if (parentConfigDirname) {
      const rawOptions = packageRootOrConfig;
      rawOptions.rootDir = rawOptions.rootDir
        ? 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.',
      );
    }
  }

View on GitHub (pinned to f49721c78e)

Solutions

  1. Pass a string path (the package root or config file path) as the second argument instead of an object.
  2. If you must pass an object, also supply `parentConfigDirname` so rootDir can be derived.
  3. Use `normalize()` directly on a fully-formed config object if you do not need file resolution.

Example fix

// before
const { projectConfig } = await readConfig(argv, { testMatch: ['**/*.test.js'] });

// after
const { projectConfig } = await readConfig(argv, process.cwd());
// or provide a parent dir if you must pass an object:
// await readInitialOptions(undefined, { packageRootOrConfig: obj, parentConfigDirname: __dirname });
Defensive patterns

Strategy: validation

Validate before calling

import {readInitialOptions} from 'jest-config';
async function safeRead(obj: object, parentDir?: string) {
  if (typeof obj !== 'string' && !parentDir) {
    throw new Error('Pass a string path, or provide parentConfigDirname for an object config');
  }
  return readInitialOptions(undefined, { packageRootOrConfig: obj, parentConfigDirname: parentDir ?? null });
}

Type guard

const isStringPath = (p: unknown): p is string => typeof p === 'string';

Prevention

When it happens

Trigger: Calling the programmatic API `readConfig(argv, { testMatch: [...] })` (an object) without a parentConfigDirname; tooling that forwards a parsed config object where a path was expected.

Common situations: Custom test runners or build tooling that calls jest-config's readConfig directly with an in-memory config; incorrect migration from a path to an object argument.

Related errors


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