@vitest/web-worker does not structurally clone its postMessage message
argument
#2399
Closed
6 tasks done
Labels
message
argument
#2399
Describe the bug
First, thanks for this package -- it's been super helpful for me.
Second, when passing values to a worker's
postMessage()
, the specification states that they are copied via a structural clone algorithm. (Spec). This only really matters when passing non-primitive values, e.g.{ prop: value }
.At present, this wrapper does not clone the data, meaning that if the Worker mutates the data directly, it gets reflected in the main thread's data. The fix is relatively simple:
https://github.com/vitest-dev/vitest/blob/main/packages/web-worker/src/pure.ts#L35
Instead of
I imagine something like this would do the trick:
(there is also a whole thing about
transfers
of the properties, but that shouldn't be necessary in normal testing scenarios).structuredClone
is available in Node 17, but it seems Vite's compatibility starts with Node 14? I'm not an expert in making build systems play nice with polyfills (otherwise I would have just filed the PR...): https://developer.mozilla.org/en-US/docs/Web/API/structuredCloneReproduction
My first time sharing something on stackblitz... hope it works.
worker.js
is just the barebones worker, and the testsuite illustrates the copying problem.https://stackblitz.com/edit/vitest-dev-vitest-e5mxcm
System Info
Used Package Manager
npm
Validations
The text was updated successfully, but these errors were encountered: