fix: should not throw error when undefined module is mocked#740
Merged
Conversation
✅ Deploy Preview for rstest-dev ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes an issue where importing undefined/virtual modules that are mocked would throw errors during the build phase. The fix externalizes modules that fail to resolve as CommonJS modules, allowing the mocking system to provide them at runtime. This enables testing with virtual modules (modules that don't exist in the filesystem but are provided via rs.mock()).
Key changes:
- Modified module resolution error handling to externalize unresolved modules instead of silently ignoring them
- Changed the order of external handlers to prioritize built-in module detection
- Updated test expectations to account for the trade-off of losing code frame information when modules fail to resolve
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| packages/core/src/core/plugins/external.ts | Modified error handling to externalize unresolved modules as 'node-commonjs' and changed external handler order from push to unshift |
| e2e/externals/mocked.test.ts | Added new test case demonstrating virtual module mocking functionality |
| e2e/test-api/fixtures/moduleNotFound.test.ts | Updated error message assertion from exact string match to regex to accommodate varying error formats |
| e2e/test-api/edgeCase.test.ts | Commented out code frame expectation due to loss of frame info when modules are externalized |
| packages/core/tests/core/snapshots/rsbuild.test.ts.snap | Updated snapshots to reflect new order of external handlers in config |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
should not throw error when undefined module is mocked. Ignored and treated it as an external CommonJS module. However, if the module is really not found, we will lose code frame information.
The below code should be worked:
Related Links
Checklist