fix(rstest): prevent the re-exports optimization for mocked modules#13262
fix(rstest): prevent the re-exports optimization for mocked modules#13262
Conversation
There was a problem hiding this comment.
Pull request overview
This PR fixes an rstest mocking edge case where side-effects optimization can inline re-export chains and skip intermediate modules, causing rs.mock() / rs.doMock() to miss the intended module IDs. It does so by ensuring modules referenced by rstest mock-module-id dependencies are treated as non-side-effect-free during optimize_dependencies, and adds regression tests covering re-export chains.
Changes:
- Add an early
CompilationOptimizeDependencieshook inRstestPluginto mark mocked modules as not side-effect-free. - Add new rstest config-case entries and test specs for mocking through single and multi-hop re-export chains.
- Add local
node_modules/*fixtures (reexport-top,reexport-intermediate,reexport-source) to reproduce sideEffects-driven re-export inlining.
Reviewed changes
Copilot reviewed 6 out of 12 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| tests/rspack-test/configCases/rstest/mock/rspack.config.js | Adds new rstest entries to run the added re-export mocking regression tests. |
| tests/rspack-test/configCases/rstest/mock/reExportMockedModule.js | New regression test: rs.mock() against a side-effect-free re-exported package. |
| tests/rspack-test/configCases/rstest/mock/reExportDoMockedModule.js | New regression test: rs.doMock() against a side-effect-free re-exported package. |
| tests/rspack-test/configCases/rstest/mock/reExportTripleMockedModule.js | New regression test: rs.mock() across A→B→C re-export chain. |
| tests/rspack-test/configCases/rstest/mock/reExportTripleDoMockedModule.js | New regression test: rs.doMock() across A→B→C re-export chain. |
| tests/rspack-test/configCases/rstest/mock/node_modules/reexport-top/package.json | Fixture package marked sideEffects: false to trigger side-effects optimization behavior. |
| tests/rspack-test/configCases/rstest/mock/node_modules/reexport-top/index.js | Fixture top package that re-exports from reexport-intermediate. |
| tests/rspack-test/configCases/rstest/mock/node_modules/reexport-intermediate/package.json | Fixture package marked sideEffects: false to trigger re-export inlining. |
| tests/rspack-test/configCases/rstest/mock/node_modules/reexport-intermediate/index.js | Fixture intermediate package that re-exports from reexport-source. |
| tests/rspack-test/configCases/rstest/mock/node_modules/reexport-source/package.json | Fixture “real implementation” package metadata. |
| tests/rspack-test/configCases/rstest/mock/node_modules/reexport-source/index.js | Fixture “real implementation” export. |
| crates/rspack_plugin_rstest/src/plugin.rs | Adds optimize_dependencies hook to flip side-effect-free flags for mocked modules. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
You can also share your feedback on Copilot code review. Take the survey.
📦 Binary Size-limit
❌ Size increased by 4.88KB from 48.99MB to 49.00MB (⬆️0.01%) |
Rsdoctor Bundle Diff Analysis
Found 5 projects in monorepo, 0 projects with changes. 📊 Quick Summary
Generated by Rsdoctor GitHub Action |
Merging this PR will not alter performance
Comparing Footnotes
|
Deploying rspack with
|
| Latest commit: |
1c2bde9
|
| Status: | ✅ Deploy successful! |
| Preview URL: | https://a5de4d39.rspack-v2.pages.dev |
| Branch Preview URL: | https://mock-with-re-export.rspack-v2.pages.dev |
Summary
When modules are marked as side-effect-free, Rspack may inline re-export chains and skip intermediate modules. That can break
rs.mock()/rs.doMock()when users mock the intermediate package name (the mocked module ID is no longer the one consumers execute).This change marks mocked modules as non-side-effect-free during
optimize_dependencies, so module boundaries are preserved and mocks reliably hit the intended module IDs.optimize_dependencieshook inRstestPlugin(stage = -1000) to run before side-effects optimization.DependencyType::RstestMockModuleId(covers both rs.mock and rs.doMock).factory_meta.side_effect_free == Some(true), override to Some(false).Related links
fix web-infra-dev/rstest#972
Checklist