{"record":{"id":"b7269c9df3668d2b","repo":"vitest-dev/vitest","slug":"tocontain-expected-a-dom-node-as-the-argument-b","errorCode":null,"errorMessage":"toContain() expected a DOM node as the argument, but got ${typeof item}","messagePattern":"toContain\\(\\) expected a DOM node as the argument, but got (.+?)","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"packages/expect/src/jest-expect.ts","lineNumber":251,"sourceCode":"      typeof expected === 'string'\n        ? actual.includes(expected)\n        : actual.match(expected),\n      `expected #{this} to match #{exp}`,\n      `expected #{this} not to match #{exp}`,\n      expected,\n      actual,\n    )\n  })\n  def('toContain', function (item) {\n    const actual = this._obj as\n      | Iterable<unknown>\n      | string\n      | Node\n      | DOMTokenList\n\n    if (typeof Node !== 'undefined' && actual instanceof Node) {\n      if (!(item instanceof Node)) {\n        throw new TypeError(\n          `toContain() expected a DOM node as the argument, but got ${typeof item}`,\n        )\n      }\n\n      return this.assert(\n        actual.contains(item),\n        'expected #{this} to contain element #{exp}',\n        'expected #{this} not to contain element #{exp}',\n        item,\n        actual,\n      )\n    }\n\n    if (typeof DOMTokenList !== 'undefined' && actual instanceof DOMTokenList) {\n      assertTypes(item, 'class name', ['string'])\n      const isNot = utils.flag(this, 'negate') as boolean\n      const expectedClassList = isNot\n        ? actual.value.replace(item, '').trim()","sourceCodeStart":233,"sourceCodeEnd":269,"githubUrl":"https://github.com/vitest-dev/vitest/blob/1fa9837ec26533512fdcad8baebf249771bd340a/packages/expect/src/jest-expect.ts#L233-L269","documentation":"When the actual value is a DOM Node, toContain switches into DOM mode and requires the argument to also be a Node (it calls actual.contains(item)). Passing anything else (string, number, element-like object) throws a TypeError.","triggerScenarios":"expect(node).toContain(item) where actual instanceof Node is true but item is not a Node (e.g. a string of text or a number).","commonSituations":"Passing a text string expecting substring containment of textContent (use toContain on node.textContent or a text-based matcher); passing a cheerio/jsdom-lite wrapper that is not a real Node; passing an element from a different document.","solutions":["Pass an actual DOM Node: expect(parentNode).toContain(childNode).","For text content, assert on the string: expect(node.textContent).toContain('hello').","If the argument comes from another library, materialize it into a real Node first."],"exampleFix":"// before\nexpect(container).toContain('Save')\n// after\nexpect(container).toContain(saveButton) // a real DOM Node\n// or assert on text\nexpect(container.textContent).toContain('Save')","handlingStrategy":"type-guard","validationCode":"function assertNodeForContain(actual, item) {\n  if (typeof Node !== 'undefined' && actual instanceof Node && !(item instanceof Node)) {\n    throw new TypeError('toContain() on a Node needs a Node argument; for text use node.textContent')\n  }\n}","typeGuard":"function isDomNode(v): v is Node {\n  return typeof Node !== 'undefined' && v instanceof Node\n}","tryCatchPattern":null,"preventionTips":["When asserting DOM containment, pass a real Node as the expected item.","For text checks, assert on node.textContent with toContain instead.","Materialize library-wrapped elements into real Nodes before toContain."],"tags":["expect","matcher","dom","type-error"],"backgroundTag":null,"analyzedSha":"1fa9837ec26533512fdcad8baebf249771bd340a","analyzedAt":"2026-08-11T16:11:39.638Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}