-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetupJestEnv.js
executable file
·73 lines (60 loc) · 1.66 KB
/
setupJestEnv.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
/* eslint-disable */
const JSDom = require('jsdom');
const JestUtil = require('jest-util');
const mock = require('jest-mock');
const { FakeTimers, installCommonGlobals } = JestUtil;
class JSDOMEnvironment {
constructor(config, options = {}) {
this.dom = new JSDom.JSDOM(
'<!doctype html><html><body><div id="root"></div></body><script></script></html>',
Object.assign(
{
pretendToBeVisual: true,
runScripts: 'dangerously',
url: config.testURL,
virtualConsole: new JSDom.VirtualConsole().sendTo(options.console || console),
},
config.testEnvironmentOptions,
),
);
this.global = this.dom.window.document.defaultView;
const global = this.global;
// Node's error-message stack size is limited at 10, but it's pretty useful
// to see more than that when a test fails.
this.global.Error.stackTraceLimit = 100;
installCommonGlobals(global, config.globals);
this.moduleMocker = new mock.ModuleMocker(global);
const timerConfig = {
idToRef: id => id,
refToId: ref => ref,
};
this.fakeTimers = new FakeTimers({
config,
global,
moduleMocker: this.moduleMocker,
timerConfig,
});
}
setup() {
return Promise.resolve();
}
teardown() {
if (this.fakeTimers) {
this.fakeTimers.dispose();
}
if (this.global) {
this.global.close();
}
this.global = null;
this.document = null;
this.fakeTimers = null;
return Promise.resolve();
}
runScript(script) {
if (this.dom) {
return this.dom.runVMScript(script);
}
return null;
}
}
module.exports = JSDOMEnvironment;