jestjs/jest · error · Error

${EXPECTED_COLOR('expected')} path must not be an empty arra

Error message

${EXPECTED_COLOR('expected')} path must not be an empty array

What it means

Thrown by `toHaveProperty` (matchers.ts:752) when the path is an array but is empty (`length === 0`). An empty path would mean 'the object itself', which is ambiguous and not useful; the matcher rejects it. This guard only applies when the path type is 'array' (string paths are split and may legitimately yield segments).

Source

Thrown at packages/expect/src/matchers.ts:753

    const expectedPathType = getType(expectedPath);

    if (expectedPathType !== 'string' && expectedPathType !== 'array') {
      throw new Error(
        matcherErrorMessage(
          matcherHint(matcherName, undefined, expectedArgument, options),
          `${EXPECTED_COLOR('expected')} path must be a string or array`,
          printWithType('Expected', expectedPath, printExpected),
        ),
      );
    }

    const expectedPathLength =
      typeof expectedPath === 'string'
        ? pathAsArray(expectedPath).length
        : expectedPath.length;

    if (expectedPathType === 'array' && expectedPathLength === 0) {
      throw new Error(
        matcherErrorMessage(
          matcherHint(matcherName, undefined, expectedArgument, options),
          `${EXPECTED_COLOR('expected')} path must not be an empty array`,
          printWithType('Expected', expectedPath, printExpected),
        ),
      );
    }

    const result = getPath(received, expectedPath);
    const {lastTraversedObject, endPropIsDefined, hasEndProp, value} = result;
    const receivedPath = result.traversedPath;
    const hasCompletePath = receivedPath.length === expectedPathLength;
    const receivedValue = hasCompletePath ? result.value : lastTraversedObject;

    const pass =
      hasValue && endPropIsDefined
        ? equals(value, expectedValue, [
            ...this.customTesters,

View on GitHub (pinned to 8e6d128e4a)

Solutions

  1. Provide at least one key in the path array: toHaveProperty(['id']).
  2. If the path is dynamic, validate it is non-empty before the assertion.
  3. If you meant to assert the object itself exists, use toBeDefined()/toEqual(expect.any(Object)) instead.
  4. Filter the path builder to ensure at least the root key remains.

Example fix

// before
expect(obj).toHaveProperty([]);
expect(obj).toHaveProperty(keys.filter(Boolean)); // may be empty

// after
expect(obj).toHaveProperty(['id']);
// guard dynamically
const keys = parts.filter(Boolean);
expect(keys.length).toBeGreaterThan(0);
expect(obj).toHaveProperty(keys);
Defensive patterns

Strategy: validation

Validate before calling

const path = buildPath();
if (Array.isArray(path) && path.length === 0) {
  throw new Error('toHaveProperty path array must not be empty');
}
expect(obj).toHaveProperty(path);

Type guard

const isNonEmptyPath = (v: unknown): v is string | Array<string> =>
  (typeof v === 'string' && v.length > 0) || (Array.isArray(v) && v.length > 0);

Prevention

When it happens

Trigger: Calling `expect(obj).toHaveProperty([])`. Happens when a dynamically-constructed path array ends up empty because all segments were filtered out, or when a spread of optional keys produces no keys.

Common situations: Building a key path array from optional/guessed keys that all resolve to nothing; copy-paste leftover; config-driven tests where the path list is empty for a case.

Related errors


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