jsdom-worker

在 Jest / JSDOM 中使用 Web Worker。「👷‍♀️ Use Web Workers in Jest / JSDOM 🌈」

Github stars Tracking Chart

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

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 Jest config option:

{
  "setupFiles": [
    "jsdom-worker"
  ]
}

License

MIT License

Main metrics

Overview
Name With Ownerdevelopit/jsdom-worker
Primary LanguageJavaScript
Program languageJavaScript (Language Count: 1)
Platform
License:
所有者活动
Created At2018-01-31 20:44:22
Pushed At2023-04-06 08:37:17
Last Commit At
Release Count4
Last Release Name0.3.0 (Posted on )
First Release Name0.1.0 (Posted on )
用户参与
Stargazers Count289
Watchers Count7
Fork Count20
Commits Count26
Has Issues Enabled
Issues Count17
Issue Open Count10
Pull Requests Count6
Pull Requests Open Count2
Pull Requests Close Count1
项目设置
Has Wiki Enabled
Is Archived
Is Fork
Is Locked
Is Mirror
Is Private