[Bug]: `Worker` class of `jest-worker` does not support ESM worker modules
See original GitHub issueVersion
v27.4.7
Steps to reproduce
An exception is raised, if I try to initialise a worker with an ESM module: new Worker('path/to/ESMworker.mjs')
. Looks like jest-worker
is simply trying to require
the module and that does not work:
https://github.com/facebook/jest/blob/d1bc333c549ffb53691e1ba68b16a21915fc5fdc/packages/jest-worker/src/workers/processChild.ts#L113-L114
I was trying to refactor the code using the requireOrImportModule
from jest-utils
. Unfortunately the private method this._bindExposedWorkerMethods()
of the Worker
class has to become async, but it is called in the constructor. Async calls are not possible in constructors.
Good news: Worker
is a named export. If I get it right, this makes it possible to export something else from jest-worker
. So perhaps an asynchronous createWorker()
factory function could be a non-breaking solution?
import {createWorker} from 'jest-worker';
const workerPath = new URL('./testWorker.js', import.meta.url).pathname;
const worker = await createWorker(workerPath);
@SimenB Does it look acceptable for you? Feels like I could solve it this way.
Expected behavior
Would be great to support ESM worker modules in jest-worker
package.
Actual behavior
jest-worker
package does not support ESM worker modules.
Additional context
No response
Environment
System:
OS: macOS 10.15.7
CPU: (8) x64 Intel(R) Core(TM) i7-3615QM CPU @ 2.30GHz
Binaries:
Node: 17.3.1 - ~/.nvm/versions/node/v17.3.1/bin/node
Yarn: 2.4.3 - /usr/local/opt/node@16/bin/yarn
npm: 8.3.0 - ~/.nvm/versions/node/v17.3.1/bin/npm
npmPackages:
jest: workspace:* => 27.4.7
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:5 (2 by maintainers)
Top GitHub Comments
yes! factory pattern is how we’ve solved other “class does
require
in constructor, we needawait import
”yay!