jestjs/jest · error · Error

"cwd" must be an absolute path. cwd

Error message

"cwd" must be an absolute path. cwd: ${cwd}

What it means

Thrown at the top of resolveConfigPath when the cwd argument is not an absolute path. resolveConfigPath later uses path.resolve(cwd, pathToResolve), so a relative cwd would produce incorrect config resolution. The check is path.isAbsolute(cwd).

Solutions

  1. Pass path.resolve(cwd) (or process.cwd(), which is absolute) as the cwd argument
  2. If calling from user code, ensure cwd starts with '/' (posix) or a drive letter (windows)
  3. Normalize early: const absCwd = path.isAbsolute(cwd) ? cwd : path.resolve(cwd)

Example fix

// before
resolveConfigPath('./config', 'src');

// after
resolveConfigPath('./config', path.resolve('src'));
Defensive patterns

Strategy: validation

Validate before calling

import * as path from 'node:path';
function resolveCwd(cwd: string): string {
  if (!path.isAbsolute(cwd)) {
    throw new Error(`cwd must be absolute, got: ${cwd}`);
  }
  return cwd;
}

Type guard

function isAbsolutePath(p: unknown): p is string {
  return typeof p === 'string' && path.isAbsolute(p);
}

Prevention

When it happens

Trigger: Calling resolveConfigPath directly with a relative cwd (e.g. '.' or 'src'), or wiring Jest programmatically with process.cwd() replaced by a relative value. Not typically hit via the CLI since Jest sets cwd to an absolute path internally.

Common situations: Custom Jest runners or wrappers that pass config.rootDir or a computed relative path as cwd; test harnesses that change process.cwd() before invoking Jest APIs.

Related errors


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

Appendix: source

Thrown at packages/jest-config/src/resolveConfigPath.ts:31

import {
  JEST_CONFIG_BASE_NAME,
  JEST_CONFIG_EXT_ORDER,
  PACKAGE_JSON,
} from './constants';
import {BULLET, DOCUMENTATION_NOTE} from './utils';

const isFile = (filePath: string) =>
  fs.existsSync(filePath) && !fs.lstatSync(filePath).isDirectory();

const getConfigFilename = (ext: string) => JEST_CONFIG_BASE_NAME + ext;

export default function resolveConfigPath(
  pathToResolve: string,
  cwd: string,
  skipMultipleConfigError = false,
): string {
  if (!path.isAbsolute(cwd)) {
    throw new Error(`"cwd" must be an absolute path. cwd: ${cwd}`);
  }
  const absolutePath = path.isAbsolute(pathToResolve)
    ? pathToResolve
    : path.resolve(cwd, pathToResolve);

  if (isFile(absolutePath)) {
    return absolutePath;
  }

  // This is a guard against passing non existing path as a project/config,
  // that will otherwise result in a very confusing situation.
  // e.g.
  // With a directory structure like this:
  //   my_project/
  //     package.json
  //
  // Passing a `my_project/some_directory_that_doesnt_exist` as a project
  // name will resolve into a (possibly empty) `my_project/package.json` and

View on GitHub (pinned to 8e6d128e4a)