continue-custom/gui/src/util/test/setupTests.ts
Patrick Erichsen f39d3527fd linting
2025-07-13 21:21:34 -07:00

72 lines
1.6 KiB
TypeScript

import "@testing-library/jest-dom";
afterEach(() => {
vi.clearAllMocks();
});
afterAll(() => {
vi.resetAllMocks();
});
// Suppress uncaught ProseMirror errors in test environment
window.addEventListener("error", (event) => {
if (
event.error?.message?.includes("getClientRects") ||
event.error?.message?.includes("prosemirror")
) {
event.preventDefault();
return false;
}
});
window.addEventListener("unhandledrejection", (event) => {
if (
event.reason?.message?.includes("getClientRects") ||
event.reason?.message?.includes("prosemirror")
) {
event.preventDefault();
return false;
}
});
// https://github.com/vitest-dev/vitest/issues/821
Object.defineProperty(window, "matchMedia", {
writable: true,
value: vi.fn().mockImplementation((query) => ({
matches: false,
media: query,
onchange: null,
addListener: vi.fn(), // deprecated
removeListener: vi.fn(), // deprecated
addEventListener: vi.fn(),
removeEventListener: vi.fn(),
dispatchEvent: vi.fn(),
})),
});
// Mock getBoundingClientRect and getClientRects for ProseMirror
Object.defineProperty(Element.prototype, "getClientRects", {
value: vi.fn(() => ({
length: 1,
0: { top: 0, bottom: 20, left: 0, right: 100, width: 100, height: 20 },
item: () => ({
top: 0,
bottom: 20,
left: 0,
right: 100,
width: 100,
height: 20,
}),
})),
});
Object.defineProperty(Element.prototype, "getBoundingClientRect", {
value: vi.fn(() => ({
top: 0,
bottom: 20,
left: 0,
right: 100,
width: 100,
height: 20,
})),
});