Skip to content

test_runner: add exports option to mock.module#61727

Merged
nodejs-github-bot merged 9 commits intonodejs:mainfrom
Han5991:test-runner-mock-exports
Mar 27, 2026
Merged

test_runner: add exports option to mock.module#61727
nodejs-github-bot merged 9 commits intonodejs:mainfrom
Han5991:test-runner-mock-exports

Conversation

@Han5991
Copy link
Copy Markdown
Contributor

@Han5991 Han5991 commented Feb 8, 2026

Summary

  • add mock.module(..., { exports }) and normalize option shapes through a shared exports path
  • keep defaultExport and namedExports as aliases, and emit runtime DeprecationWarning when legacy options are used
  • update docs, tests, and output fixtures/snapshots to reflect deprecation signaling and the new preferred option shape

Legacy Option Mixing Behavior

  • Mixed usage of options.exports with legacy options is currently allowed.
  • Values are normalized/merged into the same internal exports structure.
  • Legacy options are deprecated, but still supported for compatibility during migration.

Scope

Testing

  • make lint-js
  • python3 tools/test.py test/parallel/test-runner-module-mocking.js
  • python3 tools/test.py test/test-runner/test-output-coverage-with-mock.mjs test/test-runner/test-output-typescript-coverage.mjs

Refs: #58443

@nodejs-github-bot
Copy link
Copy Markdown
Collaborator

Review requested:

  • @nodejs/test_runner

@nodejs-github-bot nodejs-github-bot added needs-ci PRs that need a full CI run. test_runner Issues and PRs related to the test runner subsystem. labels Feb 8, 2026
@Han5991 Han5991 marked this pull request as draft February 8, 2026 00:27
@Han5991 Han5991 force-pushed the test-runner-mock-exports branch from 3d95c0c to fa99ada Compare February 8, 2026 00:35
@JakobJingleheimer
Copy link
Copy Markdown
Member

JakobJingleheimer commented Feb 8, 2026

Related: #58443

(I haven't read through this PR yet though)

@Han5991
Copy link
Copy Markdown
Contributor Author

Han5991 commented Feb 8, 2026

Related: #58443

(I haven't read through this PR yet though)

@JakobJingleheimer

I was working on it with that in mind.
I'm planning to proceed only up to Step 2 of the plan. Is that okay?

@Han5991 Han5991 force-pushed the test-runner-mock-exports branch from 73c4cf1 to 715c12c Compare February 8, 2026 01:11
@JakobJingleheimer
Copy link
Copy Markdown
Member

JakobJingleheimer commented Feb 8, 2026

Yes, sounds good go me!

It would be ideal (but not required or anything) to land this with a userland migration ready to go. I think it could be done fairly easily.

Cc @Ceres6 this should make your Jest → node:test migration easier 🙂

@Han5991
Copy link
Copy Markdown
Contributor Author

Han5991 commented Feb 8, 2026

Thanks for the feedback. I will scope this PR to Step 2 of the plan: introduce options.exports, keep defaultExport/namedExports as aliases (with deprecation signaling), and defer userland migration + removal to follow-up work.

@Han5991 Han5991 marked this pull request as ready for review February 8, 2026 01:33
@codecov
Copy link
Copy Markdown

codecov bot commented Feb 8, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 89.69%. Comparing base (7547e79) to head (410505f).
⚠️ Report is 35 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #61727      +/-   ##
==========================================
- Coverage   91.60%   89.69%   -1.91%     
==========================================
  Files         337      676     +339     
  Lines      140745   206758   +66013     
  Branches    21802    39595   +17793     
==========================================
+ Hits       128925   185453   +56528     
- Misses      11595    13456    +1861     
- Partials      225     7849    +7624     
Files with missing lines Coverage Δ
lib/internal/test_runner/mock/loader.js 99.35% <100.00%> (ø)
lib/internal/test_runner/mock/mock.js 98.81% <100.00%> (+0.61%) ⬆️

... and 458 files with indirect coverage changes

🚀 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.

@Han5991 Han5991 force-pushed the test-runner-mock-exports branch 2 times, most recently from 424675e to 0498de3 Compare February 8, 2026 07:24
Copy link
Copy Markdown
Member

@JakobJingleheimer JakobJingleheimer left a comment

Choose a reason for hiding this comment

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

Thanks for picking this up!

Comment on lines +826 to +829
if ('exports' in options) {
validateObject(options.exports, 'options.exports');
copyOwnProperties(options.exports, moduleExports);
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Since last-wins, I think the "new" should win (and be moved below namedExports and defaultExport copying).

Or maybe when exports exists, the others should be ignored? I feel like there's some kind of back-version compatibility thing here. @ljharb thoughts?

Copy link
Copy Markdown
Member

@JakobJingleheimer JakobJingleheimer left a comment

Choose a reason for hiding this comment

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

I think this is looking pretty good :)

@Han5991
Copy link
Copy Markdown
Contributor Author

Han5991 commented Mar 2, 2026

@JakobJingleheimer,

I'd like to help with the userland-migration tool for mock.module.

If you approve, I can implement the migration script in short order to help users move to the new exports shape. Should I contribute it to https://github.com/nodejs/userland-migrations?

@JakobJingleheimer
Copy link
Copy Markdown
Member

I'd like to help with the userland-migration tool for mock.module.

If you approve, I can implement the migration script in short order to help users move to the new exports shape. Should I contribute it to https://github.com/nodejs/userland-migrations?

Heck yes! And yes, that is where to go. @brunocroh may have started. Could you sync with him? I think this would be a great one to get started with 🙂

@brunocroh
Copy link
Copy Markdown
Member

brunocroh commented Mar 3, 2026

I'd like to help with the userland-migration tool for mock.module.

If you approve, I can implement the migration script in short order to help users move to the new exports shape. Should I contribute it to https://github.com/nodejs/userland-migrations?

Heck yes! And yes, that is where to go. @brunocroh may have started. Could you sync with him? I think this would be a great one to get started with 🙂

Its almost done, https://github.com/nodejs/userland-migrations/pull/390/changes, I just need add more edge cases and docs for it

Copy link
Copy Markdown
Member

@JakobJingleheimer JakobJingleheimer left a comment

Choose a reason for hiding this comment

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

LGTM aside from the item I'd like some others' opinions on. TC39 and some other conferences are this week, so I'll poke people next week after things are calmer.

Bold the `mock.module()` option compatibility notes in the test
API docs.

This makes the mutual exclusion between `exports`,
`defaultExport`, and `namedExports` easier to notice when
reading the module mocking options.
@Han5991 Han5991 force-pushed the test-runner-mock-exports branch from 06ba0aa to 2466ad5 Compare March 20, 2026 20:54
Copy link
Copy Markdown
Member

@gurgunday gurgunday left a comment

Choose a reason for hiding this comment

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

lgtm

@JakobJingleheimer JakobJingleheimer added commit-queue-squash Add this label to instruct the Commit Queue to squash all the PR commits into the first one. request-ci Add this label to start a Jenkins CI on a PR. notable-change PRs with changes that should be highlighted in changelogs. labels Mar 21, 2026
@github-actions
Copy link
Copy Markdown
Contributor

The notable-change PRs with changes that should be highlighted in changelogs. label has been added by @JakobJingleheimer.

Please suggest a text for the release notes if you'd like to include a more detailed summary, then proceed to update the PR description with the text or a link to the notable change suggested text comment. Otherwise, the commit will be placed in the Other Notable Changes section.

@JakobJingleheimer
Copy link
Copy Markdown
Member

Notable change

MockModuleOptions.defaultExport and MockModuleOptions.namedExports have been consolidated into a single option MockModuleOptions.exports to align with user expectations and other test runners.

A default property on MockModuleOptions.exports represents the default export, and own enumerable properties are treated as named exports.

An automated migration is available to update user code: https://github.com/nodejs/userland-migrations/tree/main/recipes/mock-module-exports

npx codemod @nodejs/mock-module-exports

@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Mar 26, 2026
@nodejs-github-bot
Copy link
Copy Markdown
Collaborator

@nodejs-github-bot
Copy link
Copy Markdown
Collaborator

@nodejs-github-bot
Copy link
Copy Markdown
Collaborator

@nodejs-github-bot
Copy link
Copy Markdown
Collaborator

@nodejs-github-bot
Copy link
Copy Markdown
Collaborator

@JakobJingleheimer
Copy link
Copy Markdown
Member

For the curious, CI is consistently failing on

parallel/test-http2-res-writable-properties

For win11-arm64-COMPILED_BY-vs2022_clang-arm64

Eg https://ci.nodejs.org/job/node-test-binary-windows-js-suites/RUN_SUBSET=0,nodes=win11-arm64-COMPILED_BY-vs2022_clang-arm64/39500/console

@nodejs-github-bot
Copy link
Copy Markdown
Collaborator

CI: https://ci.nodejs.org/job/node-test-pull-request/72072/

@nodejs-github-bot
Copy link
Copy Markdown
Collaborator

@JakobJingleheimer JakobJingleheimer added the commit-queue Add this label to land a pull request using GitHub Actions. label Mar 27, 2026
@nodejs-github-bot nodejs-github-bot removed the commit-queue Add this label to land a pull request using GitHub Actions. label Mar 27, 2026
@nodejs-github-bot nodejs-github-bot merged commit 7d1f1b4 into nodejs:main Mar 27, 2026
74 checks passed
@nodejs-github-bot
Copy link
Copy Markdown
Collaborator

Landed in 7d1f1b4

@github-project-automation github-project-automation bot moved this from In Progress to Done in Test Runner Board Mar 27, 2026
aduh95 pushed a commit that referenced this pull request Mar 28, 2026
Add options.exports support in mock.module() and normalize option

shapes through a shared exports path.

Keep defaultExport and namedExports as aliases, emit runtime

deprecation warnings for legacy options, and update docs and tests,

including output fixtures and coverage snapshots.

Refs: #58443
PR-URL: #61727
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Jacob Smith <jacob@frende.me>
Reviewed-By: Pietro Marchini <pietro.marchini94@gmail.com>
Reviewed-By: Gürgün Dayıoğlu <hey@gurgun.day>
Reviewed-By: Chemi Atlow <chemi@atlow.co.il>
aduh95 pushed a commit that referenced this pull request Mar 28, 2026
Add options.exports support in mock.module() and normalize option

shapes through a shared exports path.

Keep defaultExport and namedExports as aliases, emit runtime

deprecation warnings for legacy options, and update docs and tests,

including output fixtures and coverage snapshots.

Refs: #58443
PR-URL: #61727
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Jacob Smith <jacob@frende.me>
Reviewed-By: Pietro Marchini <pietro.marchini94@gmail.com>
Reviewed-By: Gürgün Dayıoğlu <hey@gurgun.day>
Reviewed-By: Chemi Atlow <chemi@atlow.co.il>
aduh95 pushed a commit that referenced this pull request Mar 28, 2026
Add options.exports support in mock.module() and normalize option

shapes through a shared exports path.

Keep defaultExport and namedExports as aliases, emit runtime

deprecation warnings for legacy options, and update docs and tests,

including output fixtures and coverage snapshots.

Refs: #58443
PR-URL: #61727
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Jacob Smith <jacob@frende.me>
Reviewed-By: Pietro Marchini <pietro.marchini94@gmail.com>
Reviewed-By: Gürgün Dayıoğlu <hey@gurgun.day>
Reviewed-By: Chemi Atlow <chemi@atlow.co.il>
Qard pushed a commit to Qard/node that referenced this pull request Mar 29, 2026
Add options.exports support in mock.module() and normalize option

shapes through a shared exports path.

Keep defaultExport and namedExports as aliases, emit runtime

deprecation warnings for legacy options, and update docs and tests,

including output fixtures and coverage snapshots.

Refs: nodejs#58443
PR-URL: nodejs#61727
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Jacob Smith <jacob@frende.me>
Reviewed-By: Pietro Marchini <pietro.marchini94@gmail.com>
Reviewed-By: Gürgün Dayıoğlu <hey@gurgun.day>
Reviewed-By: Chemi Atlow <chemi@atlow.co.il>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

commit-queue-squash Add this label to instruct the Commit Queue to squash all the PR commits into the first one. needs-ci PRs that need a full CI run. notable-change PRs with changes that should be highlighted in changelogs. test_runner Issues and PRs related to the test runner subsystem.

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

10 participants