{"id":"87445f8d7fdac148","repo":"jestjs/jest","slug":"jest-cannot-use-configuration-as-an-object-withou","errorCode":null,"errorMessage":"Jest: Cannot use configuration as an object without a file path.","messagePattern":"Jest: Cannot use configuration as an object without a file path\\.","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/jest-config/src/index.ts","lineNumber":350,"sourceCode":" */\nexport async function readInitialOptions(\n  config?: string,\n  {\n    packageRootOrConfig = process.cwd(),\n    parentConfigDirname = null,\n    readFromCwd = false,\n    skipMultipleConfigError = false,\n  }: ReadJestConfigOptions = {},\n): Promise<{config: Config.InitialOptions; configPath: string | null}> {\n  if (typeof packageRootOrConfig !== 'string') {\n    if (parentConfigDirname) {\n      const rawOptions = packageRootOrConfig;\n      rawOptions.rootDir = rawOptions.rootDir\n        ? replaceRootDirInPath(parentConfigDirname, rawOptions.rootDir)\n        : parentConfigDirname;\n      return {config: rawOptions, configPath: null};\n    } else {\n      throw new Error(\n        'Jest: Cannot use configuration as an object without a file path.',\n      );\n    }\n  }\n  if (isJSONString(config)) {\n    try {\n      // A JSON string was passed to `--config` argument and we can parse it\n      // and use as is.\n      const initialOptions = JSON.parse(config);\n      // NOTE: we might need to resolve this dir to an absolute path in the future\n      initialOptions.rootDir = initialOptions.rootDir || packageRootOrConfig;\n      return {config: initialOptions, configPath: null};\n    } catch {\n      throw new Error(\n        'There was an error while parsing the `--config` argument as a JSON string.',\n      );\n    }\n  }","sourceCodeStart":332,"sourceCodeEnd":368,"githubUrl":"https://github.com/jestjs/jest/blob/f49721c78e195558b40913977c9230f5b7f559d8/packages/jest-config/src/index.ts#L332-L368","documentation":"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.","triggerScenarios":"Calling the programmatic API `readConfig(argv, { testMatch: [...] })` (an object) without a parentConfigDirname; tooling that forwards a parsed config object where a path was expected.","commonSituations":"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.","solutions":["Pass a string path (the package root or config file path) as the second argument instead of an object.","If you must pass an object, also supply `parentConfigDirname` so rootDir can be derived.","Use `normalize()` directly on a fully-formed config object if you do not need file resolution."],"exampleFix":"// before\nconst { projectConfig } = await readConfig(argv, { testMatch: ['**/*.test.js'] });\n\n// after\nconst { projectConfig } = await readConfig(argv, process.cwd());\n// or provide a parent dir if you must pass an object:\n// await readInitialOptions(undefined, { packageRootOrConfig: obj, parentConfigDirname: __dirname });","handlingStrategy":"validation","validationCode":"import {readInitialOptions} from 'jest-config';\nasync function safeRead(obj: object, parentDir?: string) {\n  if (typeof obj !== 'string' && !parentDir) {\n    throw new Error('Pass a string path, or provide parentConfigDirname for an object config');\n  }\n  return readInitialOptions(undefined, { packageRootOrConfig: obj, parentConfigDirname: parentDir ?? null });\n}","typeGuard":"const isStringPath = (p: unknown): p is string => typeof p === 'string';","tryCatchPattern":null,"preventionTips":["Pass package root (a string) to readConfig in programmatic use.","Provide parentConfigDirname when forwarding inline config objects.","Use normalize() directly if you already hold a fully-formed config."],"tags":["jest-config","config","programmatic","rootdir"],"analyzedSha":"f49721c78e195558b40913977c9230f5b7f559d8","analyzedAt":"2026-08-03T20:16:28.571Z","schemaVersion":2}