Skip to content

Convert worker pool modules to ESM#6015

Open
macayu17 wants to merge 2 commits into
mochajs:mainfrom
macayu17:fix/5969-nodejs-worker-pool-esm
Open

Convert worker pool modules to ESM#6015
macayu17 wants to merge 2 commits into
mochajs:mainfrom
macayu17:fix/5969-nodejs-worker-pool-esm

Conversation

@macayu17

Copy link
Copy Markdown

PR Checklist

Overview

Converts the lib/nodejs worker pool modules from CommonJS to ESM.

This updates the worker pool, parallel runner, worker entrypoint, and buffered worker reporter to use .mjs files with named runtime exports. The worker setup now lives behind a small injectable helper so the unit tests no longer need to mock CommonJS loading for these modules.

Validated with the relevant node, integration, lint, typecheck, build, and browser test scripts.

Copilot AI review requested due to automatic review settings May 24, 2026 17:13

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

This PR migrates Mocha’s parallel-mode worker/reporting internals toward ESM (.mjs) and improves unit-testability by extracting injectable “core” factories, replacing rewiremock usage in tests.

Changes:

  • Added ESM worker entrypoint (worker.mjs) and extracted worker runtime logic into worker-core.mjs with dependency injection.
  • Added ESM implementations/factories for buffered-worker-pool and parallel-buffered reporter; updated unit tests to use these factories directly.
  • Updated parallel runner/test wiring and package.json browser exclusions to reference new .mjs entrypoints.

Reviewed changes

Copilot reviewed 15 out of 15 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
test/node-unit/worker.spec.js Switches worker tests from rewiremock to startWorker() with injected deps.
test/node-unit/reporters/parallel-buffered.spec.js Switches reporter tests from rewiremock to createParallelBufferedClass() and real serializer types.
test/node-unit/parallel-buffered-runner.spec.js Removes rewiremock, stubs BufferedWorkerPool.create, adjusts fatal error expectation.
test/node-unit/mocha.spec.js Updates stubbed runner path to .mjs + named export.
test/node-unit/buffered-worker-pool.spec.js Uses createBufferedWorkerPoolClass() instead of rewiremock.
package.json Updates browser field exclusions to .mjs parallel/worker paths.
lib/nodejs/worker.mjs New ESM worker entrypoint delegating to startWorker().
lib/nodejs/worker.js Removes old CJS worker implementation.
lib/nodejs/worker-core.mjs New extracted worker runtime with injectable dependencies + startWorker().
lib/nodejs/reporters/parallel-buffered.mjs New ESM reporter + factory to inject Base/serializer types.
lib/nodejs/reporters/parallel-buffered.js Removes old CJS reporter implementation.
lib/nodejs/parallel-buffered-runner.js Converts runner implementation to ESM-style imports/exports (but retains .js filename).
lib/nodejs/buffered-worker-pool.mjs New ESM workerpool wrapper + injectable factory.
lib/nodejs/buffered-worker-pool.js Removes old CJS workerpool wrapper.
lib/mocha.js Updates parallel runner resolution to point at .mjs named export.

Comment thread lib/mocha.js
// swap Runner class
this._runnerClass = parallel
? require("./nodejs/parallel-buffered-runner")
? require("./nodejs/parallel-buffered-runner.mjs").ParallelBufferedRunner
/**
* A mapping of Mocha `Options` objects to serialized values.
*
* This is helpful because we tend to same the same options over and over
Comment on lines +68 to +72
* Runs a single test file in a worker thread.
* @param {string} filepath - Filepath of test file
* @param {string} [serializedOptions] - **Serialized** options. This string will be eval'd!
* @see https://npm.im/serialize-javascript
* @returns {Promise<{failures: number, events: BufferedEvent[]}>} - Test
if (!filepath) {
throw createInvalidArgumentTypeError(
'Expected a non-empty "filepath" argument',
"file",
@codecov

codecov Bot commented May 25, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 92.46575% with 11 lines in your changes missing coverage. Please review.
✅ Project coverage is 80.16%. Comparing base (6695fba) to head (8101ac0).

Files with missing lines Patch % Lines
lib/nodejs/worker-core.mjs 90.16% 6 Missing ⚠️
lib/nodejs/buffered-worker-pool.mjs 93.18% 2 Missing and 1 partial ⚠️
lib/nodejs/worker.mjs 0.00% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #6015      +/-   ##
==========================================
- Coverage   80.89%   80.16%   -0.74%     
==========================================
  Files          64       65       +1     
  Lines        4602     4624      +22     
  Branches      976     1016      +40     
==========================================
- Hits         3723     3707      -16     
- Misses        879      916      +37     
- Partials        0        1       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

🛠️ Repo: Convert lib/nodejs/ worker pool files to ESM

2 participants