Skip to content

Commit 8b3118f

Browse files
authored
fix(core): add editor cleanup in BlockNoteEditor.test.ts to prevent unhandled DOMObserver errors (#2816)
Tests that mount editors were not calling unmount() afterward, leaving ProseMirror's DOMObserver timers active. When prosemirror-view is updated to newer versions (e.g. 1.41.8 via fresh dep resolution), a dangling setTimeout in DOMObserver.flush() fires after jsdom teardown, causing 'document is not defined' ReferenceError. Add afterEach cleanup following the same pattern as transformPasted.test.ts.
1 parent faae3d5 commit 8b3118f

1 file changed

Lines changed: 19 additions & 1 deletion

File tree

packages/core/src/editor/BlockNoteEditor.test.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { expect, it } from "vitest";
1+
import { afterEach, expect, it } from "vitest";
22
import * as Y from "yjs";
33

44
import {
@@ -11,15 +11,27 @@ import { BlocksChanged } from "../api/getBlocksChangedByTransaction.js";
1111
/**
1212
* @vitest-environment jsdom
1313
*/
14+
15+
const editorsToCleanup: BlockNoteEditor<any, any, any>[] = [];
16+
17+
afterEach(() => {
18+
for (const editor of editorsToCleanup) {
19+
editor.unmount();
20+
}
21+
editorsToCleanup.length = 0;
22+
});
23+
1424
it("creates an editor", () => {
1525
const editor = BlockNoteEditor.create();
26+
editorsToCleanup.push(editor);
1627
const posInfo = editor.transact((tr) => getNearestBlockPos(tr.doc, 2));
1728
const info = getBlockInfo(posInfo);
1829
expect(info.blockNoteType).toEqual("paragraph");
1930
});
2031

2132
it("immediately replaces doc", async () => {
2233
const editor = BlockNoteEditor.create();
34+
editorsToCleanup.push(editor);
2335
const blocks = await editor.tryParseMarkdownToBlocks(
2436
"This is a normal text\n\n# And this is a large heading",
2537
);
@@ -70,6 +82,7 @@ it("adds id attribute when requested", async () => {
7082
const editor = BlockNoteEditor.create({
7183
setIdAttribute: true,
7284
});
85+
editorsToCleanup.push(editor);
7386
const blocks = await editor.tryParseMarkdownToBlocks(
7487
"This is a normal text\n\n# And this is a large heading",
7588
);
@@ -81,6 +94,7 @@ it("adds id attribute when requested", async () => {
8194

8295
it("updates block", () => {
8396
const editor = BlockNoteEditor.create();
97+
editorsToCleanup.push(editor);
8498
editor.updateBlock(editor.document[0], {
8599
content: "hello",
86100
});
@@ -89,6 +103,7 @@ it("updates block", () => {
89103
it("block prop types", () => {
90104
// this test checks whether the block props are correctly typed in typescript
91105
const editor = BlockNoteEditor.create();
106+
editorsToCleanup.push(editor);
92107
const block = editor.document[0];
93108
if (block.type === "paragraph") {
94109
// @ts-expect-error
@@ -108,6 +123,7 @@ it("block prop types", () => {
108123

109124
it("onMount and onUnmount", async () => {
110125
const editor = BlockNoteEditor.create();
126+
editorsToCleanup.push(editor);
111127
let mounted = false;
112128
let unmounted = false;
113129
editor.onMount(() => {
@@ -143,6 +159,7 @@ it("sets an initial block id when using Y.js", async () => {
143159
},
144160
},
145161
});
162+
editorsToCleanup.push(editor);
146163

147164
editor.mount(document.createElement("div"));
148165

@@ -193,6 +210,7 @@ it("sets an initial block id when using Y.js", async () => {
193210

194211
it("onBeforeChange", () => {
195212
const editor = BlockNoteEditor.create();
213+
editorsToCleanup.push(editor);
196214
let beforeChangeCalled = false;
197215
let changes: BlocksChanged<any, any, any> = [];
198216
editor.onBeforeChange(({ getChanges }) => {

0 commit comments

Comments
 (0)