Files
gods-eye-view/src/testSupport/domAssert.mjs
T
Sameh Khamis 3a2acf9b0f test: compare DOM nodes by identity, never through assert.equal
A failed assert.equal or deepEqual prints both operands, and a happy-dom
node reaches the whole page and window: printing one exhausted memory and
took the machine down. assertSameNode() compares identity and fails with
a short description of each node; every node comparison in the happy-dom
tests uses it.
2026-10-30 20:38:29 -07:00

20 lines
627 B
JavaScript

import assert from 'node:assert/strict';
const describe = (node) =>
node?.nodeType === 1
? `<${node.tagName.toLowerCase()}${node.id ? `#${node.id}` : ''}>`
: String(node);
/**
* Assert that two DOM nodes (or a node and null) are the same. Never hand
* happy-dom nodes to assert.equal or deepEqual: a failure prints both, and a
* node reaches the whole page and window, which runs the process out of
* memory.
*/
export function assertSameNode(actual, expected, message = '') {
assert.ok(
actual === expected,
`${message ? `${message}: ` : ''}${describe(actual)} is not ${describe(expected)}`,
);
}