mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-06-19 13:21:46 +00:00
f594cbc21b
In jsdom, source modules resolve bare 'URL' identifiers through window.URL (the jsdom window object), not through globalThis.URL (Node's URL class). On GitHub Actions these are distinct objects, so all prior attempts (Object.defineProperty, direct assignment, vi.stubGlobal) were patching the wrong object and failing silently. Changes: - setup.ts: Object.defineProperty targets window.URL so the vi.fn mock is visible to authUrl.ts at call time - authUrl.test.ts: drop vi.stubGlobal approach; add vi.clearAllMocks() to reset accumulated call counts on the setup.ts vi.fn between tests; fix vi.spyOn target to window.URL in test 012
70 lines
2.6 KiB
TypeScript
70 lines
2.6 KiB
TypeScript
import '@testing-library/jest-dom/vitest';
|
|
import { cleanup } from '@testing-library/react';
|
|
import { afterAll, afterEach, beforeAll, vi } from 'vitest';
|
|
import { server } from './helpers/msw/server';
|
|
|
|
// Mock the websocket module so stores don't try to open real connections
|
|
vi.mock('../src/api/websocket', () => ({
|
|
connect: vi.fn(),
|
|
disconnect: vi.fn(),
|
|
getSocketId: vi.fn(() => null),
|
|
setRefetchCallback: vi.fn(),
|
|
}));
|
|
|
|
// MSW lifecycle
|
|
beforeAll(() => server.listen({ onUnhandledRequest: 'warn' }));
|
|
afterEach(() => {
|
|
server.resetHandlers();
|
|
cleanup();
|
|
localStorage.clear();
|
|
sessionStorage.clear();
|
|
});
|
|
afterAll(() => server.close());
|
|
|
|
// ── jsdom stubs ────────────────────────────────────────────────────────────────
|
|
|
|
// window.matchMedia — used by dark mode / responsive components
|
|
Object.defineProperty(window, 'matchMedia', {
|
|
writable: true,
|
|
value: vi.fn().mockImplementation((query: string) => ({
|
|
matches: false,
|
|
media: query,
|
|
onchange: null,
|
|
addListener: vi.fn(),
|
|
removeListener: vi.fn(),
|
|
addEventListener: vi.fn(),
|
|
removeEventListener: vi.fn(),
|
|
dispatchEvent: vi.fn(),
|
|
})),
|
|
});
|
|
|
|
// IntersectionObserver — used by lazy loading
|
|
// Must use a class or regular function (not arrow function) so 'new IntersectionObserver()' works
|
|
class _MockIntersectionObserver {
|
|
observe = vi.fn()
|
|
unobserve = vi.fn()
|
|
disconnect = vi.fn()
|
|
root = null
|
|
rootMargin = ''
|
|
thresholds: ReadonlyArray<number> = []
|
|
takeRecords = vi.fn(() => [])
|
|
constructor(_callback: IntersectionObserverCallback, _options?: IntersectionObserverInit) {}
|
|
}
|
|
globalThis.IntersectionObserver = _MockIntersectionObserver as unknown as typeof IntersectionObserver;
|
|
|
|
// ResizeObserver — used by resizable panels
|
|
globalThis.ResizeObserver = vi.fn().mockImplementation(() => ({
|
|
observe: vi.fn(),
|
|
unobserve: vi.fn(),
|
|
disconnect: vi.fn(),
|
|
})) as unknown as typeof ResizeObserver;
|
|
|
|
// URL.createObjectURL / revokeObjectURL — in jsdom, modules access globals
|
|
// through window.URL (not globalThis.URL — these are different objects on CI).
|
|
// Targeting window.URL is required so the mock is visible to source modules.
|
|
Object.defineProperty(window.URL, 'createObjectURL', { writable: true, configurable: true, value: vi.fn(() => 'blob:mock') });
|
|
Object.defineProperty(window.URL, 'revokeObjectURL', { writable: true, configurable: true, value: vi.fn() });
|
|
|
|
// Element.prototype.scrollIntoView — jsdom doesn't implement it
|
|
Element.prototype.scrollIntoView = vi.fn();
|