Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

jsdom-worker

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

jsdom-worker

Experimental Web Worker API implementation for JSDOM.

  • 0.3.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

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".

npm travis

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

Keywords

FAQs

Package last updated on 29 Aug 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc