{"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/d568f8ce3739b532d5bf2c1ee1e45e8a8a473d09/packages/expect/src/jest-expect.ts#L233-L269","documentation":"TypeError thrown by toContain when the actual value is a DOM Node but the argument passed in is not a Node. When asserting containment against a DOM node, both sides must be nodes (Node.contains semantics); any other argument type is rejected up front.","triggerScenarios":"expect(domElement).toContain('some-string'); expect(parentNode).toContain(42); expect(node).toContain({}). Any toContain call where actual instanceof Node is true but the item argument is not instanceof Node.","commonSituations":"Confusing the DOM-node form of toContain with the string/array form. A developer passes a class name string or a selector to toContain against an element, when they meant classList.contains (the DOMTokenList branch) or querySelector.","solutions":["To check a class, assert against the classList: expect(el.classList).toContain('active').","To check text, assert against the text string: expect(el.textContent).toContain('hello').","To check a child node, pass the actual child node reference: expect(parent).toContain(childNode)."],"exampleFix":"// before\nexpect(button).toContain('btn-primary');\n// after\nexpect(button.classList).toContain('btn-primary');","handlingStrategy":"type-guard","validationCode":"if (actual instanceof Node && !(item instanceof Node)) throw new TypeError('toContain on a Node requires a Node argument');","typeGuard":"const isNode = (v: unknown): v is Node => typeof Node !== 'undefined' && v instanceof Node;","tryCatchPattern":null,"preventionTips":["For class checks use expect(el.classList).toContain(name).","For text checks use expect(el.textContent).toContain(substring)."],"tags":["typeerror","tocontain","dom","node","type-mismatch"],"analyzedSha":"d568f8ce3739b532d5bf2c1ee1e45e8a8a473d09","analyzedAt":"2026-08-03T20:23:56.861Z","schemaVersion":2}