{"record":{"id":"71fd3635eef2f006","repo":"jestjs/jest","slug":"you-must-provide-an-array-to-this-tostring-n","errorCode":null,"errorMessage":"You must provide an array to ${this.toString()}, not '${typeof this.sample}'.","messagePattern":"You must provide an array to (.+?), not '(.+?)'\\.","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"packages/expect/src/asymmetricMatchers.ts","lineNumber":200,"sourceCode":"  toString() {\n    return 'Anything';\n  }\n\n  // No getExpectedType method, because it matches either null or undefined.\n\n  override toAsymmetricMatcher() {\n    return 'Anything';\n  }\n}\n\nclass ArrayContaining extends AsymmetricMatcher<Array<unknown>> {\n  constructor(sample: Array<unknown>, inverse = false) {\n    super(sample, inverse);\n  }\n\n  asymmetricMatch(other: unknown) {\n    if (!Array.isArray(this.sample)) {\n      throw new TypeError(\n        `You must provide an array to ${this.toString()}, not '${typeof this\n          .sample}'.`,\n      );\n    }\n\n    const matcherContext = this.getMatcherContext();\n    const result =\n      this.sample.length === 0 ||\n      (Array.isArray(other) &&\n        this.sample.every(item =>\n          other.some(another =>\n            equals(item, another, matcherContext.customTesters),\n          ),\n        ));\n\n    return this.inverse ? !result : result;\n  }\n","sourceCodeStart":182,"sourceCodeEnd":218,"githubUrl":"https://github.com/jestjs/jest/blob/8e6d128e4a278059ecddecaa97400b04c8ae5fd9/packages/expect/src/asymmetricMatchers.ts#L182-L218","documentation":"arrayContaining / arrayNotContaining build an ArrayContaining matcher whose asymmetricMatch checks that this.sample is an array before testing containment. Although TypeScript types the sample as Array<unknown>, at runtime (plain JS, or a cast) a non-array sample triggers this TypeError at match time. The check exists so the failure is reported clearly instead of producing a confusing `.every` error.","triggerScenarios":"Calling `expect.arrayContaining(value)` where value is not an array — e.g. an object, a single primitive, or undefined. The error surfaces when the matcher actually runs (during toEqual/toStrictEqual), not at construction.","commonSituations":"Passing an object literal instead of an array; passing a single element instead of a list; mixing up objectContaining and arrayContaining; receiving data from an untyped source whose shape differs from expectation.","solutions":["Pass an array literal: `expect.arrayContaining([item1, item2])`.","If you intended to match an object's subset, use `expect.objectContaining({...})` instead.","Verify the runtime shape of the value passed (log it / assert Array.isArray) before constructing the matcher."],"exampleFix":"// before\nexpect(result).toEqual(expect.arrayContaining({ id: 1 })); // object, not array\n\n// after\nexpect(result).toEqual(expect.arrayContaining([{ id: 1 }]));\n// or for object subset:\nexpect(result).toEqual(expect.objectContaining({ id: 1 }));","handlingStrategy":"type-guard","validationCode":"function arrayContainingSafe(sample) {\n  if (!Array.isArray(sample)) {\n    throw new TypeError('arrayContaining requires an array');\n  }\n  return expect.arrayContaining(sample);\n}","typeGuard":"function isUnknownArray(v: unknown): v is unknown[] {\n  return Array.isArray(v);\n}\n\nconst m = isUnknownArray(sample) ? expect.arrayContaining(sample) : expect.anything();","tryCatchPattern":null,"preventionTips":["Always wrap arrayContaining samples in [...].","Use objectContaining for object subsets, arrayContaining for array contents.","Log/assert the runtime shape of dynamic samples before building matchers."],"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"}