Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
About the context
Vite provides a very convinient way to import workers. But I cannot use it, since jest does not support it. The beauty of Vite's workers is, it basically bundles the worker to its own file, which allows me to split my worker into multiple files and can happily do imports :)
Okay. After doing some configuration, I was able to emulate vites behavior in Jest, except the "multi-file"-thing. But since this lib does not run a real worker but just emulates it in the jsdom environment, we can make of jests imports. In order to get everything running, I required the following config:
Now I am able to import my worker within my spec's as
import Worker from 'background.worker.ts?worker'
. Alrighty, so far so good, as long I do not import/require anything (in my configuration allimport
s becamerequire
s afterswcTransform.process
).Solution
Everything I needed was the
require
/exports
members in my worker scope. Having this allows me to emulate vite worker imports using jest working perfectly fine.