Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/developit/jsdom-worker
👷♀️ Use Web Workers in Jest / JSDOM 🌈
https://github.com/developit/jsdom-worker
jest jest-environment jest-plugin jsdom web-worker webworker
Last synced: 4 days ago
JSON representation
👷♀️ Use Web Workers in Jest / JSDOM 🌈
- Host: GitHub
- URL: https://github.com/developit/jsdom-worker
- Owner: developit
- Created: 2018-01-31T20:44:22.000Z (almost 7 years ago)
- Default Branch: main
- Last Pushed: 2023-04-06T08:37:17.000Z (over 1 year ago)
- Last Synced: 2024-12-23T06:06:46.856Z (11 days ago)
- Topics: jest, jest-environment, jest-plugin, jsdom, web-worker, webworker
- Language: JavaScript
- Homepage: https://npm.im/jsdom-worker
- Size: 199 KB
- Stars: 287
- Watchers: 8
- Forks: 20
- Open Issues: 12
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# jsdom-worker
> _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 the `Worker` 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".
## Why?
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 functionality _and_ Web Workers. `jsdom-worker` implements enough of the Worker spec that it is now possible to do so.
## Installation
`npm i jsdom-worker`
## Example
```js
import 'jsdom-global/register';
import 'jsdom-worker';let code = `onmessage = e => postMessage(e.data*2)`;
let worker = new Worker(URL.createObjectURL(new Blob([code])));
worker.onmessage = console.log;
worker.postMessage(5); // 10
```## Usage with Jest
For single tests, simply add `import 'jsdom-worker'` to your module.
Otherwise, add it via the [setupFiles](https://facebook.github.io/jest/docs/en/configuration.html#setupfiles-array) Jest config option:
```js
{
"setupFiles": [
"jsdom-worker"
]
}
```## License
[MIT License](https://oss.ninja/mit/developit)