Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: Worker class of jest-worker does not support ESM worker modules #12274

Closed
mrazauskas opened this issue Jan 28, 2022 · 7 comments
Closed

Comments

@mrazauskas
Copy link
Contributor

Version

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

https://github.com/facebook/jest/blob/d1bc333c549ffb53691e1ba68b16a21915fc5fdc/packages/jest-worker/src/workers/processChild.ts#L96-L97

https://github.com/facebook/jest/blob/d1bc333c549ffb53691e1ba68b16a21915fc5fdc/packages/jest-worker/src/index.ts#L32-L33

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
@mrazauskas
Copy link
Contributor Author

@SimenB Would it be possible to include this issue in Jest 28 milestone? I really need it for one work-in-progress test runner (;

Might be there is better solution, but it seemed like having async createWorker('path/toWorker') factory instead of new Worker('path/toWorker') would be enough. Perhaps this breaking change could be introduced now and the real ESM support could be implemented later.

The problem is that constructor of the class cannot be asynchronous. Async createWorker() makes it possible to await import() the worker module in order to detect exported methods.

@SimenB
Copy link
Member

SimenB commented Feb 25, 2022

@SimenB Does it look acceptable for you? Feels like I could solve it this way.

yes! factory pattern is how we've solved other "class does require in constructor, we need await import"

@SimenB SimenB added this to the Jest 28 milestone Feb 25, 2022
@SimenB
Copy link
Member

SimenB commented Apr 8, 2022

@mrazauskas wanna send a PR for this one? 🙂

@mrazauskas
Copy link
Contributor Author

Yes, I would love to. Next week (;

@SimenB
Copy link
Member

SimenB commented Apr 8, 2022

yay!

@SimenB SimenB modified the milestones: Jest 28, High priority future Apr 26, 2022
ivangabriele added a commit to betagouv/metiers-numeriques-pep that referenced this issue Apr 29, 2022
This step required to use CommonJS instead of ESM since neither bull nor
jest-worker support it.

See:
- jestjs/jest#12274
- OptimalBits/bull#924
@mrazauskas
Copy link
Contributor Author

This is so old. Probably nobody needs it anymore. Closing.

@mrazauskas mrazauskas closed this as not planned Won't fix, can't repro, duplicate, stale Sep 10, 2024
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 11, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants