vitest 4.0.6 → 4.0.8
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.
- package/LICENSE.md +1 -1
- package/dist/browser.d.ts +2 -2
- package/dist/browser.js +2 -2
- package/dist/chunks/base.BgTO2qAg.js +156 -0
- package/dist/chunks/{benchmark.DHKMYAts.js → benchmark.B3N2zMcH.js} +9 -4
- package/dist/chunks/{browser.d.ScGeWTou.d.ts → browser.d.DTTM2PTh.d.ts} +1 -1
- package/dist/chunks/{cac.BBqWH4nd.js → cac.CfkWq8Qy.js} +117 -43
- package/dist/chunks/{cli-api.UL3SwFUb.js → cli-api.BQ-bjcRi.js} +1870 -847
- package/dist/chunks/console.Cf-YriPC.js +146 -0
- package/dist/chunks/{coverage.DuCn_Tmx.js → coverage.NVjCOln1.js} +281 -103
- package/dist/chunks/{creator.cqqifzG7.js → creator.fzVyoMf3.js} +74 -30
- package/dist/chunks/{date.-jtEtIeV.js → date.Bq6ZW5rf.js} +17 -6
- package/dist/chunks/{git.BFNcloKD.js → git.Bm2pzPAa.js} +3 -3
- package/dist/chunks/{global.d.DdOkMiVb.d.ts → global.d.DVdCfKp5.d.ts} +1 -1
- package/dist/chunks/{globals.BGT_RUsD.js → globals.DOh96BiR.js} +5 -5
- package/dist/chunks/{resolveSnapshotEnvironment.BZzLjzkh.js → index.BY4-tcno.js} +42 -25
- package/dist/chunks/{index.Bgo3tNWt.js → index.DAL392Ss.js} +40 -15
- package/dist/chunks/{index.RwjEGCQ0.js → index.DIFZf73e.js} +2 -2
- package/dist/chunks/{index.DV0mQLEO.js → index.DfKyPFVi.js} +195 -64
- package/dist/chunks/{index.BL8Hg4Uk.js → index.kotH7DY7.js} +837 -380
- package/dist/chunks/{index.CpdwpN7L.js → index.op2Re5rn.js} +22 -12
- package/dist/chunks/{init-forks.CSGFj9zN.js → init-forks.2hx7cf78.js} +16 -5
- package/dist/chunks/{init-threads.CIJLeFO8.js → init-threads.Cm4OCIWA.js} +3 -2
- package/dist/chunks/{init.DUeOfNO9.js → init.DMDG-idf.js} +124 -54
- package/dist/chunks/{inspector.DLZxSeU3.js → inspector.CvyFGlXm.js} +25 -10
- package/dist/chunks/{moduleRunner.d.TP-w6tIQ.d.ts → moduleRunner.d.CzOZ_4wC.d.ts} +1 -1
- package/dist/chunks/{node.BwAWWjHZ.js → node.Ce0vMQM7.js} +1 -1
- package/dist/chunks/{plugin.d.lctzD3Wk.d.ts → plugin.d.D4RrtywJ.d.ts} +1 -1
- package/dist/chunks/{reporters.d.PEs0tXod.d.ts → reporters.d.Da1D1VbQ.d.ts} +19 -9
- package/dist/chunks/rpc.BUV7uWKJ.js +76 -0
- package/dist/chunks/{setup-common.DR1sucx6.js → setup-common.LGjNSzXp.js} +20 -8
- package/dist/chunks/{startModuleRunner.Di-EZqh0.js → startModuleRunner.BOmUtLIO.js} +228 -105
- package/dist/chunks/{test.CnspO-X4.js → test.ClrAtjMv.js} +48 -22
- package/dist/chunks/{utils.CG9h5ccR.js → utils.DvEY5TfP.js} +14 -5
- package/dist/chunks/{vi.BZvkKVkM.js → vi.Bgcdy3bQ.js} +261 -111
- package/dist/chunks/{vm.Co_lR2NL.js → vm.BIkCDs68.js} +177 -70
- package/dist/chunks/{worker.d.B4Hthdvt.d.ts → worker.d.DadbA89M.d.ts} +52 -6
- package/dist/cli.js +2 -2
- package/dist/config.d.ts +5 -5
- package/dist/coverage.d.ts +3 -3
- package/dist/coverage.js +1 -1
- package/dist/environments.js +2 -1
- package/dist/index.d.ts +5 -5
- package/dist/index.js +5 -5
- package/dist/module-evaluator.d.ts +2 -2
- package/dist/module-evaluator.js +85 -35
- package/dist/module-runner.js +2 -2
- package/dist/node.d.ts +7 -7
- package/dist/node.js +16 -12
- package/dist/reporters.d.ts +3 -3
- package/dist/reporters.js +2 -2
- package/dist/runners.js +7 -7
- package/dist/snapshot.js +2 -2
- package/dist/suite.js +2 -2
- package/dist/worker.d.ts +2 -1
- package/dist/worker.js +27 -27
- package/dist/workers/forks.js +34 -31
- package/dist/workers/runVmTests.js +41 -22
- package/dist/workers/threads.js +34 -31
- package/dist/workers/vmForks.js +14 -14
- package/dist/workers/vmThreads.js +14 -14
- package/package.json +20 -20
- package/dist/chunks/base.BAf_bYeI.js +0 -128
- package/dist/chunks/console.CTJL2nuH.js +0 -115
- package/dist/chunks/rpc.Dv1Jt3i2.js +0 -66
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { URL } from 'node:url';
|
|
1
2
|
import { Console } from 'node:console';
|
|
2
3
|
|
|
3
4
|
// SEE https://github.com/jsdom/jsdom/blob/master/lib/jsdom/living/interfaces.js
|
|
@@ -136,6 +137,7 @@ const LIVING_KEYS = [
|
|
|
136
137
|
"Plugin",
|
|
137
138
|
"MimeType",
|
|
138
139
|
"FileReader",
|
|
140
|
+
"FormData",
|
|
139
141
|
"Blob",
|
|
140
142
|
"File",
|
|
141
143
|
"FileList",
|
|
@@ -174,7 +176,8 @@ const LIVING_KEYS = [
|
|
|
174
176
|
"Audio",
|
|
175
177
|
"Option",
|
|
176
178
|
"CSS"
|
|
177
|
-
]
|
|
179
|
+
];
|
|
180
|
+
const OTHER_KEYS = [
|
|
178
181
|
"addEventListener",
|
|
179
182
|
"alert",
|
|
180
183
|
"blur",
|
|
@@ -239,20 +242,28 @@ const skipKeys = [
|
|
|
239
242
|
function getWindowKeys(global, win, additionalKeys = []) {
|
|
240
243
|
const keysArray = [...additionalKeys, ...KEYS];
|
|
241
244
|
return new Set(keysArray.concat(Object.getOwnPropertyNames(win)).filter((k) => {
|
|
242
|
-
|
|
245
|
+
if (skipKeys.includes(k)) return false;
|
|
246
|
+
if (k in global) return keysArray.includes(k);
|
|
247
|
+
return true;
|
|
243
248
|
}));
|
|
244
249
|
}
|
|
245
250
|
function isClassLikeName(name) {
|
|
246
251
|
return name[0] === name[0].toUpperCase();
|
|
247
252
|
}
|
|
248
253
|
function populateGlobal(global, win, options = {}) {
|
|
249
|
-
const { bindFunctions = false } = options
|
|
254
|
+
const { bindFunctions = false } = options;
|
|
255
|
+
const keys = getWindowKeys(global, win, options.additionalKeys);
|
|
256
|
+
const originals = /* @__PURE__ */ new Map();
|
|
257
|
+
const overridenKeys = new Set([...KEYS, ...options.additionalKeys || []]);
|
|
258
|
+
const overrideObject = /* @__PURE__ */ new Map();
|
|
250
259
|
for (const key of keys) {
|
|
251
260
|
const boundFunction = bindFunctions && typeof win[key] === "function" && !isClassLikeName(key) && win[key].bind(win);
|
|
252
261
|
if (overridenKeys.has(key) && key in global) originals.set(key, global[key]);
|
|
253
262
|
Object.defineProperty(global, key, {
|
|
254
263
|
get() {
|
|
255
|
-
|
|
264
|
+
if (overrideObject.has(key)) return overrideObject.get(key);
|
|
265
|
+
if (boundFunction) return boundFunction;
|
|
266
|
+
return win[key];
|
|
256
267
|
},
|
|
257
268
|
set(v) {
|
|
258
269
|
overrideObject.set(key, v);
|
|
@@ -260,14 +271,19 @@ function populateGlobal(global, win, options = {}) {
|
|
|
260
271
|
configurable: true
|
|
261
272
|
});
|
|
262
273
|
}
|
|
263
|
-
|
|
274
|
+
global.window = global;
|
|
275
|
+
global.self = global;
|
|
276
|
+
global.top = global;
|
|
277
|
+
global.parent = global;
|
|
278
|
+
if (global.global) global.global = global;
|
|
264
279
|
// rewrite defaultView to reference the same global context
|
|
265
280
|
if (global.document && global.document.defaultView) Object.defineProperty(global.document, "defaultView", {
|
|
266
281
|
get: () => global,
|
|
267
282
|
enumerable: true,
|
|
268
283
|
configurable: true
|
|
269
284
|
});
|
|
270
|
-
|
|
285
|
+
skipKeys.forEach((k) => keys.add(k));
|
|
286
|
+
return {
|
|
271
287
|
keys,
|
|
272
288
|
skipKeys,
|
|
273
289
|
originals
|
|
@@ -278,8 +294,11 @@ var edge = {
|
|
|
278
294
|
name: "edge-runtime",
|
|
279
295
|
viteEnvironment: "ssr",
|
|
280
296
|
async setupVM() {
|
|
281
|
-
const { EdgeVM } = await import('@edge-runtime/vm')
|
|
282
|
-
|
|
297
|
+
const { EdgeVM } = await import('@edge-runtime/vm');
|
|
298
|
+
const vm = new EdgeVM({ extend: (context) => {
|
|
299
|
+
context.global = context;
|
|
300
|
+
context.Buffer = Buffer;
|
|
301
|
+
return context;
|
|
283
302
|
} });
|
|
284
303
|
return {
|
|
285
304
|
getVmContext() {
|
|
@@ -291,20 +310,27 @@ var edge = {
|
|
|
291
310
|
};
|
|
292
311
|
},
|
|
293
312
|
async setup(global) {
|
|
294
|
-
const { EdgeVM } = await import('@edge-runtime/vm')
|
|
295
|
-
|
|
313
|
+
const { EdgeVM } = await import('@edge-runtime/vm');
|
|
314
|
+
const { keys, originals } = populateGlobal(global, new EdgeVM({ extend: (context) => {
|
|
315
|
+
context.global = context;
|
|
316
|
+
context.Buffer = Buffer;
|
|
317
|
+
KEYS.forEach((key) => {
|
|
296
318
|
if (key in global) context[key] = global[key];
|
|
297
|
-
})
|
|
298
|
-
|
|
319
|
+
});
|
|
320
|
+
return context;
|
|
321
|
+
} }).context, { bindFunctions: true });
|
|
299
322
|
return { teardown(global) {
|
|
300
|
-
keys.forEach((key) => delete global[key])
|
|
323
|
+
keys.forEach((key) => delete global[key]);
|
|
324
|
+
originals.forEach((v, k) => global[k] = v);
|
|
301
325
|
} };
|
|
302
326
|
}
|
|
303
327
|
};
|
|
304
328
|
|
|
305
329
|
async function teardownWindow(win) {
|
|
306
|
-
if (win.close && win.happyDOM.abort)
|
|
307
|
-
|
|
330
|
+
if (win.close && win.happyDOM.abort) {
|
|
331
|
+
await win.happyDOM.abort();
|
|
332
|
+
win.close();
|
|
333
|
+
} else win.happyDOM.cancelAsync();
|
|
308
334
|
}
|
|
309
335
|
var happy = {
|
|
310
336
|
name: "happy-dom",
|
|
@@ -320,21 +346,25 @@ var happy = {
|
|
|
320
346
|
disableErrorCapturing: true
|
|
321
347
|
}
|
|
322
348
|
});
|
|
349
|
+
// TODO: browser doesn't expose Buffer, but a lot of dependencies use it
|
|
350
|
+
win.Buffer = Buffer;
|
|
323
351
|
// inject structuredClone if it exists
|
|
324
|
-
if (
|
|
352
|
+
if (typeof structuredClone !== "undefined" && !win.structuredClone) win.structuredClone = structuredClone;
|
|
325
353
|
return {
|
|
326
354
|
getVmContext() {
|
|
327
355
|
return win;
|
|
328
356
|
},
|
|
329
357
|
async teardown() {
|
|
330
|
-
await teardownWindow(win)
|
|
358
|
+
await teardownWindow(win);
|
|
359
|
+
win = void 0;
|
|
331
360
|
}
|
|
332
361
|
};
|
|
333
362
|
},
|
|
334
363
|
async setup(global, { happyDOM = {} }) {
|
|
335
364
|
// happy-dom v3 introduced a breaking change to Window, but
|
|
336
365
|
// provides GlobalWindow as a way to use previous behaviour
|
|
337
|
-
const { Window, GlobalWindow } = await import('happy-dom')
|
|
366
|
+
const { Window, GlobalWindow } = await import('happy-dom');
|
|
367
|
+
const win = new (GlobalWindow || Window)({
|
|
338
368
|
...happyDOM,
|
|
339
369
|
console: console && global.console ? global.console : void 0,
|
|
340
370
|
url: happyDOM.url || "http://localhost:3000",
|
|
@@ -342,7 +372,8 @@ var happy = {
|
|
|
342
372
|
...happyDOM.settings,
|
|
343
373
|
disableErrorCapturing: true
|
|
344
374
|
}
|
|
345
|
-
})
|
|
375
|
+
});
|
|
376
|
+
const { keys, originals } = populateGlobal(global, win, {
|
|
346
377
|
bindFunctions: true,
|
|
347
378
|
additionalKeys: [
|
|
348
379
|
"Request",
|
|
@@ -358,7 +389,9 @@ var happy = {
|
|
|
358
389
|
]
|
|
359
390
|
});
|
|
360
391
|
return { async teardown(global) {
|
|
361
|
-
await teardownWindow(win)
|
|
392
|
+
await teardownWindow(win);
|
|
393
|
+
keys.forEach((key) => delete global[key]);
|
|
394
|
+
originals.forEach((v, k) => global[k] = v);
|
|
362
395
|
} };
|
|
363
396
|
}
|
|
364
397
|
};
|
|
@@ -366,41 +399,66 @@ var happy = {
|
|
|
366
399
|
function catchWindowErrors(window) {
|
|
367
400
|
let userErrorListenerCount = 0;
|
|
368
401
|
function throwUnhandlerError(e) {
|
|
369
|
-
if (userErrorListenerCount === 0 && e.error != null)
|
|
402
|
+
if (userErrorListenerCount === 0 && e.error != null) {
|
|
403
|
+
e.preventDefault();
|
|
404
|
+
process.emit("uncaughtException", e.error);
|
|
405
|
+
}
|
|
370
406
|
}
|
|
371
|
-
const addEventListener = window.addEventListener.bind(window)
|
|
372
|
-
|
|
407
|
+
const addEventListener = window.addEventListener.bind(window);
|
|
408
|
+
const removeEventListener = window.removeEventListener.bind(window);
|
|
409
|
+
window.addEventListener("error", throwUnhandlerError);
|
|
410
|
+
window.addEventListener = function(...args) {
|
|
373
411
|
if (args[0] === "error") userErrorListenerCount++;
|
|
374
412
|
return addEventListener.apply(this, args);
|
|
375
|
-
}
|
|
413
|
+
};
|
|
414
|
+
window.removeEventListener = function(...args) {
|
|
376
415
|
if (args[0] === "error" && userErrorListenerCount) userErrorListenerCount--;
|
|
377
416
|
return removeEventListener.apply(this, args);
|
|
378
|
-
}
|
|
417
|
+
};
|
|
418
|
+
return function clearErrorHandlers() {
|
|
379
419
|
window.removeEventListener("error", throwUnhandlerError);
|
|
380
420
|
};
|
|
381
421
|
}
|
|
382
|
-
let
|
|
422
|
+
let NodeFormData_;
|
|
423
|
+
let NodeBlob_;
|
|
424
|
+
let NodeRequest_;
|
|
383
425
|
var jsdom = {
|
|
384
426
|
name: "jsdom",
|
|
385
427
|
viteEnvironment: "client",
|
|
386
428
|
async setupVM({ jsdom = {} }) {
|
|
387
429
|
// delay initialization because it takes ~1s
|
|
388
|
-
|
|
389
|
-
|
|
430
|
+
NodeFormData_ = globalThis.FormData;
|
|
431
|
+
NodeBlob_ = globalThis.Blob;
|
|
432
|
+
NodeRequest_ = globalThis.Request;
|
|
433
|
+
const { CookieJar, JSDOM, ResourceLoader, VirtualConsole } = await import('jsdom');
|
|
434
|
+
const { html = "<!DOCTYPE html>", userAgent, url = "http://localhost:3000", contentType = "text/html", pretendToBeVisual = true, includeNodeLocations = false, runScripts = "dangerously", resources, console = false, cookieJar = false,...restOptions } = jsdom;
|
|
435
|
+
let virtualConsole;
|
|
436
|
+
if (console && globalThis.console) {
|
|
437
|
+
virtualConsole = new VirtualConsole();
|
|
438
|
+
// jsdom <27
|
|
439
|
+
if ("sendTo" in virtualConsole) virtualConsole.sendTo(globalThis.console);
|
|
440
|
+
else virtualConsole.forwardTo(globalThis.console);
|
|
441
|
+
}
|
|
390
442
|
let dom = new JSDOM(html, {
|
|
391
443
|
pretendToBeVisual,
|
|
392
444
|
resources: resources ?? (userAgent ? new ResourceLoader({ userAgent }) : void 0),
|
|
393
445
|
runScripts,
|
|
394
446
|
url,
|
|
395
|
-
virtualConsole
|
|
447
|
+
virtualConsole,
|
|
396
448
|
cookieJar: cookieJar ? new CookieJar() : void 0,
|
|
397
449
|
includeNodeLocations,
|
|
398
450
|
contentType,
|
|
399
451
|
userAgent,
|
|
400
452
|
...restOptions
|
|
401
453
|
});
|
|
402
|
-
const clearAddEventListenerPatch = patchAddEventListener(dom.window)
|
|
403
|
-
|
|
454
|
+
const clearAddEventListenerPatch = patchAddEventListener(dom.window);
|
|
455
|
+
const clearWindowErrors = catchWindowErrors(dom.window);
|
|
456
|
+
const utils = createCompatUtils(dom.window);
|
|
457
|
+
// TODO: browser doesn't expose Buffer, but a lot of dependencies use it
|
|
458
|
+
dom.window.Buffer = Buffer;
|
|
459
|
+
dom.window.jsdom = dom;
|
|
460
|
+
dom.window.Request = createCompatRequest(utils);
|
|
461
|
+
dom.window.URL = createJSDOMCompatURL(utils);
|
|
404
462
|
for (const name of [
|
|
405
463
|
"structuredClone",
|
|
406
464
|
"BroadcastChannel",
|
|
@@ -414,12 +472,10 @@ var jsdom = {
|
|
|
414
472
|
}
|
|
415
473
|
for (const name of [
|
|
416
474
|
"fetch",
|
|
417
|
-
"Request",
|
|
418
475
|
"Response",
|
|
419
476
|
"Headers",
|
|
420
477
|
"AbortController",
|
|
421
478
|
"AbortSignal",
|
|
422
|
-
"URL",
|
|
423
479
|
"URLSearchParams"
|
|
424
480
|
]) {
|
|
425
481
|
const value = globalThis[name];
|
|
@@ -430,48 +486,106 @@ var jsdom = {
|
|
|
430
486
|
return dom.getInternalVMContext();
|
|
431
487
|
},
|
|
432
488
|
teardown() {
|
|
433
|
-
clearAddEventListenerPatch()
|
|
489
|
+
clearAddEventListenerPatch();
|
|
490
|
+
clearWindowErrors();
|
|
491
|
+
dom.window.close();
|
|
492
|
+
dom = void 0;
|
|
434
493
|
}
|
|
435
494
|
};
|
|
436
495
|
},
|
|
437
496
|
async setup(global, { jsdom = {} }) {
|
|
438
497
|
// delay initialization because it takes ~1s
|
|
439
|
-
|
|
440
|
-
|
|
498
|
+
NodeFormData_ = globalThis.FormData;
|
|
499
|
+
NodeBlob_ = globalThis.Blob;
|
|
500
|
+
NodeRequest_ = globalThis.Request;
|
|
501
|
+
const { CookieJar, JSDOM, ResourceLoader, VirtualConsole } = await import('jsdom');
|
|
502
|
+
const { html = "<!DOCTYPE html>", userAgent, url = "http://localhost:3000", contentType = "text/html", pretendToBeVisual = true, includeNodeLocations = false, runScripts = "dangerously", resources, console = false, cookieJar = false,...restOptions } = jsdom;
|
|
503
|
+
let virtualConsole;
|
|
504
|
+
if (console && globalThis.console) {
|
|
505
|
+
virtualConsole = new VirtualConsole();
|
|
506
|
+
// jsdom <27
|
|
507
|
+
if ("sendTo" in virtualConsole) virtualConsole.sendTo(globalThis.console);
|
|
508
|
+
else virtualConsole.forwardTo(globalThis.console);
|
|
509
|
+
}
|
|
510
|
+
const dom = new JSDOM(html, {
|
|
441
511
|
pretendToBeVisual,
|
|
442
512
|
resources: resources ?? (userAgent ? new ResourceLoader({ userAgent }) : void 0),
|
|
443
513
|
runScripts,
|
|
444
514
|
url,
|
|
445
|
-
virtualConsole
|
|
515
|
+
virtualConsole,
|
|
446
516
|
cookieJar: cookieJar ? new CookieJar() : void 0,
|
|
447
517
|
includeNodeLocations,
|
|
448
518
|
contentType,
|
|
449
519
|
userAgent,
|
|
450
520
|
...restOptions
|
|
451
|
-
})
|
|
452
|
-
|
|
453
|
-
|
|
521
|
+
});
|
|
522
|
+
const clearAddEventListenerPatch = patchAddEventListener(dom.window);
|
|
523
|
+
const { keys, originals } = populateGlobal(global, dom.window, { bindFunctions: true });
|
|
524
|
+
const clearWindowErrors = catchWindowErrors(global);
|
|
525
|
+
const utils = createCompatUtils(dom.window);
|
|
526
|
+
global.jsdom = dom;
|
|
527
|
+
global.Request = createCompatRequest(utils);
|
|
528
|
+
global.URL = createJSDOMCompatURL(utils);
|
|
529
|
+
return { teardown(global) {
|
|
530
|
+
clearAddEventListenerPatch();
|
|
531
|
+
clearWindowErrors();
|
|
532
|
+
dom.window.close();
|
|
533
|
+
delete global.jsdom;
|
|
534
|
+
keys.forEach((key) => delete global[key]);
|
|
535
|
+
originals.forEach((v, k) => global[k] = v);
|
|
454
536
|
} };
|
|
455
537
|
}
|
|
456
538
|
};
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
// override it with Node.js implementation, but keep the DOM behaviour
|
|
460
|
-
// this is required because Request (and other fetch API)
|
|
461
|
-
// are not implemented by JSDOM
|
|
462
|
-
function createFormData(window) {
|
|
463
|
-
const JSDOMFormData = window.FormData;
|
|
464
|
-
return _FormData ? class FormData extends _FormData {
|
|
539
|
+
function createCompatRequest(utils) {
|
|
540
|
+
return class Request extends NodeRequest_ {
|
|
465
541
|
constructor(...args) {
|
|
466
|
-
|
|
467
|
-
|
|
542
|
+
const [input, init] = args;
|
|
543
|
+
if (init?.body != null) {
|
|
544
|
+
const compatInit = { ...init };
|
|
545
|
+
if (init.body instanceof utils.window.Blob) compatInit.body = utils.makeCompatBlob(init.body);
|
|
546
|
+
if (init.body instanceof utils.window.FormData) compatInit.body = utils.makeCompatFormData(init.body);
|
|
547
|
+
super(input, compatInit);
|
|
548
|
+
} else super(...args);
|
|
549
|
+
}
|
|
550
|
+
};
|
|
551
|
+
}
|
|
552
|
+
function createJSDOMCompatURL(utils) {
|
|
553
|
+
return class URL$1 extends URL {
|
|
554
|
+
static createObjectURL(blob) {
|
|
555
|
+
if (blob instanceof utils.window.Blob) {
|
|
556
|
+
const compatBlob = utils.makeCompatBlob(blob);
|
|
557
|
+
return URL.createObjectURL(compatBlob);
|
|
558
|
+
}
|
|
559
|
+
return URL.createObjectURL(blob);
|
|
560
|
+
}
|
|
561
|
+
};
|
|
562
|
+
}
|
|
563
|
+
function createCompatUtils(window) {
|
|
564
|
+
// this returns a hidden Symbol(impl)
|
|
565
|
+
// this is cursed, and jsdom should just implement fetch API itself
|
|
566
|
+
const implSymbol = Object.getOwnPropertySymbols(Object.getOwnPropertyDescriptors(new window.Blob()))[0];
|
|
567
|
+
const utils = {
|
|
568
|
+
window,
|
|
569
|
+
makeCompatFormData(formData) {
|
|
570
|
+
const nodeFormData = new NodeFormData_();
|
|
571
|
+
formData.forEach((value, key) => {
|
|
572
|
+
if (value instanceof window.Blob) nodeFormData.append(key, utils.makeCompatBlob(value));
|
|
573
|
+
else nodeFormData.append(key, value);
|
|
468
574
|
});
|
|
575
|
+
return nodeFormData;
|
|
576
|
+
},
|
|
577
|
+
makeCompatBlob(blob) {
|
|
578
|
+
const buffer = blob[implSymbol]._buffer;
|
|
579
|
+
return new NodeBlob_([buffer], { type: blob.type });
|
|
469
580
|
}
|
|
470
|
-
}
|
|
581
|
+
};
|
|
582
|
+
return utils;
|
|
471
583
|
}
|
|
472
584
|
function patchAddEventListener(window) {
|
|
473
|
-
const JSDOMAbortSignal = window.AbortSignal
|
|
474
|
-
|
|
585
|
+
const JSDOMAbortSignal = window.AbortSignal;
|
|
586
|
+
const JSDOMAbortController = window.AbortController;
|
|
587
|
+
const originalAddEventListener = window.EventTarget.prototype.addEventListener;
|
|
588
|
+
window.EventTarget.prototype.addEventListener = function addEventListener(type, callback, options) {
|
|
475
589
|
if (typeof options === "object" && options.signal != null) {
|
|
476
590
|
const { signal,...otherOptions } = options;
|
|
477
591
|
// - this happens because AbortSignal is provided by Node.js,
|
|
@@ -484,13 +598,16 @@ function patchAddEventListener(window) {
|
|
|
484
598
|
// use jsdom-native abort controller instead and forward the
|
|
485
599
|
// previous one with `addEventListener`
|
|
486
600
|
const jsdomAbortController = new JSDOMAbortController();
|
|
487
|
-
|
|
601
|
+
signal.addEventListener("abort", () => {
|
|
488
602
|
jsdomAbortController.abort(signal.reason);
|
|
489
|
-
})
|
|
603
|
+
});
|
|
604
|
+
jsdomCompatOptions.signal = jsdomAbortController.signal;
|
|
605
|
+
return originalAddEventListener.call(this, type, callback, jsdomCompatOptions);
|
|
490
606
|
}
|
|
491
607
|
}
|
|
492
608
|
return originalAddEventListener.call(this, type, callback, options);
|
|
493
|
-
}
|
|
609
|
+
};
|
|
610
|
+
return () => {
|
|
494
611
|
window.EventTarget.prototype.addEventListener = originalAddEventListener;
|
|
495
612
|
};
|
|
496
613
|
}
|
|
@@ -503,10 +620,12 @@ const denyList = new Set([
|
|
|
503
620
|
"Buffer",
|
|
504
621
|
"ArrayBuffer",
|
|
505
622
|
"Uint8Array"
|
|
506
|
-
])
|
|
623
|
+
]);
|
|
624
|
+
const nodeGlobals = /* @__PURE__ */ new Map();
|
|
507
625
|
function populateNodeGlobals() {
|
|
508
626
|
if (nodeGlobals.size !== 0) return;
|
|
509
|
-
const names = Object.getOwnPropertyNames(globalThis)
|
|
627
|
+
const names = Object.getOwnPropertyNames(globalThis);
|
|
628
|
+
const length = names.length;
|
|
510
629
|
for (let i = 0; i < length; i++) {
|
|
511
630
|
const globalName = names[i];
|
|
512
631
|
if (!denyList.has(globalName)) {
|
|
@@ -522,7 +641,8 @@ var node = {
|
|
|
522
641
|
async setupVM() {
|
|
523
642
|
populateNodeGlobals();
|
|
524
643
|
const vm = await import('node:vm');
|
|
525
|
-
let context = vm.createContext()
|
|
644
|
+
let context = vm.createContext();
|
|
645
|
+
let global = vm.runInContext("this", context);
|
|
526
646
|
const contextGlobals = new Set(Object.getOwnPropertyNames(global));
|
|
527
647
|
for (const [nodeGlobalsKey, descriptor] of nodeGlobals) if (!contextGlobals.has(nodeGlobalsKey)) if (descriptor.configurable) Object.defineProperty(global, nodeGlobalsKey, {
|
|
528
648
|
configurable: true,
|
|
@@ -530,12 +650,14 @@ var node = {
|
|
|
530
650
|
get() {
|
|
531
651
|
// @ts-expect-error: no index signature
|
|
532
652
|
const val = globalThis[nodeGlobalsKey];
|
|
533
|
-
|
|
653
|
+
// override lazy getter
|
|
654
|
+
Object.defineProperty(global, nodeGlobalsKey, {
|
|
534
655
|
configurable: true,
|
|
535
656
|
enumerable: descriptor.enumerable,
|
|
536
657
|
value: val,
|
|
537
658
|
writable: descriptor.writable === true || nodeGlobalsKey === "performance"
|
|
538
|
-
})
|
|
659
|
+
});
|
|
660
|
+
return val;
|
|
539
661
|
},
|
|
540
662
|
set(val) {
|
|
541
663
|
// override lazy getter
|
|
@@ -559,17 +681,26 @@ var node = {
|
|
|
559
681
|
get: descriptor.get,
|
|
560
682
|
set: descriptor.set
|
|
561
683
|
});
|
|
562
|
-
|
|
684
|
+
global.global = global;
|
|
685
|
+
global.Buffer = Buffer;
|
|
686
|
+
global.ArrayBuffer = ArrayBuffer;
|
|
687
|
+
// TextEncoder (global or via 'util') references a Uint8Array constructor
|
|
688
|
+
// different than the global one used by users in tests. This makes sure the
|
|
689
|
+
// same constructor is referenced by both.
|
|
690
|
+
global.Uint8Array = Uint8Array;
|
|
691
|
+
return {
|
|
563
692
|
getVmContext() {
|
|
564
693
|
return context;
|
|
565
694
|
},
|
|
566
695
|
teardown() {
|
|
567
|
-
context = void 0
|
|
696
|
+
context = void 0;
|
|
697
|
+
global = void 0;
|
|
568
698
|
}
|
|
569
699
|
};
|
|
570
700
|
},
|
|
571
701
|
async setup(global) {
|
|
572
|
-
|
|
702
|
+
global.console.Console = Console;
|
|
703
|
+
return { teardown(global) {
|
|
573
704
|
delete global.console.Console;
|
|
574
705
|
} };
|
|
575
706
|
}
|