-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
jest-shim.ts
53 lines (45 loc) · 1.51 KB
/
jest-shim.ts
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
// Shims originally adapted from https://github.com/nteract/nteract/blob/47f8b038ff129543e42c39395129efc433eb4e90/scripts/test-shim.js
const fetchMod = ((window as any).fetch = require('node-fetch')); // tslint:disable-line
(window as any).Request = fetchMod.Request;
(window as any).Headers = fetchMod.Headers;
(global as any).Image = (window as any).Image;
(global as any).Range = function Range() {
/* no-op */
};
const createContextualFragment = (html: string) => {
const div = document.createElement('div');
div.innerHTML = html;
return div.children[0]; // so hokey it's not even funny
};
(global as any).Range.prototype.createContextualFragment = (html: string) =>
createContextualFragment(html);
window.focus = () => {
/* no-op */
};
// HACK: Polyfill that allows codemirror to render in a JSDOM env.
(window as any).document.createRange = function createRange() {
return {
setEnd: () => {
/* no-op */
},
setStart: () => {
/* no-op */
},
getBoundingClientRect: () => ({ right: 0 }),
getClientRects: (): ClientRect[] => [],
createContextualFragment
};
};
(window as any).document.elementFromPoint = (left: number, top: number) =>
document.body;
process.on('unhandledRejection', (error, promise) => {
console.error('Unhandled promise rejection somewhere in tests');
if (error) {
console.error(error);
const stack = (error as any).stack;
if (stack) {
console.error(stack);
}
}
promise.catch(err => console.error('promise rejected', err));
});