jest-environment-jsdom 29.1.2 → 29.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/build/index.js +28 -48
  2. package/package.json +16 -8
package/build/index.js CHANGED
@@ -4,53 +4,44 @@ Object.defineProperty(exports, '__esModule', {
4
4
  value: true
5
5
  });
6
6
  exports.default = exports.TestEnvironment = void 0;
7
-
8
7
  function _jsdom() {
9
8
  const data = require('jsdom');
10
-
11
9
  _jsdom = function () {
12
10
  return data;
13
11
  };
14
-
15
12
  return data;
16
13
  }
17
-
18
14
  function _fakeTimers() {
19
15
  const data = require('@jest/fake-timers');
20
-
21
16
  _fakeTimers = function () {
22
17
  return data;
23
18
  };
24
-
25
19
  return data;
26
20
  }
27
-
28
21
  function _jestMock() {
29
22
  const data = require('jest-mock');
30
-
31
23
  _jestMock = function () {
32
24
  return data;
33
25
  };
34
-
35
26
  return data;
36
27
  }
37
-
38
28
  function _jestUtil() {
39
29
  const data = require('jest-util');
40
-
41
30
  _jestUtil = function () {
42
31
  return data;
43
32
  };
44
-
45
33
  return data;
46
34
  }
47
-
48
35
  /**
49
36
  * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
50
37
  *
51
38
  * This source code is licensed under the MIT license found in the
52
39
  * LICENSE file in the root directory of this source tree.
53
40
  */
41
+
42
+ function isString(value) {
43
+ return typeof value === 'string';
44
+ }
54
45
  class JSDOMEnvironment {
55
46
  dom;
56
47
  fakeTimers;
@@ -59,7 +50,6 @@ class JSDOMEnvironment {
59
50
  errorEventListener;
60
51
  moduleMocker;
61
52
  customExportConditions = ['browser'];
62
-
63
53
  constructor(config, context) {
64
54
  const {projectConfig} = config;
65
55
  const virtualConsole = new (_jsdom().VirtualConsole)();
@@ -88,54 +78,51 @@ class JSDOMEnvironment {
88
78
  }
89
79
  );
90
80
  const global = (this.global = this.dom.window.document.defaultView);
91
-
92
- if (!global) {
81
+ if (global == null) {
93
82
  throw new Error('JSDOM did not return a Window object');
94
- } // for "universal" code (code should use `globalThis`)
83
+ }
95
84
 
96
- global.global = global; // Node's error-message stack size is limited at 10, but it's pretty useful
97
- // to see more than that when a test fails.
85
+ // @ts-expect-error - for "universal" code (code should use `globalThis`)
86
+ global.global = global;
98
87
 
88
+ // Node's error-message stack size is limited at 10, but it's pretty useful
89
+ // to see more than that when a test fails.
99
90
  this.global.Error.stackTraceLimit = 100;
100
- (0, _jestUtil().installCommonGlobals)(global, projectConfig.globals); // TODO: remove this ASAP, but it currently causes tests to run really slow
91
+ (0, _jestUtil().installCommonGlobals)(global, projectConfig.globals);
101
92
 
102
- global.Buffer = Buffer; // Report uncaught errors.
93
+ // TODO: remove this ASAP, but it currently causes tests to run really slow
94
+ global.Buffer = Buffer;
103
95
 
96
+ // Report uncaught errors.
104
97
  this.errorEventListener = event => {
105
- if (userErrorListenerCount === 0 && event.error) {
98
+ if (userErrorListenerCount === 0 && event.error != null) {
106
99
  process.emit('uncaughtException', event.error);
107
100
  }
108
101
  };
102
+ global.addEventListener('error', this.errorEventListener);
109
103
 
110
- global.addEventListener('error', this.errorEventListener); // However, don't report them as uncaught if the user listens to 'error' event.
104
+ // However, don't report them as uncaught if the user listens to 'error' event.
111
105
  // In that case, we assume the might have custom error handling logic.
112
-
113
- const originalAddListener = global.addEventListener;
114
- const originalRemoveListener = global.removeEventListener;
106
+ const originalAddListener = global.addEventListener.bind(global);
107
+ const originalRemoveListener = global.removeEventListener.bind(global);
115
108
  let userErrorListenerCount = 0;
116
-
117
109
  global.addEventListener = function (...args) {
118
110
  if (args[0] === 'error') {
119
111
  userErrorListenerCount++;
120
112
  }
121
-
122
113
  return originalAddListener.apply(this, args);
123
114
  };
124
-
125
115
  global.removeEventListener = function (...args) {
126
116
  if (args[0] === 'error') {
127
117
  userErrorListenerCount--;
128
118
  }
129
-
130
119
  return originalRemoveListener.apply(this, args);
131
120
  };
132
-
133
121
  if ('customExportConditions' in projectConfig.testEnvironmentOptions) {
134
122
  const {customExportConditions} = projectConfig.testEnvironmentOptions;
135
-
136
123
  if (
137
124
  Array.isArray(customExportConditions) &&
138
- customExportConditions.every(item => typeof item === 'string')
125
+ customExportConditions.every(isString)
139
126
  ) {
140
127
  this.customExportConditions = customExportConditions;
141
128
  } else {
@@ -144,7 +131,6 @@ class JSDOMEnvironment {
144
131
  );
145
132
  }
146
133
  }
147
-
148
134
  this.moduleMocker = new (_jestMock().ModuleMocker)(global);
149
135
  this.fakeTimers = new (_fakeTimers().LegacyFakeTimers)({
150
136
  config: projectConfig,
@@ -159,55 +145,49 @@ class JSDOMEnvironment {
159
145
  config: projectConfig,
160
146
  global: global
161
147
  });
162
- } // eslint-disable-next-line @typescript-eslint/no-empty-function
148
+ }
163
149
 
150
+ // eslint-disable-next-line @typescript-eslint/no-empty-function
164
151
  async setup() {}
165
-
166
152
  async teardown() {
167
153
  if (this.fakeTimers) {
168
154
  this.fakeTimers.dispose();
169
155
  }
170
-
171
156
  if (this.fakeTimersModern) {
172
157
  this.fakeTimersModern.dispose();
173
158
  }
174
-
175
- if (this.global) {
159
+ if (this.global != null) {
176
160
  if (this.errorEventListener) {
177
161
  this.global.removeEventListener('error', this.errorEventListener);
178
162
  }
163
+ this.global.close();
164
+
165
+ // Dispose "document" to prevent "load" event from triggering.
179
166
 
180
- this.global.close(); // Dispose "document" to prevent "load" event from triggering.
181
167
  // Note that this.global.close() will trigger the CustomElement::disconnectedCallback
182
168
  // Do not reset the document before CustomElement disconnectedCallback function has finished running,
183
169
  // document should be accessible within disconnectedCallback.
184
-
185
170
  Object.defineProperty(this.global, 'document', {
186
171
  value: null
187
172
  });
188
173
  }
189
-
190
- this.errorEventListener = null; // @ts-expect-error: this.global not allowed to be `null`
191
-
174
+ this.errorEventListener = null;
175
+ // @ts-expect-error: this.global not allowed to be `null`
192
176
  this.global = null;
193
177
  this.dom = null;
194
178
  this.fakeTimers = null;
195
179
  this.fakeTimersModern = null;
196
180
  }
197
-
198
181
  exportConditions() {
199
182
  return this.customExportConditions;
200
183
  }
201
-
202
184
  getVmContext() {
203
185
  if (this.dom) {
204
186
  return this.dom.getInternalVMContext();
205
187
  }
206
-
207
188
  return null;
208
189
  }
209
190
  }
210
-
211
191
  exports.default = JSDOMEnvironment;
212
192
  const TestEnvironment = JSDOMEnvironment;
213
193
  exports.TestEnvironment = TestEnvironment;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jest-environment-jsdom",
3
- "version": "29.1.2",
3
+ "version": "29.2.0",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/facebook/jest.git",
@@ -17,17 +17,25 @@
17
17
  "./package.json": "./package.json"
18
18
  },
19
19
  "dependencies": {
20
- "@jest/environment": "^29.1.2",
21
- "@jest/fake-timers": "^29.1.2",
22
- "@jest/types": "^29.1.2",
20
+ "@jest/environment": "^29.2.0",
21
+ "@jest/fake-timers": "^29.2.0",
22
+ "@jest/types": "^29.2.0",
23
23
  "@types/jsdom": "^20.0.0",
24
24
  "@types/node": "*",
25
- "jest-mock": "^29.1.2",
26
- "jest-util": "^29.1.2",
25
+ "jest-mock": "^29.2.0",
26
+ "jest-util": "^29.2.0",
27
27
  "jsdom": "^20.0.0"
28
28
  },
29
29
  "devDependencies": {
30
- "@jest/test-utils": "^29.1.2"
30
+ "@jest/test-utils": "^29.2.0"
31
+ },
32
+ "peerDependencies": {
33
+ "canvas": "^2.5.0"
34
+ },
35
+ "peerDependenciesMeta": {
36
+ "canvas": {
37
+ "optional": true
38
+ }
31
39
  },
32
40
  "engines": {
33
41
  "node": "^14.15.0 || ^16.10.0 || >=18.0.0"
@@ -35,5 +43,5 @@
35
43
  "publishConfig": {
36
44
  "access": "public"
37
45
  },
38
- "gitHead": "3c31dd619e8c022cde53f40fa12ea2a67f4752ce"
46
+ "gitHead": "ee5b37a4f4433afcfffb0356cea47739d8092287"
39
47
  }