From 6789f5abc7dce445ea52ce3d561ca15d9b213f95 Mon Sep 17 00:00:00 2001 From: audichuang <72485427+audichuang@users.noreply.github.com> Date: Sun, 6 Dec 2026 06:46:34 +0800 Subject: [PATCH] test(editor): destroy every focused-cell editor after each focusedCell test (#8301) Some focusedCell tests never destroyed their focused cell editor, and two cleaned up inside try rather than finally. Focusing a CodeMirror view schedules a 10 ms timer; when the file ended inside it, the timer called requestAnimationFrame on the torn-down jsdom window, failing the Frontend job with an unhandled error. Every cell editor is now destroyed in afterEach. Co-authored-by: audichuang <72485427+audichuang@users.noreply.github.com> --- src/editor/core/focusedCell.test.ts | 70 +++++++++++------------------ 1 file changed, 27 insertions(+), 43 deletions(-) diff --git a/src/editor/core/focusedCell.test.ts b/src/editor/core/focusedCell.test.ts index d713dfbfec..bb04b30a11 100644 --- a/src/editor/core/focusedCell.test.ts +++ b/src/editor/core/focusedCell.test.ts @@ -16,6 +16,7 @@ vi.mock("./fileAttributes", async (original) => ({ })); const ctrl = editorController(); +const cells: EditorView[] = []; let disposeActions: () => void; beforeAll(() => { disposeActions = registerEditorActions(); @@ -30,6 +31,12 @@ afterEach(() => { useEditorStatus.setState({ status: null }); useContextKeys.setState({}, true); _setFocusedCellEditor(null); + // A live view's pending focus timer would run after jsdom is torn down. + for (const view of cells.splice(0)) { + const host = view.dom.parentElement; + view.destroy(); + host?.remove(); + } toggleReadOnlyMock.mockClear(); }); @@ -39,32 +46,23 @@ function focusedCell(doc = "x = 1\n"): { view: EditorView; clear: () => void } { document.body.appendChild(host); // `columnModeField`: what a cell editor carries through its own extension list. const view = new EditorView({ doc, parent: host, extensions: columnModeField }); + cells.push(view); view.focus(); if (!view.hasFocus) throw new Error("the jsdom view did not take focus"); _setFocusedCellEditor(view); return { view, clear: () => _setFocusedCellEditor(null) }; } -function disposeView(cell: { view: EditorView }): void { - cell.view.destroy(); - cell.view.dom.remove(); -} - describe("editor context keys with a focused notebook cell", () => { it("keeps the keys on while a cell editor has focus, with no text editor open", () => { - const cell = focusedCell(); - try { - ctrl.publishContext(); - expect(getContext().textEditorActive).toBe(true); - expect(getContext().editorTextFocus).toBe(true); - // Republishing (tab churn, pane focus) must not drop the keys. - ctrl.publishContext(); - expect(getContext().textEditorActive).toBe(true); - expect(getContext().editorTextFocus).toBe(true); - } finally { - cell.clear(); - disposeView(cell); - } + focusedCell(); + ctrl.publishContext(); + expect(getContext().textEditorActive).toBe(true); + expect(getContext().editorTextFocus).toBe(true); + // Republishing (tab churn, pane focus) must not drop the keys. + ctrl.publishContext(); + expect(getContext().textEditorActive).toBe(true); + expect(getContext().editorTextFocus).toBe(true); }); it("lets a focused cell editor win over the mounted but unfocused text editor", async () => { @@ -75,12 +73,10 @@ describe("editor context keys with a focused notebook cell", () => { try { const textView = slot.mount(host); ctrl.onMounted(slot); - const cell = focusedCell(); + focusedCell(); ctrl.publishContext(); expect(getContext().editorTextFocus).toBe(true); expect(textView.hasFocus).toBe(false); - cell.clear(); - disposeView(cell); } finally { host.remove(); } @@ -88,16 +84,12 @@ describe("editor context keys with a focused notebook cell", () => { it("drops the keys once no cell editor has focus", () => { const cell = focusedCell(); - try { - ctrl.publishContext(); - expect(getContext().textEditorActive).toBe(true); - cell.clear(); - ctrl.publishContext(); - expect(getContext().textEditorActive).toBe(false); - expect(getContext().editorTextFocus).toBe(false); - } finally { - disposeView(cell); - } + ctrl.publishContext(); + expect(getContext().textEditorActive).toBe(true); + cell.clear(); + ctrl.publishContext(); + expect(getContext().textEditorActive).toBe(false); + expect(getContext().editorTextFocus).toBe(false); }); it("keeps a text tab's own keys when no cell editor is focused", async () => { @@ -132,8 +124,6 @@ describe("editor actions in a focused notebook cell", () => { await executeAction("editor.toggleColumnSelection"); expect(isColumnMode(cell.view.state)).toBe(true); expect(isColumnMode(textView.state)).toBe(false); - cell.clear(); - disposeView(cell); } finally { host.remove(); } @@ -183,8 +173,6 @@ describe("editor actions in a focused notebook cell", () => { } finally { uninstall(); disposeStub(); - cell.clear(); - disposeView(cell); } }); }); @@ -211,13 +199,9 @@ describe("Copy Path with a notebook tab", () => { }, }, })); - const cell = focusedCell(); - try { - ctrl.publishContext(); - expect(ctrl.activePath()).toBe("/p/a.ipynb"); - expect(evaluateWhen("textEditorActive", getContext())).toBe(true); - } finally { - cell.clear(); - } + focusedCell(); + ctrl.publishContext(); + expect(ctrl.activePath()).toBe("/p/a.ipynb"); + expect(evaluateWhen("textEditorActive", getContext())).toBe(true); }); });