@vitest/web-worker
TypeScript icon, indicating that this package has built-in type declarations

2.1.6 • Public • Published

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

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
2.1.6
550latest
2.2.0-beta.2
10beta

Version History

VersionDownloads (Last 7 Days)Published
2.1.6
550
2.2.0-beta.2
10
2.2.0-beta.1
5
2.1.5
6,622
2.1.4
3,604
2.1.3
741
2.1.2
1,178
2.1.1
1,680
2.1.0
2
2.1.0-beta.7
0
2.1.0-beta.6
0
2.1.0-beta.5
0
2.1.0-beta.4
0
2.1.0-beta.3
0
2.1.0-beta.2
0
2.1.0-beta.1
0
2.0.5
3,351
2.0.4
1
2.0.3
22
2.0.2
90
2.0.1
1
2.0.0
0
2.0.0-beta.13
0
2.0.0-beta.12
0
2.0.0-beta.11
0
2.0.0-beta.10
0
2.0.0-beta.9
0
2.0.0-beta.8
0
2.0.0-beta.7
0
2.0.0-beta.6
0
2.0.0-beta.5
0
2.0.0-beta.4
0
2.0.0-beta.3
0
2.0.0-beta.2
0
2.0.0-beta.1
0
1.6.0
7,616
1.5.3
28
1.5.2
0
1.5.1
0
1.5.0
57
1.4.0
765
1.3.1
472
1.3.00
1.2.214
1.2.11
1.2.08
1.1.330
1.1.20
1.1.133
1.1.00
1.0.45
1.0.30
1.0.20
1.0.10
1.0.00
1.0.0-beta.60
1.0.0-beta.50
1.0.0-beta.40
1.0.0-beta.30
1.0.0-beta.20
1.0.0-beta.10
0.34.74,181
1.0.0-beta.00
0.34.6825
0.34.50
0.34.41
0.34.30
0.34.22
0.34.11
0.34.02
0.33.0242
0.32.41
0.32.32
0.32.21
0.32.10
0.32.00
0.31.42
0.31.30
0.31.20
0.31.10
0.31.08
0.30.153
0.30.00
0.29.80
0.29.70
0.29.60
0.29.51
0.29.40
0.29.30
0.29.21
0.29.10
0.29.01
0.28.523
0.28.41
0.28.31
0.28.20
0.28.10
0.28.00
0.27.36
0.27.21
0.27.10
0.27.00
0.26.35
0.26.21
0.26.00
0.25.80
0.25.71
0.25.60
0.25.51
0.25.41
0.25.31
0.25.21
0.25.11
0.25.00
0.24.512
0.24.40
0.24.30
0.24.20
0.24.10
0.24.00
0.23.4163
0.23.20
0.23.10
0.23.00
0.22.11
0.22.00
0.21.10
0.21.00
0.20.35
0.20.21
0.20.10
0.20.00
0.19.10
0.19.00
0.18.10
0.18.00
0.17.10
0.17.00
0.16.00
0.15.20
0.15.11
0.15.00
0.14.20
0.14.10
0.14.00
0.13.10
0.13.00
0.12.1034
0.12.90
0.12.81
0.12.71
0.12.60
0.12.50
0.12.40
0.12.30
0.12.20
0.12.11
0.12.00
0.11.00
0.10.50
0.10.40
0.10.30
0.10.20
0.10.11
0.10.00
0.9.40
0.9.30
0.9.20
0.9.10
0.9.00
0.8.50
0.8.40
0.8.30
0.8.20
0.8.10
0.8.00
0.7.130
0.7.120
0.7.110
0.7.100
0.7.90
0.7.80
0.7.70
0.7.60
0.7.50
0.7.40
0.7.30
0.7.20
0.7.11
0.7.00

Package Sidebar

Install

npm i @vitest/web-worker

Weekly Downloads

32,476

Version

2.1.6

License

MIT

Unpacked Size

25.7 kB

Total Files

8

Last publish

Collaborators

  • antfu
  • patak
  • oreanno
  • vitestbot