-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathjest.setup.js
More file actions
36 lines (30 loc) · 944 Bytes
/
jest.setup.js
File metadata and controls
36 lines (30 loc) · 944 Bytes
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
import { webcrypto } from 'crypto';
process.env.VERSION = 'mock-version';
const mockGlobalProperty = (property, value) => {
Object.defineProperty(global, property, { writable: true, value });
};
mockGlobalProperty('crypto', webcrypto);
const store = {};
mockGlobalProperty('localStorage', {
getItem: jest.fn((key) => (key in store ? store[key] : null)),
setItem: jest.fn((key, value) => {
store[key] = value.toString();
}),
removeItem: jest.fn((key) => {
delete store[key];
}),
clear: jest.fn(() => {
Object.keys(store).forEach((key) => delete store[key]);
})
});
mockGlobalProperty('matchMedia', (query) => ({
matches: false,
media: query,
onchange: null,
addListener: jest.fn(), // Deprecated
removeListener: jest.fn(), // Deprecated
addEventListener: jest.fn(),
removeEventListener: jest.fn(),
dispatchEvent: jest.fn()
}));
mockGlobalProperty('navigator', { userAgent: 'some-user-agent' });