-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathjest.setup.ts
More file actions
63 lines (57 loc) · 1.91 KB
/
jest.setup.ts
File metadata and controls
63 lines (57 loc) · 1.91 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
63
import { beforeEach } from '@jest/globals';
import { toMatchImageSnapshot } from 'jest-image-snapshot';
expect.extend({ toMatchImageSnapshot });
beforeEach(() => {
const originalConsoleError = console.error;
jest.spyOn(console, 'error').mockImplementation((error, ...args) => {
// When testing the pages' Head component, we get the following error:
// "<html> cannot appear as a child of <div>."
// Because `@testing-library/react` renders the page in a div. We can
// safely ignore this error, since Gatsby will render the Head component
// in the <head> tag.
if (
typeof error === 'string' &&
error.includes('cannot appear as a child of') &&
args[0] === '<html>' &&
args[1] === 'div'
) {
return;
}
// Require tests to be wrapped in act(...).
if (
typeof error === 'string' &&
error.includes('was not wrapped in act(...).')
) {
originalConsoleError(error, ...args);
throw new Error('Test was not wrapped in act(...).');
}
originalConsoleError(error, ...args);
});
const realAddEventListener =
typeof window !== 'undefined' && window?.addEventListener?.bind?.(window);
// Return window.ethereum for EIP6963 when applicable.
Object.assign(globalThis, 'window', {
addEventListener: jest.fn().mockImplementation((type, listener) => {
if (
typeof listener === 'function' &&
type === 'eip6963:announceProvider' &&
window.ethereum
) {
listener(
new CustomEvent('eip6963:announceProvider', {
detail: {
info: {
name: 'MetaMask',
rdns: 'io.metamask',
uuid: '359b317d-0e02-4cea-ade8-7f671fdd5c7e',
},
provider: window.ethereum,
},
}),
);
} else if (realAddEventListener) {
realAddEventListener(type, listener);
}
}),
});
});