Skip to content

feat(rstest_plugin): support hoist mock and resolve mocked module id #10519

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

Merged
merged 2 commits into from
Jun 9, 2025

Conversation

fi3ework
Copy link
Member

@fi3ework fi3ework commented May 29, 2025

Summary

  • hoist rs.mock to the top of the current module
  • change rs.mock to __webpack_require__.set_mock, set_mock is a runtime function, which could be injected from other supporting plugin. Since it could be implemented by JS plugin API, so for future development, I'm not implementing it here but in Rstest side.

TODO:

  • Now, rs.mock is hard coded call, even rs is not defined for imported from arbitrary package.

After hoisting, the code be like:

(() => {
// [Rstest mock hoist] "./foo"
__webpack_require__.set_mock(298, () => {
  return { value: 'mockedFoo' }
});

// [Rstest mock hoist] "./bar"
__webpack_require__.set_mock(562, () => {
  return { value: 'mockedBar' }
});

/* ESM import */var _barrel__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(298);
/* ESM import */var _barrel__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(562);
const TOP_OF_FILE = 1

;






it('should mock modules', () => {
	expect(_barrel__WEBPACK_IMPORTED_MODULE_0__.value).toBe('mockedFoo')
	expect(_barrel__WEBPACK_IMPORTED_MODULE_1__.value).toBe('mockedBar')
})

})();

Checklist

  • Tests updated (or not required).
  • Documentation updated (or not required).

@github-actions github-actions bot added release: feature release: feature related release(mr only) team The issue/pr is created by the member of Rspack. labels May 29, 2025
Copy link

netlify bot commented May 29, 2025

Deploy Preview for rspack canceled.

Name Link
🔨 Latest commit 9ad8300
🔍 Latest deploy log https://app.netlify.com/projects/rspack/deploys/684189bf4b40180008227da4

Copy link

codspeed-hq bot commented May 29, 2025

CodSpeed Performance Report

Merging #10519 will not alter performance

Comparing rstest_mock (9ad8300) with main (e32b7ed)

🎉 Hooray! codspeed-node just leveled up to 4.0.1!

A heads-up, this is a breaking change and it might affect your current performance baseline a bit. But here's the exciting part - it's packed with new, cool features and promises improved result stability 🥳!
Curious about what's new? Visit our releases page to delve into all the awesome details about this new version.

Summary

✅ 12 untouched benchmarks

@fi3ework fi3ework force-pushed the rstest_mock branch 4 times, most recently from af5f13e to e0a18b2 Compare June 5, 2025 11:11
@fi3ework fi3ework marked this pull request as ready for review June 5, 2025 12:23
@fi3ework fi3ework requested a review from LingyuCoder June 5, 2025 12:25
@fi3ework fi3ework merged commit 0bfad55 into main Jun 9, 2025
35 checks passed
@fi3ework fi3ework deleted the rstest_mock branch June 9, 2025 05:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release: feature release: feature related release(mr only) team The issue/pr is created by the member of Rspack.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants