Skip to content

fix: works with async mock factory#11050

Merged
fi3ework merged 1 commit intomainfrom
rstest-async
Jul 18, 2025
Merged

fix: works with async mock factory#11050
fi3ework merged 1 commit intomainfrom
rstest-async

Conversation

@fi3ework
Copy link
Copy Markdown
Member

@fi3ework fi3ework commented Jul 15, 2025

Summary

The runtime code modification and integration test case is added in Rstest repository web-infra-dev/rstest#389.

This PR is to resolve two main issues in Rstest mock side:

  1. Issue: mock hoist order conflict: if the mock for ../src/c is hoisted to a position that is too top, the mock factory in ../src/c will access an uninitialized (undefined) rs object.

    Solution: When a mock factory exists, hoist .mock to the placeholder by using InitFragmentStage::StageAsyncESMImports and set its position to the maximum. Additionally, add the line var _src_c__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__("./mock/src/c.ts"); to refresh the reference to _src_c__WEBPACK_IMPORTED_MODULE_1__.

    import { expect, it, rs } from '@rstest/core';
    import { c } from '../src/c';
    import { d } from '../src/d';
    
    // To test async mocking factory.
    rs.mock('../src/c', async () => {
      return {
        c: rs.fn(),
        d,
      };
    });
    
    it('mocked c', async () => {
      // @ts-expect-error: It has been mocked.
      c('c');
      expect(c).toHaveBeenCalledWith('c');
      expect(d).toBe(4);
    });
  2. Issue: async mock factory: In the example below, the async function in the mock factory must be awaited before executing the test case.

    Solution: For the .mock method with a mock factory, insert the line await __webpack_require__.rstest_exec("../src/d"). Additionally, mark all modules with a factory in their .mock methods as async (TLA) so that the previously mentioned await can be used with the module TLA.

    import { d } from '../src/d';
    
    // To test async mocking factory.
    rs.mock('../src/d', async () => {
      await sleep(1000);
      return {
        d: rs.fn(),
      };
    });
    
    it('mocked d', async () => {
      // @ts-expect-error: It has been mocked.
      d('string1');
      expect(d).toHaveBeenCalledWith('string1');
    });

Here's an image to helping understand what init fragments are injected.

image

Related links

Checklist

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

@netlify
Copy link
Copy Markdown

netlify bot commented Jul 15, 2025

Deploy Preview for rspack canceled.

Name Link
🔨 Latest commit b0b0315
🔍 Latest deploy log https://app.netlify.com/projects/rspack/deploys/68776820643e8f0008c31993

@github-actions github-actions bot added release: bug fix release: bug related release(mr only) team The issue/pr is created by the member of Rspack. labels Jul 15, 2025
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Jul 15, 2025

📦 Binary Size-limit

Comparing b0b0315 to chore(deps): update patch npm dependencies (#11060) by renovate[bot]

❌ Size increased by 7.88KB from 56.70MB to 56.71MB (⬆️0.01%)

@codspeed-hq
Copy link
Copy Markdown

codspeed-hq bot commented Jul 15, 2025

CodSpeed Performance Report

Merging #11050 will not alter performance

Comparing rstest-async (b0b0315) with main (b1f67f5)

🎉 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

✅ 16 untouched benchmarks

@fi3ework fi3ework force-pushed the rstest-async branch 2 times, most recently from fe64871 to 6b3f63f Compare July 16, 2025 07:42
@fi3ework fi3ework marked this pull request as ready for review July 16, 2025 09:21
@fi3ework fi3ework requested a review from LingyuCoder as a code owner July 16, 2025 09:21
@fi3ework fi3ework merged commit b7a6af6 into main Jul 18, 2025
46 checks passed
@fi3ework fi3ework deleted the rstest-async branch July 18, 2025 06:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release: bug fix release: bug 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