vitest-dev/vitest · error · TypeError
toContain() expected a DOM node as the argument, but got ${t
Error message
toContain() expected a DOM node as the argument, but got ${typeof item} What it means
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.
Source
Thrown at packages/expect/src/jest-expect.ts:251
typeof expected === 'string'
? actual.includes(expected)
: actual.match(expected),
`expected #{this} to match #{exp}`,
`expected #{this} not to match #{exp}`,
expected,
actual,
)
})
def('toContain', function (item) {
const actual = this._obj as
| Iterable<unknown>
| string
| Node
| DOMTokenList
if (typeof Node !== 'undefined' && actual instanceof Node) {
if (!(item instanceof Node)) {
throw new TypeError(
`toContain() expected a DOM node as the argument, but got ${typeof item}`,
)
}
return this.assert(
actual.contains(item),
'expected #{this} to contain element #{exp}',
'expected #{this} not to contain element #{exp}',
item,
actual,
)
}
if (typeof DOMTokenList !== 'undefined' && actual instanceof DOMTokenList) {
assertTypes(item, 'class name', ['string'])
const isNot = utils.flag(this, 'negate') as boolean
const expectedClassList = isNot
? actual.value.replace(item, '').trim()View on GitHub (pinned to d568f8ce37)
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).
Example fix
// before
expect(button).toContain('btn-primary');
// after
expect(button.classList).toContain('btn-primary'); Defensive patterns
Strategy: type-guard
Validate before calling
if (actual instanceof Node && !(item instanceof Node)) throw new TypeError('toContain on a Node requires a Node argument'); Type guard
const isNode = (v: unknown): v is Node => typeof Node !== 'undefined' && v instanceof Node;
Prevention
- For class checks use expect(el.classList).toContain(name).
- For text checks use expect(el.textContent).toContain(substring).
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- .toMatch() expects to receive a string, but got ${typeof act
- ${utils.inspect(assertion._obj)} is not a spy or a call to a
- ${utils.inspect(resultSpy)} is not a spy or a call to a spy
- You must provide a Promise to expect() when using .resolves,
- You must provide a Promise to expect() when using .rejects,
AI-assisted analysis of vitest-dev/vitest@d568f8ce37 (2026-08-03).
Data as JSON: /data/errors/b7269c9df3668d2b.json.
Report an issue: GitHub.