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

@vitest/web-worker

Package Overview
Dependencies
Maintainers
4
Versions
194
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@vitest/web-worker

Web Worker support for testing in Vitest

  • 2.1.8
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
29K
decreased by-20.88%
Maintainers
4
Weekly downloads
 
Created
Source

@vitest/web-worker

Web Worker support for Vitest testing. Doesn't require JSDom.

Simulates Web Worker, but in the same thread.

Supported:

  • new Worker(path)
  • new SharedWorker(path)
  • import MyWorker from './worker?worker'
  • import MySharedWorker from './worker?sharedworker'

Installing

# with npm
npm install -D @vitest/web-worker

# with pnpm
pnpm install -D @vitest/web-worker

# with yarn
yarn add --dev @vitest/web-worker

Usage

Just import @vitest/web-worker in your test file to test only in current suite.

Or add @vitest/web-worker in your setupFiles, if you want to have a global support.

import { defineConfig } from 'vitest/node'

export default defineConfig({
  test: {
    setupFiles: ['@vitest/web-worker'],
  },
})

You can also import defineWebWorkers from @vitest/web-worker/pure to define workers, whenever you need:

import { defineWebWorkers } from '@vitest/web-worker/pure'

if (process.env.SUPPORT_WORKERS) {
  defineWebWorkers({ clone: 'none' })
}

It accepts options:

  • clone: 'native' | 'ponyfill' | 'none'. Defines how should Worker clone message, when transferring data. Applies only to Worker communication. SharedWorker uses MessageChannel from Node's worker_threads module, and is not configurable.

Note Requires Node 17, if you want to use native structuredClone. Otherwise, it fallbacks to polyfill, if not specified as none. You can also configure this option with VITEST_WEB_WORKER_CLONE environmental variable.

Examples

// worker.ts
self.onmessage = (e) => {
  self.postMessage(`${e.data} world`)
}
// worker.test.ts
import '@vitest/web-worker'
import MyWorker from '../worker?worker'

let worker = new MyWorker()
// new Worker is also supported
worker = new Worker(new URL('../src/worker.ts', import.meta.url))

worker.postMessage('hello')
worker.onmessage = (e) => {
  // e.data equals to 'hello world'
}

Notes

  • Worker does not support onmessage = () => {}. Please, use self.onmessage = () => {}.
  • Shared worker does not support onconnect = () => {}. Please, use self.onconnect = () => {}.
  • Transferring Buffer will not change its byteLength.
  • You have access to shared global space as your tests.
  • You can debug your worker, using DEBUG=vitest:web-worker environmental variable.

FAQs

Package last updated on 02 Dec 2024

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