Skip to content

fix: should not throw error when undefined module is mocked#740

Merged
9aoy merged 2 commits intomainfrom
mocked-undefined-module
Dec 8, 2025
Merged

fix: should not throw error when undefined module is mocked#740
9aoy merged 2 commits intomainfrom
mocked-undefined-module

Conversation

@9aoy
Copy link
Copy Markdown
Collaborator

@9aoy 9aoy commented Dec 8, 2025

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:

import { expect, it, rs } from '@rstest/core';
// @ts-expect-error
import v from 'virtual-module';

rs.mock('virtual-module', () => ({
  default: 'this is a mocked module',
}));

it('should load mocked module', () => {
  expect(v).toBe('this is a mocked module');
});

Related Links

Checklist

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

Copilot AI review requested due to automatic review settings December 8, 2025 03:59
@netlify
Copy link
Copy Markdown

netlify bot commented Dec 8, 2025

Deploy Preview for rstest-dev ready!

Name Link
🔨 Latest commit 3513dc7
🔍 Latest deploy log https://app.netlify.com/projects/rstest-dev/deploys/6936696f646d170008291c75
😎 Deploy Preview https://deploy-preview-740--rstest-dev.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

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>
@9aoy 9aoy requested a review from fi3ework December 8, 2025 06:08
@github-actions
Copy link
Copy Markdown

github-actions bot commented Dec 8, 2025

📝 Rstest Ecosystem CI: Open

suite result
rsbuild ✅ success
rslib ✅ success
rsdoctor ✅ success

@9aoy 9aoy merged commit df0d282 into main Dec 8, 2025
16 checks passed
@9aoy 9aoy deleted the mocked-undefined-module branch December 8, 2025 07:45
@9aoy 9aoy mentioned this pull request Dec 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants