UNPKG

4.71 kBJavaScriptView Raw
1'use strict';
2
3function _jsdom() {
4 const data = require('jsdom');
5
6 _jsdom = function () {
7 return data;
8 };
9
10 return data;
11}
12
13function _fakeTimers() {
14 const data = require('@jest/fake-timers');
15
16 _fakeTimers = function () {
17 return data;
18 };
19
20 return data;
21}
22
23function _jestMock() {
24 const data = require('jest-mock');
25
26 _jestMock = function () {
27 return data;
28 };
29
30 return data;
31}
32
33function _jestUtil() {
34 const data = require('jest-util');
35
36 _jestUtil = function () {
37 return data;
38 };
39
40 return data;
41}
42
43function _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
57class 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)(
72 typeof config.testEnvironmentOptions.html === 'string'
73 ? config.testEnvironmentOptions.html
74 : '<!DOCTYPE html>',
75 {
76 pretendToBeVisual: true,
77 resources:
78 typeof config.testEnvironmentOptions.userAgent === 'string'
79 ? new (_jsdom().ResourceLoader)({
80 userAgent: config.testEnvironmentOptions.userAgent
81 })
82 : undefined,
83 runScripts: 'dangerously',
84 url: config.testURL,
85 virtualConsole: new (_jsdom().VirtualConsole)().sendTo(
86 (options === null || options === void 0 ? void 0 : options.console) ||
87 console
88 ),
89 ...config.testEnvironmentOptions
90 }
91 );
92 const global = (this.global = this.dom.window.document.defaultView);
93
94 if (!global) {
95 throw new Error('JSDOM did not return a Window object');
96 } // for "universal" code (code should use `globalThis`)
97
98 global.global = global; // Node's error-message stack size is limited at 10, but it's pretty useful
99 // to see more than that when a test fails.
100
101 this.global.Error.stackTraceLimit = 100;
102 (0, _jestUtil().installCommonGlobals)(global, config.globals); // TODO: remove this ASAP, but it currently causes tests to run really slow
103
104 global.Buffer = Buffer; // Report uncaught errors.
105
106 this.errorEventListener = event => {
107 if (userErrorListenerCount === 0 && event.error) {
108 process.emit('uncaughtException', event.error);
109 }
110 };
111
112 global.addEventListener('error', this.errorEventListener); // However, don't report them as uncaught if the user listens to 'error' event.
113 // In that case, we assume the might have custom error handling logic.
114
115 const originalAddListener = global.addEventListener;
116 const originalRemoveListener = global.removeEventListener;
117 let userErrorListenerCount = 0;
118
119 global.addEventListener = function (...args) {
120 if (args[0] === 'error') {
121 userErrorListenerCount++;
122 }
123
124 return originalAddListener.apply(this, args);
125 };
126
127 global.removeEventListener = function (...args) {
128 if (args[0] === 'error') {
129 userErrorListenerCount--;
130 }
131
132 return originalRemoveListener.apply(this, args);
133 };
134
135 this.moduleMocker = new (_jestMock().ModuleMocker)(global);
136 const timerConfig = {
137 idToRef: id => id,
138 refToId: ref => ref
139 };
140 this.fakeTimers = new (_fakeTimers().LegacyFakeTimers)({
141 config,
142 global: global,
143 moduleMocker: this.moduleMocker,
144 timerConfig
145 });
146 this.fakeTimersModern = new (_fakeTimers().ModernFakeTimers)({
147 config,
148 global: global
149 });
150 }
151
152 async setup() {}
153
154 async teardown() {
155 if (this.fakeTimers) {
156 this.fakeTimers.dispose();
157 }
158
159 if (this.fakeTimersModern) {
160 this.fakeTimersModern.dispose();
161 }
162
163 if (this.global) {
164 if (this.errorEventListener) {
165 this.global.removeEventListener('error', this.errorEventListener);
166 } // Dispose "document" to prevent "load" event from triggering.
167
168 Object.defineProperty(this.global, 'document', {
169 value: null
170 });
171 this.global.close();
172 }
173
174 this.errorEventListener = null; // @ts-expect-error
175
176 this.global = null;
177 this.dom = null;
178 this.fakeTimers = null;
179 this.fakeTimersModern = null;
180 }
181
182 getVmContext() {
183 if (this.dom) {
184 return this.dom.getInternalVMContext();
185 }
186
187 return null;
188 }
189}
190
191module.exports = JSDOMEnvironment;