{"record":{"id":"e815340ad4f4c590","repo":"jestjs/jest","slug":"you-must-provide-an-object-to-this-tostring","errorCode":null,"errorMessage":"You must provide an object to ${this.toString()}, not '${typeof this.sample}'.","messagePattern":"You must provide an object to (.+?), not '(.+?)'\\.","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"packages/expect/src/asymmetricMatchers.ts","lineNumber":259,"sourceCode":"    return `${this.inverse ? 'Not' : ''}ArrayOf`;\n  }\n\n  override getExpectedType() {\n    return 'array';\n  }\n}\n\nclass ObjectContaining extends AsymmetricMatcher<\n  Record<string | symbol, unknown>\n> {\n  constructor(sample: Record<string | symbol, unknown>, inverse = false) {\n    super(sample, inverse);\n  }\n\n  asymmetricMatch(other: any) {\n    // Ensures that the argument passed to the objectContaining method is an object\n    if (typeof this.sample !== 'object') {\n      throw new TypeError(\n        `You must provide an object to ${this.toString()}, not '${typeof this\n          .sample}'.`,\n      );\n    }\n\n    // Ensures that the argument passed to the expect function is an object\n    // This is necessary to avoid matching of non-object values\n    // Arrays are a special type of object, but having a valid match with a standard object\n    // does not make sense, hence we do a simple array check\n    if (typeof other !== 'object' || Array.isArray(other)) {\n      return false;\n    }\n\n    let result = true;\n\n    const matcherContext = this.getMatcherContext();\n    const objectKeys = getObjectKeys(this.sample);\n","sourceCodeStart":241,"sourceCodeEnd":277,"githubUrl":"https://github.com/jestjs/jest/blob/8e6d128e4a278059ecddecaa97400b04c8ae5fd9/packages/expect/src/asymmetricMatchers.ts#L241-L277","documentation":"objectContaining / objectNotContaining build an ObjectContaining matcher whose asymmetricMatch requires this.sample to be of type 'object' (and rejects arrays as the sample). A non-object sample (string, number, undefined) throws a TypeError at match time. The guard gives a clear message rather than failing inside the key-iteration loop.","triggerScenarios":"Calling `expect.objectContaining(value)` where value is a primitive (string/number), undefined, or an array. The throw occurs when the matcher executes during an assertion like toEqual.","commonSituations":"Passing a string or number where an object schema was intended; passing an array (which is technically typeof 'object') expecting key matching; supplying undefined from an optional builder.","solutions":["Pass a plain object describing the expected subset: `expect.objectContaining({ id: 1 })`.","If you intended to match array contents, use `expect.arrayContaining([...])`.","Confirm the sample is a non-null, non-array object at runtime before constructing the matcher."],"exampleFix":"// before\nexpect(result).toEqual(expect.objectContaining('id')); // string, not object\n\n// after\nexpect(result).toEqual(expect.objectContaining({ id: 1 }));","handlingStrategy":"type-guard","validationCode":"function objectContainingSafe(sample) {\n  if (sample === null || typeof sample !== 'object' || Array.isArray(sample)) {\n    throw new TypeError('objectContaining requires a plain object');\n  }\n  return expect.objectContaining(sample);\n}","typeGuard":"function isPlainObject(v: unknown): v is Record<string | symbol, unknown> {\n  return typeof v === 'object' && v !== null && !Array.isArray(v);\n}\n\nconst m = isPlainObject(sample) ? expect.objectContaining(sample) : expect.anything();","tryCatchPattern":null,"preventionTips":["Pass plain object literals ({...}) to objectContaining.","Do not pass arrays to objectContaining; use arrayContaining instead.","Confirm samples from dynamic sources are non-null objects before use."],"tags":["expect","asymmetric-matcher","typeerror","api-misuse"],"backgroundTag":null,"analyzedSha":"8e6d128e4a278059ecddecaa97400b04c8ae5fd9","analyzedAt":"2026-08-10T18:11:27.960Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}