{"record":{"id":"be0f12117cac2aba","repo":"jestjs/jest","slug":"expected-color-expected-path-must-be-a-strin","errorCode":null,"errorMessage":"${EXPECTED_COLOR('expected')} path must be a string or array","messagePattern":"(.+?) path must be a string or array","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/expect/src/matchers.ts","lineNumber":738,"sourceCode":"      isNot: this.isNot,\n      promise: this.promise,\n      secondArgument: hasValue ? 'value' : '',\n    };\n\n    if (received === null || received === undefined) {\n      throw new Error(\n        matcherErrorMessage(\n          matcherHint(matcherName, undefined, expectedArgument, options),\n          `${RECEIVED_COLOR('received')} value must not be null nor undefined`,\n          printWithType('Received', received, printReceived),\n        ),\n      );\n    }\n\n    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`,","sourceCodeStart":720,"sourceCodeEnd":756,"githubUrl":"https://github.com/jestjs/jest/blob/8e6d128e4a278059ecddecaa97400b04c8ae5fd9/packages/expect/src/matchers.ts#L720-L756","documentation":"Thrown by `toHaveProperty` (matchers.ts:737) when the path argument is neither a string nor an array. The path can be a dotted string ('a.b.c') or an array of keys (['a','b','c']); any other type (number, object, null, undefined) is rejected before traversal via `getType`.","triggerScenarios":"Calling `expect(obj).toHaveProperty(2)` (number), `toHaveProperty(null)`, `toHaveProperty({key: 'x'})` (object), or omitting the path argument so it is undefined.","commonSituations":"Passing a numeric key as the path instead of a string key ('2'); passing an object descriptor by mistake; forgetting the path argument entirely; dynamic path built from non-string parts.","solutions":["Pass the path as a dotted string: toHaveProperty('a.b') or as an array of strings: toHaveProperty(['a','b']).","Coerce numeric keys to strings when building a path: String(index).","If you omitted the path, add it: toHaveProperty('key') is required (unlike toBe).","Build array paths from string parts only: ['users', String(id), 'name']."],"exampleFix":"// before\nexpect(obj).toHaveProperty(2);\nexpect(obj).toHaveProperty();\n\n// after\nexpect(obj).toHaveProperty('items.2');\nexpect(obj).toHaveProperty(['items', '2']);\nexpect(obj).toHaveProperty('id');","handlingStrategy":"type-guard","validationCode":"const path = expectedPath;\nif (typeof path !== 'string' && !Array.isArray(path)) {\n  throw new Error('toHaveProperty path must be a string or array of keys');\n}\nexpect(obj).toHaveProperty(path);","typeGuard":"const isPath = (v: unknown): v is string | Array<string> =>\n  typeof v === 'string' || (Array.isArray(v) && v.every(p => typeof p === 'string'));","tryCatchPattern":null,"preventionTips":["Always pass a path as a dotted string or an array of string keys.","Coerce numeric indices to strings when building array paths.","Never omit the path argument."],"tags":["jest","expect","matcher","type-error","tohaveproperty","path"],"backgroundTag":null,"analyzedSha":"8e6d128e4a278059ecddecaa97400b04c8ae5fd9","analyzedAt":"2026-08-10T18:11:27.960Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}