-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathjest.setup.browser.ts
More file actions
63 lines (54 loc) · 1.52 KB
/
jest.setup.browser.ts
File metadata and controls
63 lines (54 loc) · 1.52 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
57
58
59
60
61
62
/**
* Browser Environment Test Setup
*
* Sets up browser-like globals and polyfills for testing
*/
// jsdom provides fetch natively in newer versions
// No need to import node-fetch
// Suppress expected console errors during tests
const originalError = console.error;
const originalWarn = console.warn;
beforeAll(() => {
console.error = (...args: any[]) => {
// Suppress specific expected errors
const message = args[0]?.toString() || '';
if (
message.includes('Not implemented: HTMLFormElement.prototype.submit') ||
message.includes('Not implemented: navigation')
) {
return;
}
originalError.call(console, ...args);
};
console.warn = (...args: any[]) => {
// Suppress specific expected warnings
const message = args[0]?.toString() || '';
if (message.includes('jsdom')) {
return;
}
originalWarn.call(console, ...args);
};
});
afterAll(() => {
console.error = originalError;
console.warn = originalWarn;
});
// Add custom matchers for browser testing if needed
expect.extend({
toBeBrowserSafe(received: any) {
const forbidden = ['fs', 'path', 'crypto', 'Buffer', 'process'];
const receivedString = JSON.stringify(received);
for (const api of forbidden) {
if (receivedString.includes(api)) {
return {
pass: false,
message: () => `Expected code to be browser-safe, but found Node.js API: ${api}`,
};
}
}
return {
pass: true,
message: () => 'Code is browser-safe',
};
},
});