test: stub Range layout methods once in the test setup (#53)

Root cause: jsdom has no layout and CodeMirror measures text ranges in an animation frame, sometimes after a test finished; test files that did not stub Range.getClientRects (e.g. search/codePreview) threw an unhandled TypeError that failed whole CI runs at random (seen on #41).
Fix: stub Range.getClientRects / getBoundingClientRect once in src/test/setup.ts; drop the six per-file copies.
Verified: vitest on editor/lsp, editor/refactor, templates, search (55 files, 458 tests, no unhandled errors).

Claude-Session: https://claude.ai/code/session_01VUYsdXA2nkF5v6zsC93dpn
This commit is contained in:
l0ng-ai
2026-10-27 10:38:11 +08:00
committed by GitHub
parent 843127f4a3
commit 5670c2e8dd
7 changed files with 4 additions and 24 deletions
-4
View File
@@ -239,10 +239,6 @@ describe("lookup commit characters", () => {
});
describe("explicit completion", () => {
// jsdom has no layout; CodeMirror measures text when the tooltip is positioned.
Range.prototype.getClientRects ??= () => [] as unknown as DOMRectList;
Range.prototype.getBoundingClientRect ??= () => new DOMRect();
function editor(labels: string[]) {
const source: CompletionSource = (ctx) => {
const word = ctx.matchBefore(/\w*/);
-4
View File
@@ -16,10 +16,6 @@ const { signatureExtension, acceptsParameter, argumentSpans, argumentStop, param
);
const { lspDoc } = await import("./lsp");
// jsdom has no layout; CodeMirror measures text when the tooltip is positioned.
Range.prototype.getClientRects ??= () => [] as unknown as DOMRectList;
Range.prototype.getBoundingClientRect ??= () => new DOMRect();
const views: EditorView[] = [];
afterEach(() => {
views.splice(0).forEach((v) => v.destroy());
-3
View File
@@ -35,9 +35,6 @@ const { startRename } = await import("./rename");
const { lspDoc } = await import("./lsp");
const { useLspPopup, closePopup } = await import("./popupStore");
Range.prototype.getClientRects ??= () => [] as unknown as DOMRectList;
Range.prototype.getBoundingClientRect ??= () => new DOMRect();
const r = (line: number, col: number, endCol: number): Range => ({ start: { line, col }, end: { line, col: endCol } });
const node = (kind: string, range: Range, field: string | null = null): SyntaxNode => ({
kind,
-4
View File
@@ -120,10 +120,6 @@ const { clearLevelCache } = await import("./projectLevel");
// --- editor ---------------------------------------------------------------------
// jsdom has no layout; popups anchor at caret coordinates.
Range.prototype.getClientRects ??= () => [] as unknown as DOMRectList;
Range.prototype.getBoundingClientRect ??= () => new DOMRect();
let view: EditorView | null = null;
function editor(doc: string, language: string, path: string, sel: { anchor: number; head?: number }) {
-4
View File
@@ -55,10 +55,6 @@ const { lspDoc } = await import("@/editor/lsp/lsp");
const { inplaceRenameExtension, inplaceSession } = await import("@/editor/lsp/inplaceRename");
const { temporaryHighlights } = await import("@/editor/lsp/tempHighlight");
// jsdom has no layout; popups anchor at the caret.
Range.prototype.getClientRects ??= () => [] as unknown as DOMRectList;
Range.prototype.getBoundingClientRect ??= () => new DOMRect();
const action = (title: string, kind: string, disabledReason: string | null = null): CodeAction => ({
title,
kind,
@@ -17,11 +17,6 @@ import { closeChooser, useChooser } from "../popup/store";
const MODES: Record<string, string> = { typescript: "TypeScript", rust: "Rust", python: "Python", java: "Java" };
// jsdom has no layout; CodeMirror measures ranges when scrolling the caret into view.
const rects = () => Object.assign([], { item: () => null }) as unknown as DOMRectList;
Range.prototype.getClientRects ??= rects;
Range.prototype.getBoundingClientRect ??= () => new DOMRect();
let view: EditorView | null = null;
afterEach(() => {
view?.destroy();
+4
View File
@@ -39,5 +39,9 @@ if (typeof window !== "undefined") {
}) as MediaQueryList;
}
Element.prototype.scrollIntoView ??= function () {};
// No layout either: CodeMirror measures text ranges when it positions the caret,
// tooltips and line heights, sometimes in an animation frame after a test ends.
Range.prototype.getClientRects ??= () => Object.assign([], { item: () => null }) as unknown as DOMRectList;
Range.prototype.getBoundingClientRect ??= () => new DOMRect();
Element.prototype.scrollTo ??= function () {} as Element["scrollTo"];
}