Skip to content

Conversation

@fi3ework
Copy link
Member

@fi3ework fi3ework commented Oct 15, 2025

Summary

implementing async mock factories comes at a significant cost. besides requiring extensive runtime modifications, properly handling circular dependencies necessitates deep AST analysis and transformations. this would severely impact performance, and because static imports are converted to dynamic imports, the test files' build would inevitably diverge from the source code, leading to unknown errors.

we are dropping support for async mock factories. after investigation, we found that async mock factories are primarily used to configure partial mocks using importActual, and the asynchronous nature of importActual necessitates an async mock factory.

in this PR, we introduce a new synchronous replacement for rs.importActual:

  1. Import attributes (recommended)
import * as foo from 'foo' with { "rstest": "importActual" }
rs.mock('foo', ()=> {
  return { ...foo, extra: 1 }
})
  1. query
import * as foo from 'foo?rstest=importActual'
rs.mock('foo', ()=> {
  return { ...foo, extra: 1 }
})

and, of course, rs.importActual will be retained. You can continue to use it within it / test test functions to import original modules that are not being mocked. however, it can no longer be used within mock factories, as the second parameter of the mock factory no longer accepts async functions.

Related Links

Checklist

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

@netlify
Copy link

netlify bot commented Oct 15, 2025

Deploy Preview for rstest-dev ready!

Name Link
🔨 Latest commit 58569c2
🔍 Latest deploy log https://app.netlify.com/projects/rstest-dev/deploys/68f7339299886e0008259d16
😎 Deploy Preview https://deploy-preview-618--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.

@fi3ework fi3ework force-pushed the sync-mock branch 11 times, most recently from 6b80654 to e1c0239 Compare October 17, 2025 08:31
@fi3ework fi3ework marked this pull request as ready for review October 17, 2025 09:02
@Copilot Copilot AI review requested due to automatic review settings October 17, 2025 09:02
Copy link
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 removes support for async mock factories in Rstest to improve performance and build reliability. The change replaces the async rs.importActual pattern within mock factories with synchronous alternatives using import attributes or query parameters.

  • Introduces synchronous importActual methods via import attributes (with { "rstest": "importActual" }) and query parameters (?rstest=importActual)
  • Updates mock factory signatures to be synchronous only
  • Adds configuration option importActualMethods to control which synchronous import methods are supported

Reviewed Changes

Copilot reviewed 60 out of 62 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
website/docs/{en,zh}/config/test/importActualMethods.mdx New documentation for importActualMethods configuration option
website/docs/{en,zh}/api/rstest/mockModules.mdx Updated API documentation removing async factory support and adding synchronous import examples
packages/core/src/types/config.ts Added importActualMethods configuration type definition
packages/core/src/config.ts Added default configuration for importActualMethods
packages/core/src/core/rsbuild.ts Added mock loader setup for import attributes support
packages/core/src/core/plugins/mockRuntime.ts Refactored runtime code generation to use external file
packages/core/src/core/plugins/mockRuntimeCode.js New external runtime code file replacing inline generation
packages/core/src/core/plugins/external.ts Enhanced external handling to support query-based actual imports
packages/core/src/utils/helper.ts Added utility functions for parsing and manipulating rstest query parameters
e2e/mock/tests/* Updated test files to use synchronous mock factories and new import patterns
Files not reviewed (1)
  • pnpm-lock.yaml: Language not supported

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@fi3ework fi3ework requested a review from 9aoy October 17, 2025 09:03
@fi3ework fi3ework force-pushed the sync-mock branch 3 times, most recently from 42b2e27 to e63bd1e Compare October 17, 2025 09:12
@fi3ework fi3ework merged commit 2302974 into main Oct 21, 2025
19 checks passed
@fi3ework fi3ework deleted the sync-mock branch October 21, 2025 07:47
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