jestjs/jest · error · Error

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

Error message

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

What it means

readInitialOptions (index.ts:333-386) accepts either a string (path) or a config object as packageRootOrConfig. When given an object, it requires a parentConfigDirname (the directory of the parent config that referenced it) to resolve relative paths; without one, it throws (index.ts:349-353). The guard prevents an object config from being used with no anchor for <rootDir> resolution.

Solutions

  1. Pass a path string (file or directory) instead of an object so Jest can resolve config from disk.
  2. If you must pass an object, also pass parentConfigDirname pointing at a directory (e.g. process.cwd()).
  3. Set rootDir explicitly in the object and supply a parentConfigDirname so replaceRootDirInPath works.

Example fix

// before
readInitialOptions(undefined, { packageRootOrConfig: { testMatch: ['**/*.test.js'] } });
// after
readInitialOptions(undefined, {
  packageRootOrConfig: { testMatch: ['**/*.test.js'] },
  parentConfigDirname: process.cwd(),
});
Defensive patterns

Strategy: validation

Validate before calling

function safeReadInitialOptions(opts: {
  packageRootOrConfig?: string | object;
  parentConfigDirname?: string | null;
}) {
  if (typeof opts.packageRootOrConfig !== 'string' && !opts.parentConfigDirname) {
    throw new Error('Pass parentConfigDirname when supplying a config object');
  }
  return readInitialOptions(undefined, opts as any);
}

Type guard

function isPathString(v: unknown): v is string {
  return typeof v === 'string';
}

Prevention

When it happens

Trigger: Calling readConfig/readInitialOptions with a config object programmatically but not passing parentConfigDirname; a custom runner or tool that passes InitialOptions directly without a file context; calling readInitialOptions({...}) from cwd with no parent.

Common situations: Tooling that wraps jest-config and feeds an in-memory config object; programmatic API misuse where the caller assumed cwd would be implied; refactoring from a path string to an object without updating the second argument.

Related errors


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

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