@vitest/web-worker 2.0.0-beta.9 → 2.0.1
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/README.md +2 -1
- package/dist/pure.js +63 -23
- package/package.json +2 -2
package/README.md
CHANGED
@@ -45,8 +45,9 @@ You can also import `defineWebWorkers` from `@vitest/web-worker/pure` to define
|
|
45
45
|
```js
|
46
46
|
import { defineWebWorkers } from '@vitest/web-worker/pure'
|
47
47
|
|
48
|
-
if (process.env.SUPPORT_WORKERS)
|
48
|
+
if (process.env.SUPPORT_WORKERS) {
|
49
49
|
defineWebWorkers({ clone: 'none' })
|
50
|
+
}
|
50
51
|
```
|
51
52
|
|
52
53
|
It accepts options:
|
package/dist/pure.js
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
import { VitestExecutor } from 'vitest/execute';
|
2
|
-
import { readFileSync } from 'node:fs';
|
2
|
+
import { readFileSync as readFileSync$1 } from 'node:fs';
|
3
3
|
import createDebug from 'debug';
|
4
4
|
import { MessageChannel } from 'node:worker_threads';
|
5
5
|
|
@@ -11,7 +11,9 @@ class InlineWorkerRunner extends VitestExecutor {
|
|
11
11
|
prepareContext(context) {
|
12
12
|
const ctx = super.prepareContext(context);
|
13
13
|
const importScripts = () => {
|
14
|
-
throw new Error(
|
14
|
+
throw new Error(
|
15
|
+
"[vitest] `importScripts` is not supported in Vite workers. Please, consider using `import` instead."
|
16
|
+
);
|
15
17
|
};
|
16
18
|
return Object.assign(ctx, this.context, {
|
17
19
|
importScripts
|
@@ -279,13 +281,17 @@ var ponyfillStructuredClone = typeof structuredClone === "function" ?
|
|
279
281
|
) :
|
280
282
|
(any, options) => deserialize(serialize(any, options));
|
281
283
|
|
284
|
+
const readFileSync = readFileSync$1;
|
282
285
|
const debug = createDebug("vitest:web-worker");
|
283
286
|
function getWorkerState() {
|
284
287
|
return globalThis.__vitest_worker__;
|
285
288
|
}
|
286
289
|
function assertGlobalExists(name) {
|
287
|
-
if (!(name in globalThis))
|
288
|
-
throw new Error(
|
290
|
+
if (!(name in globalThis)) {
|
291
|
+
throw new Error(
|
292
|
+
`[@vitest/web-worker] Cannot initiate a custom Web Worker. "${name}" is not supported in this environment. Please, consider using jsdom or happy-dom environment.`
|
293
|
+
);
|
294
|
+
}
|
289
295
|
}
|
290
296
|
function createClonedMessageEvent(data, transferOrOptions, clone) {
|
291
297
|
const transfer = Array.isArray(transferOrOptions) ? transferOrOptions : transferOrOptions?.transfer;
|
@@ -352,12 +358,15 @@ function stripProtocol(url) {
|
|
352
358
|
return url.toString().replace(/^file:\/+/, "/");
|
353
359
|
}
|
354
360
|
function getFileIdFromUrl(url) {
|
355
|
-
if (typeof self === "undefined")
|
361
|
+
if (typeof self === "undefined") {
|
356
362
|
return stripProtocol(url);
|
357
|
-
|
363
|
+
}
|
364
|
+
if (!(url instanceof URL)) {
|
358
365
|
url = new URL(url, self.location.origin);
|
359
|
-
|
366
|
+
}
|
367
|
+
if (url.protocol === "http:" || url.protocol === "https:") {
|
360
368
|
return url.pathname;
|
369
|
+
}
|
361
370
|
return stripProtocol(url);
|
362
371
|
}
|
363
372
|
|
@@ -384,15 +393,23 @@ function createWorkerConstructor(options) {
|
|
384
393
|
return this._vw_workerTarget.dispatchEvent(event);
|
385
394
|
},
|
386
395
|
addEventListener: (...args) => {
|
387
|
-
if (args[1])
|
396
|
+
if (args[1]) {
|
388
397
|
this._vw_insideListeners.set(args[0], args[1]);
|
398
|
+
}
|
389
399
|
return this._vw_workerTarget.addEventListener(...args);
|
390
400
|
},
|
391
401
|
removeEventListener: this._vw_workerTarget.removeEventListener,
|
392
402
|
postMessage: (...args) => {
|
393
|
-
if (!args.length)
|
394
|
-
throw new SyntaxError(
|
395
|
-
|
403
|
+
if (!args.length) {
|
404
|
+
throw new SyntaxError(
|
405
|
+
'"postMessage" requires at least one argument.'
|
406
|
+
);
|
407
|
+
}
|
408
|
+
debug(
|
409
|
+
"posting message %o from the worker %s to the main thread",
|
410
|
+
args[0],
|
411
|
+
this._vw_name
|
412
|
+
);
|
396
413
|
const event = createMessageEvent(args[0], args[1], cloneType());
|
397
414
|
this.dispatchEvent(event);
|
398
415
|
},
|
@@ -419,11 +436,18 @@ function createWorkerConstructor(options) {
|
|
419
436
|
this._vw_name = options2?.name ?? fsPath;
|
420
437
|
debug("initialize worker %s", this._vw_name);
|
421
438
|
return runner.executeFile(fsPath).then(() => {
|
422
|
-
runnerOptions.moduleCache.invalidateSubDepTree([
|
439
|
+
runnerOptions.moduleCache.invalidateSubDepTree([
|
440
|
+
fsPath,
|
441
|
+
runner.mocker.getMockPath(fsPath)
|
442
|
+
]);
|
423
443
|
const q = this._vw_messageQueue;
|
424
444
|
this._vw_messageQueue = null;
|
425
|
-
if (q)
|
426
|
-
q.forEach(
|
445
|
+
if (q) {
|
446
|
+
q.forEach(
|
447
|
+
([data, transfer]) => this.postMessage(data, transfer),
|
448
|
+
this
|
449
|
+
);
|
450
|
+
}
|
427
451
|
debug("worker %s successfully initialized", this._vw_name);
|
428
452
|
});
|
429
453
|
}).catch((e) => {
|
@@ -439,25 +463,36 @@ function createWorkerConstructor(options) {
|
|
439
463
|
});
|
440
464
|
}
|
441
465
|
addEventListener(type, callback, options2) {
|
442
|
-
if (callback)
|
466
|
+
if (callback) {
|
443
467
|
this._vw_outsideListeners.set(type, callback);
|
468
|
+
}
|
444
469
|
return super.addEventListener(type, callback, options2);
|
445
470
|
}
|
446
471
|
postMessage(...args) {
|
447
|
-
if (!args.length)
|
472
|
+
if (!args.length) {
|
448
473
|
throw new SyntaxError('"postMessage" requires at least one argument.');
|
474
|
+
}
|
449
475
|
const [data, transferOrOptions] = args;
|
450
476
|
if (this._vw_messageQueue != null) {
|
451
|
-
debug(
|
477
|
+
debug(
|
478
|
+
"worker %s is not yet initialized, queue message %s",
|
479
|
+
this._vw_name,
|
480
|
+
data
|
481
|
+
);
|
452
482
|
this._vw_messageQueue.push([data, transferOrOptions]);
|
453
483
|
return;
|
454
484
|
}
|
455
|
-
debug(
|
485
|
+
debug(
|
486
|
+
"posting message %o from the main thread to the worker %s",
|
487
|
+
data,
|
488
|
+
this._vw_name
|
489
|
+
);
|
456
490
|
const event = createMessageEvent(data, transferOrOptions, cloneType());
|
457
|
-
if (event.type === "messageerror")
|
491
|
+
if (event.type === "messageerror") {
|
458
492
|
this.dispatchEvent(event);
|
459
|
-
else
|
493
|
+
} else {
|
460
494
|
this._vw_workerTarget.dispatchEvent(event);
|
495
|
+
}
|
461
496
|
}
|
462
497
|
terminate() {
|
463
498
|
debug("terminating worker %s", this._vw_name);
|
@@ -494,10 +529,12 @@ function convertNodePortToWebPort(port) {
|
|
494
529
|
const emit = port.emit.bind(port);
|
495
530
|
Object.defineProperty(port, "emit", {
|
496
531
|
value(event) {
|
497
|
-
if (event.name === "message")
|
532
|
+
if (event.name === "message") {
|
498
533
|
port.onmessage?.(event);
|
499
|
-
|
534
|
+
}
|
535
|
+
if (event.name === "messageerror") {
|
500
536
|
port.onmessageerror?.(event);
|
537
|
+
}
|
501
538
|
return emit(event);
|
502
539
|
},
|
503
540
|
configurable: true,
|
@@ -556,7 +593,10 @@ function createSharedWorkerConstructor() {
|
|
556
593
|
this._vw_name = name ?? fsPath;
|
557
594
|
debug("initialize shared worker %s", this._vw_name);
|
558
595
|
return runner.executeFile(fsPath).then(() => {
|
559
|
-
runnerOptions.moduleCache.invalidateSubDepTree([
|
596
|
+
runnerOptions.moduleCache.invalidateSubDepTree([
|
597
|
+
fsPath,
|
598
|
+
runner.mocker.getMockPath(fsPath)
|
599
|
+
]);
|
560
600
|
this._vw_workerTarget.dispatchEvent(
|
561
601
|
new MessageEvent("connect", {
|
562
602
|
ports: [this._vw_workerPort]
|
package/package.json
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
{
|
2
2
|
"name": "@vitest/web-worker",
|
3
3
|
"type": "module",
|
4
|
-
"version": "2.0.
|
4
|
+
"version": "2.0.1",
|
5
5
|
"description": "Web Worker support for testing in Vitest",
|
6
6
|
"license": "MIT",
|
7
7
|
"funding": "https://opencollective.com/vitest",
|
@@ -36,7 +36,7 @@
|
|
36
36
|
"vitest": "^1.0.0"
|
37
37
|
},
|
38
38
|
"dependencies": {
|
39
|
-
"debug": "^4.3.
|
39
|
+
"debug": "^4.3.5"
|
40
40
|
},
|
41
41
|
"devDependencies": {
|
42
42
|
"@types/debug": "^4.1.12",
|