- Notifications
You must be signed in to change notification settings - Fork20
👷♀️ Use Web Workers in Jest / JSDOM 🌈
developit/jsdom-worker
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
Lets you use Web Workers in Jest!
This is an experimental implementation of the Web Worker API (specifically Dedicated Worker) for JSDOM.
It does not currently do any real threading, rather it implements theWorker interface but all work is done in the current thread.jsdom-worker runs wherever JSDOM runs, and does not require Node.
It supports both "inline"(created via Blob) and standard(loaded via URL) workers.
Hot Take: this module likely works in the browser, where it could act as a simple inline worker "poorlyfill".
Jest uses a JSDOM environment by default, which means it doesn't support Workers. This means it is impossible to test code that requires both NodeJS functionalityand Web Workers.jsdom-worker implements enough of the Worker spec that it is now possible to do so.
npm i jsdom-worker
import'jsdom-global/register';import'jsdom-worker';letcode=`onmessage = e => postMessage(e.data*2)`;letworker=newWorker(URL.createObjectURL(newBlob([code])));worker.onmessage=console.log;worker.postMessage(5);// 10
For single tests, simply addimport 'jsdom-worker' to your module.
Otherwise, add it via thesetupFiles Jest config option:
{"setupFiles":["jsdom-worker"]}
About
👷♀️ Use Web Workers in Jest / JSDOM 🌈
Topics
Resources
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors4
Uh oh!
There was an error while loading.Please reload this page.
