jsdom-worker

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

Github星跟蹤圖

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

主要指標

概覽
名稱與所有者developit/jsdom-worker
主編程語言JavaScript
編程語言JavaScript (語言數: 1)
平台
許可證
所有者活动
創建於2018-01-31 20:44:22
推送於2023-04-06 08:37:17
最后一次提交
發布數4
最新版本名稱0.3.0 (發布於 )
第一版名稱0.1.0 (發布於 )
用户参与
星數289
關注者數7
派生數20
提交數26
已啟用問題?
問題數17
打開的問題數10
拉請求數6
打開的拉請求數2
關閉的拉請求數1
项目设置
已啟用Wiki?
已存檔?
是復刻?
已鎖定?
是鏡像?
是私有?