{"id":"43623cfe1b7c2bb1","repo":"jestjs/jest","slug":"expected-path-must-not-be-an-empty-array","errorCode":null,"errorMessage":"expected path must not be an empty array","messagePattern":"expected path must not be an empty array","errorType":"validation","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/f49721c78e195558b40913977c9230f5b7f559d8/packages/expect/src/matchers.ts#L735-L771","documentation":"Thrown by `toHaveProperty` (matchers.ts:711) when the expected path is an array but its length is zero. An empty path array has no keys to traverse and would always trivially match the root object, which is never what the developer intended. Jest computes `expectedPathLength` (via `pathAsArray(...).length` for strings or `.length` for arrays) and rejects the `array` type with zero length specifically.","triggerScenarios":"Calling `expect(obj).toHaveProperty([])` literally, or `.toHaveProperty(keys)` where `keys` is an array that was dynamically built and ended up empty (e.g. split a string with no delimiter, or a filter that removed all entries).","commonSituations":"Building a path array from user input or config where the input was blank; chaining operations that produced `[]` instead of `['']`; refactor that wrapped a path in an array conditionally and hit the empty case.","solutions":["Provide at least one key in the array: `.toHaveProperty(['key'])`.","If the path is dynamic, fall back to a meaningful default or skip the assertion when empty.","Inspect how the path array is constructed — log it before the assertion.","If you meant to assert on the root, use a different assertion like `expect(obj).toEqual(expect.any(Object))`."],"exampleFix":"// before\nconst keys = path.split('.').filter(Boolean); // [] for empty path\nexpect(obj).toHaveProperty(keys);\n\n// after\nconst keys = path.split('.').filter(Boolean);\nif (keys.length > 0) expect(obj).toHaveProperty(keys);","handlingStrategy":"validation","validationCode":"if (Array.isArray(path) && path.length === 0) {\n  throw new Error('path array must not be empty');\n}\nexpect(obj).toHaveProperty(path);","typeGuard":"const isNonEmptyPath = (v: unknown): boolean =>\n  typeof v === 'string' ? v.length > 0 : Array.isArray(v) && v.length > 0;","tryCatchPattern":"try {\n  expect(obj).toHaveProperty(path);\n} catch (e) {\n  if (e instanceof Error && /must not be an empty array/.test(e.message)) {\n    console.error('path was an empty array; supply at least one key');\n  }\n  throw e;\n}","preventionTips":["Filter paths but ensure at least one key remains, or skip the assertion.","Validate dynamically-built path arrays before passing them in.","Use a string path when only a single key is needed."],"tags":["jest","expect","validation","assertion","object-path","array"],"analyzedSha":"f49721c78e195558b40913977c9230f5b7f559d8","analyzedAt":"2026-08-03T20:16:28.571Z","schemaVersion":2}