Skip to content

AI-native knowledge base & Evernote/Obsidian alternative with instant sync & native MCP. Free on Cloudflare or self-hosted via Docker.

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

tianma-if3fd7c41922d8
diff --git a/apps/web/src/lib/editor-heading-anchor.test.mjs b/apps/web/src/lib/editor-heading-anchor.test.mjs
index f8a38ef8..3ca8818c 100644
--- a/apps/web/src/lib/editor-heading-anchor.test.mjs
+++ b/apps/web/src/lib/editor-heading-anchor.test.mjs
@@ -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);