{"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/f49721c78e195558b40913977c9230f5b7f559d8/packages/expect/src/asymmetricMatchers.ts#L182-L218","documentation":"ArrayContaining.asymmetricMatch checks that this.sample is an array before iterating; if a non-array was passed to expect.arrayContaining it throws TypeError at match time. The check is deferred to match time (not the constructor) so the matcher object can still be constructed and only fails when actually compared against a received value.","triggerScenarios":"Calling expect.arrayContaining with a non-array sample (a primitive, an object, undefined) and then using that matcher in a comparison such as toEqual, toHaveBeenCalledWith, or objectContaining where it gets evaluated.","commonSituations":"Passing a single value instead of a list: expect.arrayContaining(5) instead of expect.arrayContaining([5]); spreading a non-iterable; a variable that was expected to be an array but is undefined.","solutions":["Wrap the expected items in an array: expect.arrayContaining([5]) not expect.arrayContaining(5).","If the value comes from a variable, ensure it is an array before passing: Array.isArray(x) ? x : [x].","Re-read the matcher name — arrayContaining specifically checks membership within arrays."],"exampleFix":"// before\nexpect(received).toEqual(expect.arrayContaining(5));\n\n// after\nexpect(received).toEqual(expect.arrayContaining([5]));","handlingStrategy":"type-guard","validationCode":"if (!Array.isArray(sample)) {\n  throw new TypeError('expect.arrayContaining needs an array');\n}\nexpect.arrayContaining(sample);","typeGuard":"function isArrayOfUnknown(x: unknown): x is unknown[] {\n  return Array.isArray(x);\n}","tryCatchPattern":"// throws at match time; guard the sample at construction with Array.isArray","preventionTips":["Always pass a literal array: expect.arrayContaining([a, b]).","Defensively wrap singletons: expect.arrayContaining(Array.isArray(x) ? x : [x]).","Remember the check fires on comparison, not on construction."],"tags":["expect","asymmetric-matcher","type-error","array"],"analyzedSha":"f49721c78e195558b40913977c9230f5b7f559d8","analyzedAt":"2026-08-03T20:16:28.571Z","schemaVersion":2}