{"record":{"id":"7b753474ee9eea3a","repo":"jestjs/jest","slug":"expected-color-expected-path-must-not-be-an","errorCode":null,"errorMessage":"${EXPECTED_COLOR('expected')} path must not be an empty array","messagePattern":"(.+?) path must not be an empty array","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/expect/src/matchers.ts","lineNumber":753,"sourceCode":"    const expectedPathType = getType(expectedPath);\n\n    if (expectedPathType !== 'string' && expectedPathType !== 'array') {\n      throw new Error(\n        matcherErrorMessage(\n          matcherHint(matcherName, undefined, expectedArgument, options),\n          `${EXPECTED_COLOR('expected')} path must be a string or array`,\n          printWithType('Expected', expectedPath, printExpected),\n        ),\n      );\n    }\n\n    const expectedPathLength =\n      typeof expectedPath === 'string'\n        ? pathAsArray(expectedPath).length\n        : expectedPath.length;\n\n    if (expectedPathType === 'array' && expectedPathLength === 0) {\n      throw new Error(\n        matcherErrorMessage(\n          matcherHint(matcherName, undefined, expectedArgument, options),\n          `${EXPECTED_COLOR('expected')} path must not be an empty array`,\n          printWithType('Expected', expectedPath, printExpected),\n        ),\n      );\n    }\n\n    const result = getPath(received, expectedPath);\n    const {lastTraversedObject, endPropIsDefined, hasEndProp, value} = result;\n    const receivedPath = result.traversedPath;\n    const hasCompletePath = receivedPath.length === expectedPathLength;\n    const receivedValue = hasCompletePath ? result.value : lastTraversedObject;\n\n    const pass =\n      hasValue && endPropIsDefined\n        ? equals(value, expectedValue, [\n            ...this.customTesters,","sourceCodeStart":735,"sourceCodeEnd":771,"githubUrl":"https://github.com/jestjs/jest/blob/8e6d128e4a278059ecddecaa97400b04c8ae5fd9/packages/expect/src/matchers.ts#L735-L771","documentation":"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).","triggerScenarios":"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.","commonSituations":"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.","solutions":["Provide at least one key in the path array: toHaveProperty(['id']).","If the path is dynamic, validate it is non-empty before the assertion.","If you meant to assert the object itself exists, use toBeDefined()/toEqual(expect.any(Object)) instead.","Filter the path builder to ensure at least the root key remains."],"exampleFix":"// before\nexpect(obj).toHaveProperty([]);\nexpect(obj).toHaveProperty(keys.filter(Boolean)); // may be empty\n\n// after\nexpect(obj).toHaveProperty(['id']);\n// guard dynamically\nconst keys = parts.filter(Boolean);\nexpect(keys.length).toBeGreaterThan(0);\nexpect(obj).toHaveProperty(keys);","handlingStrategy":"validation","validationCode":"const path = buildPath();\nif (Array.isArray(path) && path.length === 0) {\n  throw new Error('toHaveProperty path array must not be empty');\n}\nexpect(obj).toHaveProperty(path);","typeGuard":"const isNonEmptyPath = (v: unknown): v is string | Array<string> =>\n  (typeof v === 'string' && v.length > 0) || (Array.isArray(v) && v.length > 0);","tryCatchPattern":null,"preventionTips":["When building paths dynamically, validate length > 0 before asserting.","Use toBeDefined() if you meant to assert the object itself exists.","Keep at least the root key in dynamic path builders."],"tags":["jest","expect","matcher","validation","tohaveproperty","path","array"],"backgroundTag":null,"analyzedSha":"8e6d128e4a278059ecddecaa97400b04c8ae5fd9","analyzedAt":"2026-08-10T18:11:27.960Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}