1 | 'use strict';
|
2 |
|
3 | function _jsdom() {
|
4 | const data = require('jsdom');
|
5 |
|
6 | _jsdom = function () {
|
7 | return data;
|
8 | };
|
9 |
|
10 | return data;
|
11 | }
|
12 |
|
13 | function _fakeTimers() {
|
14 | const data = require('@jest/fake-timers');
|
15 |
|
16 | _fakeTimers = function () {
|
17 | return data;
|
18 | };
|
19 |
|
20 | return data;
|
21 | }
|
22 |
|
23 | function _jestMock() {
|
24 | const data = require('jest-mock');
|
25 |
|
26 | _jestMock = function () {
|
27 | return data;
|
28 | };
|
29 |
|
30 | return data;
|
31 | }
|
32 |
|
33 | function _jestUtil() {
|
34 | const data = require('jest-util');
|
35 |
|
36 | _jestUtil = function () {
|
37 | return data;
|
38 | };
|
39 |
|
40 | return data;
|
41 | }
|
42 |
|
43 | function _defineProperty(obj, key, value) {
|
44 | if (key in obj) {
|
45 | Object.defineProperty(obj, key, {
|
46 | value: value,
|
47 | enumerable: true,
|
48 | configurable: true,
|
49 | writable: true
|
50 | });
|
51 | } else {
|
52 | obj[key] = value;
|
53 | }
|
54 | return obj;
|
55 | }
|
56 |
|
57 | class JSDOMEnvironment {
|
58 | constructor(config, options) {
|
59 | _defineProperty(this, 'dom', void 0);
|
60 |
|
61 | _defineProperty(this, 'fakeTimers', void 0);
|
62 |
|
63 | _defineProperty(this, 'fakeTimersModern', void 0);
|
64 |
|
65 | _defineProperty(this, 'global', void 0);
|
66 |
|
67 | _defineProperty(this, 'errorEventListener', void 0);
|
68 |
|
69 | _defineProperty(this, 'moduleMocker', void 0);
|
70 |
|
71 | this.dom = new (_jsdom().JSDOM)('<!DOCTYPE html>', {
|
72 | pretendToBeVisual: true,
|
73 | resources:
|
74 | typeof config.testEnvironmentOptions.userAgent === 'string'
|
75 | ? new (_jsdom().ResourceLoader)({
|
76 | userAgent: config.testEnvironmentOptions.userAgent
|
77 | })
|
78 | : undefined,
|
79 | runScripts: 'dangerously',
|
80 | url: config.testURL,
|
81 | virtualConsole: new (_jsdom().VirtualConsole)().sendTo(
|
82 | (options === null || options === void 0 ? void 0 : options.console) ||
|
83 | console
|
84 | ),
|
85 | ...config.testEnvironmentOptions
|
86 | });
|
87 | const global = (this.global = this.dom.window.document.defaultView);
|
88 |
|
89 | if (!global) {
|
90 | throw new Error('JSDOM did not return a Window object');
|
91 | }
|
92 |
|
93 | global.global = global;
|
94 |
|
95 |
|
96 | this.global.Error.stackTraceLimit = 100;
|
97 | (0, _jestUtil().installCommonGlobals)(global, config.globals);
|
98 |
|
99 | global.Buffer = Buffer;
|
100 |
|
101 | this.errorEventListener = event => {
|
102 | if (userErrorListenerCount === 0 && event.error) {
|
103 | process.emit('uncaughtException', event.error);
|
104 | }
|
105 | };
|
106 |
|
107 | global.addEventListener('error', this.errorEventListener);
|
108 |
|
109 |
|
110 | const originalAddListener = global.addEventListener;
|
111 | const originalRemoveListener = global.removeEventListener;
|
112 | let userErrorListenerCount = 0;
|
113 |
|
114 | global.addEventListener = function (...args) {
|
115 | if (args[0] === 'error') {
|
116 | userErrorListenerCount++;
|
117 | }
|
118 |
|
119 | return originalAddListener.apply(this, args);
|
120 | };
|
121 |
|
122 | global.removeEventListener = function (...args) {
|
123 | if (args[0] === 'error') {
|
124 | userErrorListenerCount--;
|
125 | }
|
126 |
|
127 | return originalRemoveListener.apply(this, args);
|
128 | };
|
129 |
|
130 | this.moduleMocker = new (_jestMock().ModuleMocker)(global);
|
131 | const timerConfig = {
|
132 | idToRef: id => id,
|
133 | refToId: ref => ref
|
134 | };
|
135 | this.fakeTimers = new (_fakeTimers().LegacyFakeTimers)({
|
136 | config,
|
137 | global: global,
|
138 | moduleMocker: this.moduleMocker,
|
139 | timerConfig
|
140 | });
|
141 | this.fakeTimersModern = new (_fakeTimers().ModernFakeTimers)({
|
142 | config,
|
143 | global: global
|
144 | });
|
145 | }
|
146 |
|
147 | async setup() {}
|
148 |
|
149 | async teardown() {
|
150 | if (this.fakeTimers) {
|
151 | this.fakeTimers.dispose();
|
152 | }
|
153 |
|
154 | if (this.fakeTimersModern) {
|
155 | this.fakeTimersModern.dispose();
|
156 | }
|
157 |
|
158 | if (this.global) {
|
159 | if (this.errorEventListener) {
|
160 | this.global.removeEventListener('error', this.errorEventListener);
|
161 | }
|
162 |
|
163 | Object.defineProperty(this.global, 'document', {
|
164 | value: null
|
165 | });
|
166 | this.global.close();
|
167 | }
|
168 |
|
169 | this.errorEventListener = null;
|
170 |
|
171 | this.global = null;
|
172 | this.dom = null;
|
173 | this.fakeTimers = null;
|
174 | this.fakeTimersModern = null;
|
175 | }
|
176 |
|
177 | getVmContext() {
|
178 | if (this.dom) {
|
179 | return this.dom.getInternalVMContext();
|
180 | }
|
181 |
|
182 | return null;
|
183 | }
|
184 | }
|
185 |
|
186 | module.exports = JSDOMEnvironment;
|