@vitest/web-worker 2.0.0-beta.1 → 2.0.0-beta.11
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 +61 -22
- 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
@@ -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
|
@@ -284,8 +286,11 @@ function getWorkerState() {
|
|
284
286
|
return globalThis.__vitest_worker__;
|
285
287
|
}
|
286
288
|
function assertGlobalExists(name) {
|
287
|
-
if (!(name in globalThis))
|
288
|
-
throw new Error(
|
289
|
+
if (!(name in globalThis)) {
|
290
|
+
throw new Error(
|
291
|
+
`[@vitest/web-worker] Cannot initiate a custom Web Worker. "${name}" is not supported in this environment. Please, consider using jsdom or happy-dom environment.`
|
292
|
+
);
|
293
|
+
}
|
289
294
|
}
|
290
295
|
function createClonedMessageEvent(data, transferOrOptions, clone) {
|
291
296
|
const transfer = Array.isArray(transferOrOptions) ? transferOrOptions : transferOrOptions?.transfer;
|
@@ -352,12 +357,15 @@ function stripProtocol(url) {
|
|
352
357
|
return url.toString().replace(/^file:\/+/, "/");
|
353
358
|
}
|
354
359
|
function getFileIdFromUrl(url) {
|
355
|
-
if (typeof self === "undefined")
|
360
|
+
if (typeof self === "undefined") {
|
356
361
|
return stripProtocol(url);
|
357
|
-
|
362
|
+
}
|
363
|
+
if (!(url instanceof URL)) {
|
358
364
|
url = new URL(url, self.location.origin);
|
359
|
-
|
365
|
+
}
|
366
|
+
if (url.protocol === "http:" || url.protocol === "https:") {
|
360
367
|
return url.pathname;
|
368
|
+
}
|
361
369
|
return stripProtocol(url);
|
362
370
|
}
|
363
371
|
|
@@ -384,15 +392,23 @@ function createWorkerConstructor(options) {
|
|
384
392
|
return this._vw_workerTarget.dispatchEvent(event);
|
385
393
|
},
|
386
394
|
addEventListener: (...args) => {
|
387
|
-
if (args[1])
|
395
|
+
if (args[1]) {
|
388
396
|
this._vw_insideListeners.set(args[0], args[1]);
|
397
|
+
}
|
389
398
|
return this._vw_workerTarget.addEventListener(...args);
|
390
399
|
},
|
391
400
|
removeEventListener: this._vw_workerTarget.removeEventListener,
|
392
401
|
postMessage: (...args) => {
|
393
|
-
if (!args.length)
|
394
|
-
throw new SyntaxError(
|
395
|
-
|
402
|
+
if (!args.length) {
|
403
|
+
throw new SyntaxError(
|
404
|
+
'"postMessage" requires at least one argument.'
|
405
|
+
);
|
406
|
+
}
|
407
|
+
debug(
|
408
|
+
"posting message %o from the worker %s to the main thread",
|
409
|
+
args[0],
|
410
|
+
this._vw_name
|
411
|
+
);
|
396
412
|
const event = createMessageEvent(args[0], args[1], cloneType());
|
397
413
|
this.dispatchEvent(event);
|
398
414
|
},
|
@@ -419,11 +435,18 @@ function createWorkerConstructor(options) {
|
|
419
435
|
this._vw_name = options2?.name ?? fsPath;
|
420
436
|
debug("initialize worker %s", this._vw_name);
|
421
437
|
return runner.executeFile(fsPath).then(() => {
|
422
|
-
runnerOptions.moduleCache.invalidateSubDepTree([
|
438
|
+
runnerOptions.moduleCache.invalidateSubDepTree([
|
439
|
+
fsPath,
|
440
|
+
runner.mocker.getMockPath(fsPath)
|
441
|
+
]);
|
423
442
|
const q = this._vw_messageQueue;
|
424
443
|
this._vw_messageQueue = null;
|
425
|
-
if (q)
|
426
|
-
q.forEach(
|
444
|
+
if (q) {
|
445
|
+
q.forEach(
|
446
|
+
([data, transfer]) => this.postMessage(data, transfer),
|
447
|
+
this
|
448
|
+
);
|
449
|
+
}
|
427
450
|
debug("worker %s successfully initialized", this._vw_name);
|
428
451
|
});
|
429
452
|
}).catch((e) => {
|
@@ -439,25 +462,36 @@ function createWorkerConstructor(options) {
|
|
439
462
|
});
|
440
463
|
}
|
441
464
|
addEventListener(type, callback, options2) {
|
442
|
-
if (callback)
|
465
|
+
if (callback) {
|
443
466
|
this._vw_outsideListeners.set(type, callback);
|
467
|
+
}
|
444
468
|
return super.addEventListener(type, callback, options2);
|
445
469
|
}
|
446
470
|
postMessage(...args) {
|
447
|
-
if (!args.length)
|
471
|
+
if (!args.length) {
|
448
472
|
throw new SyntaxError('"postMessage" requires at least one argument.');
|
473
|
+
}
|
449
474
|
const [data, transferOrOptions] = args;
|
450
475
|
if (this._vw_messageQueue != null) {
|
451
|
-
debug(
|
476
|
+
debug(
|
477
|
+
"worker %s is not yet initialized, queue message %s",
|
478
|
+
this._vw_name,
|
479
|
+
data
|
480
|
+
);
|
452
481
|
this._vw_messageQueue.push([data, transferOrOptions]);
|
453
482
|
return;
|
454
483
|
}
|
455
|
-
debug(
|
484
|
+
debug(
|
485
|
+
"posting message %o from the main thread to the worker %s",
|
486
|
+
data,
|
487
|
+
this._vw_name
|
488
|
+
);
|
456
489
|
const event = createMessageEvent(data, transferOrOptions, cloneType());
|
457
|
-
if (event.type === "messageerror")
|
490
|
+
if (event.type === "messageerror") {
|
458
491
|
this.dispatchEvent(event);
|
459
|
-
else
|
492
|
+
} else {
|
460
493
|
this._vw_workerTarget.dispatchEvent(event);
|
494
|
+
}
|
461
495
|
}
|
462
496
|
terminate() {
|
463
497
|
debug("terminating worker %s", this._vw_name);
|
@@ -494,10 +528,12 @@ function convertNodePortToWebPort(port) {
|
|
494
528
|
const emit = port.emit.bind(port);
|
495
529
|
Object.defineProperty(port, "emit", {
|
496
530
|
value(event) {
|
497
|
-
if (event.name === "message")
|
531
|
+
if (event.name === "message") {
|
498
532
|
port.onmessage?.(event);
|
499
|
-
|
533
|
+
}
|
534
|
+
if (event.name === "messageerror") {
|
500
535
|
port.onmessageerror?.(event);
|
536
|
+
}
|
501
537
|
return emit(event);
|
502
538
|
},
|
503
539
|
configurable: true,
|
@@ -556,7 +592,10 @@ function createSharedWorkerConstructor() {
|
|
556
592
|
this._vw_name = name ?? fsPath;
|
557
593
|
debug("initialize shared worker %s", this._vw_name);
|
558
594
|
return runner.executeFile(fsPath).then(() => {
|
559
|
-
runnerOptions.moduleCache.invalidateSubDepTree([
|
595
|
+
runnerOptions.moduleCache.invalidateSubDepTree([
|
596
|
+
fsPath,
|
597
|
+
runner.mocker.getMockPath(fsPath)
|
598
|
+
]);
|
560
599
|
this._vw_workerTarget.dispatchEvent(
|
561
600
|
new MessageEvent("connect", {
|
562
601
|
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.0-beta.
|
4
|
+
"version": "2.0.0-beta.11",
|
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",
|