Skip to content

Commit 68bb719

Browse files
committed
Fix test minor issues
1 parent ce83651 commit 68bb719

3 files changed

Lines changed: 66 additions & 43 deletions

File tree

‎src/Editor.tsx‎

Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ export type EditorHandle = {
2222
focus(): void;
2323
};
2424

25-
// Check if we're in a test environment
26-
const isTestEnvironment =
27-
typeof globalThis !== "undefined" &&
28-
(globalThis as { process?: { env?: { NODE_ENV?: string } } }).process?.env?.NODE_ENV === "test";
25+
// Vitest sets process.env.VITEST; NODE_ENV alone is unreliable in CI (e.g. production).
26+
const processEnv = (globalThis as { process?: { env?: { NODE_ENV?: string; VITEST?: string } } })
27+
.process?.env;
28+
const isTestEnvironment = processEnv?.VITEST === "true" || processEnv?.NODE_ENV === "test";
2929

3030
// Import Monaco Editor components directly
3131
import MonacoEditorComponent from "@monaco-editor/react";
@@ -180,6 +180,10 @@ function Editor({
180180

181181
// Handle Monaco disposal errors gracefully without global monkey patches.
182182
useEffect(() => {
183+
if (typeof window === "undefined") {
184+
return;
185+
}
186+
183187
const handleMonacoError = (event: ErrorEvent) => {
184188
if (shouldSuppressMonacoError(event.error ?? event.message)) {
185189
// Suppress Monaco cleanup errors - they're harmless during layout changes
@@ -462,22 +466,39 @@ function Editor({
462466
);
463467

464468
useEffect(() => {
465-
if (!Array.isArray(variablesInputURLs) || variablesInputURLs.length === 0) setReady(true);
469+
if (!Array.isArray(variablesInputURLs) || variablesInputURLs.length === 0) {
470+
setReady(true);
471+
return;
472+
}
473+
474+
if (ready) {
475+
return;
476+
}
477+
478+
let cancelled = false;
466479
const f = customFetcher || fetch;
467-
if (variablesInputURLs && variablesInputURLs.length > 0 && !ready) {
468-
Promise.all(variablesInputURLs.map(v => f(v)))
469-
.then(res =>
470-
Promise.all(res.map(r => r.json())).then(res => {
471-
const uniqueVars = buildUniqueVariables(res);
472-
setVars(v => [...v, ...uniqueVars]);
473-
setReady(true);
474-
})
475-
)
476-
.catch(() => {
480+
481+
Promise.all(variablesInputURLs.map(v => f(v)))
482+
.then(res =>
483+
Promise.all(res.map(r => r.json())).then(res => {
484+
if (cancelled) {
485+
return;
486+
}
487+
const uniqueVars = buildUniqueVariables(res);
488+
setVars(v => [...v, ...uniqueVars]);
477489
setReady(true);
478-
});
479-
}
480-
}, [variablesInputURLs]);
490+
})
491+
)
492+
.catch(() => {
493+
if (!cancelled) {
494+
setReady(true);
495+
}
496+
});
497+
498+
return () => {
499+
cancelled = true;
500+
};
501+
}, [variablesInputURLs, customFetcher, ready]);
481502

482503
useEffect(() => {
483504
if (isEditorReady) {

‎src/__tests__/setup.ts‎

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -126,31 +126,29 @@ vi.mock("@monaco-editor/react", () => ({
126126
default: vi.fn(({ onMount }) => {
127127
// Simulate component mounting
128128
if (onMount) {
129-
setTimeout(() => {
130-
const mockEditor = {
131-
dispose: vi.fn(),
132-
getValue: vi.fn(() => ""),
133-
setValue: vi.fn(),
134-
focus: vi.fn(),
135-
setPosition: vi.fn(),
136-
revealPosition: vi.fn(),
137-
onDidChangeModelContent: vi.fn(() => ({ dispose: vi.fn() })),
138-
onDidChangeCursorPosition: vi.fn(() => ({ dispose: vi.fn() })),
139-
onDidChangeCursorSelection: vi.fn(() => ({ dispose: vi.fn() })),
140-
addCommand: vi.fn(),
141-
onKeyDown: vi.fn(() => ({ dispose: vi.fn() })),
142-
getModel: vi.fn(() => ({ dispose: vi.fn() }))
143-
};
144-
const mockMonaco = {
145-
editor: {
146-
setModelMarkers: vi.fn(),
147-
MarkerSeverity: { Error: 1, Warning: 2, Info: 3, Hint: 4 }
148-
},
149-
KeyMod: { CtrlCmd: 1, Shift: 2, Alt: 4 },
150-
KeyCode: { KeyS: 1, Enter: 2, KeyZ: 3, KeyY: 4 }
151-
};
152-
onMount(mockEditor, mockMonaco);
153-
}, 0);
129+
const mockEditor = {
130+
dispose: vi.fn(),
131+
getValue: vi.fn(() => ""),
132+
setValue: vi.fn(),
133+
focus: vi.fn(),
134+
setPosition: vi.fn(),
135+
revealPosition: vi.fn(),
136+
onDidChangeModelContent: vi.fn(() => ({ dispose: vi.fn() })),
137+
onDidChangeCursorPosition: vi.fn(() => ({ dispose: vi.fn() })),
138+
onDidChangeCursorSelection: vi.fn(() => ({ dispose: vi.fn() })),
139+
addCommand: vi.fn(),
140+
onKeyDown: vi.fn(() => ({ dispose: vi.fn() })),
141+
getModel: vi.fn(() => ({ dispose: vi.fn() }))
142+
};
143+
const mockMonaco = {
144+
editor: {
145+
setModelMarkers: vi.fn(),
146+
MarkerSeverity: { Error: 1, Warning: 2, Info: 3, Hint: 4 }
147+
},
148+
KeyMod: { CtrlCmd: 1, Shift: 2, Alt: 4 },
149+
KeyCode: { KeyS: 1, Enter: 2, KeyZ: 3, KeyY: 4 }
150+
};
151+
onMount(mockEditor, mockMonaco);
154152
}
155153
return null;
156154
}),

‎src/monaco-patch.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ import { shouldSuppressMonacoError } from "./utils/monaco-errors";
33
let isPatchApplied = false;
44

55
export const applyMonacoPatch = (): (() => void) => {
6+
if (typeof window === "undefined") {
7+
return () => undefined;
8+
}
9+
610
if (isPatchApplied) {
711
return () => undefined;
812
}

0 commit comments

Comments
 (0)