⚠WARNING⚠: This package is currently experimental. Please avoid using it in production.
A Jest transformer which can execute WebWorker codes that uses WebPack's worker-loader.
This is an alternative implementation of workerloader-jest-transformer. This library aims to enable WebWorker codes run in parallel in Jest environments (where workerloader-jest-transformer cannot).
$ npm install -D jest-webworker- Add
jest-webworker/@typestocompilerOptions.typeRootsin yourtsconfig.json. - Rename your worker source file to
*.worker.ts. - Change your
importstatement in your worker launching code as below:
import HogeWorker from './hoge.worker.ts';Note: please avoid using worker-loader! prefix since it cannot be recognized by Jest.
- Add the following comment between your
importstatements and source code body:
/* __JEST_WEBWORKER_SEPARATOR__ */- Run jest and enjoy testing!
Please take a look at test codes inside the ./__tests__ directory for more detailed usage.
The workerloader-jest-transformer package is considerably a nobel implementation of WebPack's WebWorker transformer, however, workerloader-jest-transformer launches Workers in the main Node.js thread and hence cannot be parallelized in practice. This will reduce the test performance for WebWorker codes which launches multiple WebWorker threads and process data in parallel.
This package, on the other hand, launches Worker threads using Node.js's worker_threads feature and
executes WebWorkers in a thread other than the main thread.
The performance improvements can be measured by running the fibonacci test
(computes the 40-th fibonacci number using recurrsion = slow implementation, in purpose).
$ npm t fibo
> jest-webworker@0.0.1 test
> jest "fibo"
PASS __tests__/fibonacci.test.ts (11.077 s)
FibonacciWorker
✓ can execute in parallel (2276 ms)
✓ can execute in serial (7882 ms)
Test Suites: 1 passed, 1 total
Tests: 2 passed, 2 total
Snapshots: 0 total
Time: 11.131 s
Ran all test suites matching /fibo/i.On my desktop machine with 4 CPU cores can execute the test laughly x3.46 faster than the serialized case (see above).
Any contribution, including bug reports, comments, suggenstions and / or pull requests are acceptable 🍻.