forked from nextellarlabs/nextellar
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjest.setup.ts
More file actions
56 lines (49 loc) · 1.54 KB
/
Copy pathjest.setup.ts
File metadata and controls
56 lines (49 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// jest.setup.ts
// 1) Add TextEncoder/TextDecoder polyfill FIRST
import { TextEncoder, TextDecoder } from 'util';
Object.assign(global, { TextEncoder, TextDecoder });
// 2) Import Jest globals for ESM
import { jest } from '@jest/globals';
Object.assign(global, { jest });
import '@testing-library/jest-dom';
// 2) Stub BroadcastChannel (MSW uses it internally)
class BroadcastChannel {
name: string;
onmessage: ((event: { data: any }) => void) | null = null;
constructor(name: string) {
this.name = name;
}
postMessage(message: any) {
// no-op
}
close() {
// no-op
}
}
Object.assign(global, { BroadcastChannel });
// 3) Polyfill Web Streams API (required before undici import)
import {
ReadableStream,
WritableStream,
TransformStream,
} from 'web-streams-polyfill';
Object.assign(global, { ReadableStream, WritableStream, TransformStream });
// 4) Setup fetch polyfill for MSW and networked tests
const undici = await import('undici');
Object.assign(globalThis, {
Headers: undici.Headers,
Request: undici.Request,
Response: undici.Response,
fetch: undici.fetch,
});
// 5) MSW Setup (ESM-compatible dynamic import)
try {
const { server } = await import('./src/mocks/server.js');
beforeAll(() => server.listen({ onUnhandledRequest: 'warn' }));
afterEach(() => server.resetHandlers());
afterAll(() => server.close());
} catch (error) {
// MSW server not available, skip (e.g., when msw isn't installed)
// eslint-disable-next-line no-console
console.warn('MSW server not initialized:', error?.message ?? error);
}