forked from opensumi/core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jest.setup.node.js
111 lines (98 loc) · 3.06 KB
/
jest.setup.node.js
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
const { JSDOM, ResourceLoader } = require('jsdom');
const resourceLoader = new ResourceLoader({
strictSSL: false,
userAgent: `Mozilla/5.0 (${
process.platform === 'darwin' ? 'Macintosh' : process.platform === 'win32' ? 'Windows' : 'Linux'
}) AppleWebKit/537.36 (KHTML, like Gecko) jsdom/v16.7.0`,
});
const jsdom = new JSDOM('<div id="main"></div>', {
// https://github.com/jsdom/jsdom#basic-options
// 禁用掉 resources: usable, 采用 jsdom 默认策略不加载 subresources
// 避免测试用例加载 external subresource, 如 iconfont 的 css 挂掉
// resources: 'usable',
runScripts: 'dangerously',
url: 'http://localhost/?id=1',
// 保障 `platform.ts` 中 isLinux 等平台信息判断准确性
resources: resourceLoader,
});
global.document = jsdom.window.document;
let text = '';
global.navigator = Object.assign(jsdom.window.navigator, {
clipboard: {
writeText(value) {
text = value;
},
readText() {
return text;
},
},
});
global.Element = jsdom.window.Element;
global.HTMLDivElement = jsdom.window.HTMLDivElement;
global.HTMLSpanElement = jsdom.window.HTMLSpanElement;
global.fetch = jsdom.window.fetch;
global.location = jsdom.window.location;
global.getComputedStyle = jsdom.window.getComputedStyle;
global.window = jsdom.window;
global.DOMParser = jsdom.window.DOMParser;
global.MutationObserver = jsdom.window.MutationObserver;
global.KeyboardEvent = jsdom.window.KeyboardEvent;
global.requestAnimationFrame = (fn) => setTimeout(fn, 16);
global.cancelAnimationFrame = (timer) => {
clearTimeout(timer);
};
jsdom.window.requestAnimationFrame = (fn) => setTimeout(fn, 16);
jsdom.window.cancelAnimationFrame = (timer) => {
clearTimeout(timer);
};
global.document.queryCommandSupported = () => {};
global.document.execCommand = () => {};
global.HTMLElement = jsdom.window.HTMLElement;
global.self = global;
global.TextEncoder = TextEncoder;
global.TextDecoder = TextDecoder;
global.ElectronIpcRenderer = {
send: () => {},
removeListener: () => {},
on: () => {},
};
class MockLocalStorage {
constructor() {
this.store = {};
}
clear() {
this.store = {};
}
getItem(key) {
return this.store[key] || null;
}
setItem(key, value) {
this.store[key] = value.toString();
}
removeItem(key) {
delete this.store[key];
}
}
global.localStorage = new MockLocalStorage();
process.env.IS_JEST_TEST = true;
// https://jestjs.io/docs/manual-mocks#mocking-methods-which-are-not-implemented-in-jsdom
Object.defineProperty(window, 'matchMedia', {
writable: true,
value: jest.fn().mockImplementation((query) => ({
matches: false,
media: query,
onchange: null,
addListener: jest.fn(), // deprecated
removeListener: jest.fn(), // deprecated
addEventListener: jest.fn(),
removeEventListener: jest.fn(),
dispatchEvent: jest.fn(),
})),
});
process.on('unhandledRejection', (error) => {
// eslint-disable-next-line no-console
console.error('unhandledRejection', error);
if (process.env.EXIT_ON_UNHANDLED_REJECTION) {
process.exit(1); // To exit with a 'failure' code
}
});