question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:1
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
SimenBcommented, 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

0reactions
SimenBcommented, Apr 8, 2022

yay!

Read more comments on GitHub >

github_iconTop Results From Across the Web

how solve error jest-worker at run build? - Stack Overflow
i have a problem at run build production in webpack. but when i run using dev-server, more code can't get error. please help.....
Read more >
jest-worker | Yarn - Package Manager
Module for executing heavy tasks under forked processes in parallel, by providing a Promise based interface, minimum overhead, and bound workers. The module...
Read more >
Using worker_threads in Node.js - Medium
This is a beginner's guide to using worker_threads in Node.js. It does not assume you already understand Web Workers. (The API for worker_threads...
Read more >
babel-jest - Awesome JS
[jest-config] Do not warn about preset in ProjectConfig #13583 ... can be mocked (#13513); [jest-worker] Throw an error instead of hanging when jest...
Read more >
Configuring Jest
The Jest philosophy is to work great by default, but sometimes you just need ... Node.js core modules, like fs , are not...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found