test(web): restore DOM globals after heading anchor tests

This commit is contained in:
tianma-if committed 2026-10-21 18:55:24 +08:00
1 parent 3261010cd5
commit 3fd7c41922
1 file changed
+17 -1
@@ -1,4 +1,4 @@
import { beforeAll, describe, expect, test } from "bun:test";
import { afterAll, beforeAll, describe, expect, test } from "bun:test";
import { parseHTML } from "linkedom";
import {
activateHeadingAnchorClick,
@@ -7,8 +7,17 @@ import {
parseHeadingAnchorHref,
} from "./editor-heading-anchor.ts";
// Bun runs later editor tests in the same worker. Install linkedom as `window`
// for `instanceof` checks, then put the previous globals back. Leaving only
// `document` replaced makes TipTap call a `window` with no addEventListener.
const domKeys = ["window", "document", "HTMLElement", "Element", "Node", "HTMLDetailsElement"];
const originalDom = new Map(
domKeys.map((key) => [key, Object.getOwnPropertyDescriptor(globalThis, key)]),
);
beforeAll(() => {
const window = parseHTML("<!DOCTYPE html><html><body></body></html>");
globalThis.window = window;
globalThis.document = window.document;
globalThis.HTMLElement = window.HTMLElement;
globalThis.Element = window.Element;
@@ -16,6 +25,13 @@ beforeAll(() => {
globalThis.HTMLDetailsElement = window.HTMLDetailsElement;
});
afterAll(() => {
for (const [key, descriptor] of originalDom) {
if (descriptor) Object.defineProperty(globalThis, key, descriptor);
else delete globalThis[key];
}
});
const docWith = (entries) => ({
descendants(callback) {
for (const entry of entries) callback(entry.node, entry.pos);