chore: mock fetch and eventsource in tests

This commit is contained in:
Twirre Meulenbelt
2026-01-27 20:28:07 +01:00
parent b2ec0cbe85
commit 3984424113

View File

@@ -1,2 +1,46 @@
// Adds jest-dom matchers for React testing library // Adds jest-dom matchers for React testing library
import '@testing-library/jest-dom'; import '@testing-library/jest-dom';
// Minimal browser API mocks for the test environment.
// Fetch
if (!globalThis.fetch) {
globalThis.fetch = jest.fn(async () => ({
ok: true,
status: 200,
json: async () => [],
text: async () => '',
})) as unknown as typeof fetch;
}
// EventSource
if (!globalThis.EventSource) {
class MockEventSource {
url: string;
readyState = 1;
onmessage: ((event: MessageEvent) => void) | null = null;
onerror: ((event: Event) => void) | null = null;
onopen: ((event: Event) => void) | null = null;
constructor(url: string) {
this.url = url;
}
close() {
this.readyState = 2;
}
addEventListener(type: string, listener: (event: MessageEvent) => void) {
if (type === 'message') {
this.onmessage = listener;
}
}
removeEventListener(type: string, listener: (event: MessageEvent) => void) {
if (type === 'message' && this.onmessage === listener) {
this.onmessage = null;
}
}
}
globalThis.EventSource = MockEventSource as unknown as typeof EventSource;
}