Skip to content

fix(core): restore async doMock factory and doUnmock recovery#998

Merged
fi3ework merged 1 commit intomainfrom
fix/mock-runtime-async-dounmock
Mar 4, 2026
Merged

fix(core): restore async doMock factory and doUnmock recovery#998
fi3ework merged 1 commit intomainfrom
fix/mock-runtime-async-dounmock

Conversation

@fi3ework
Copy link
Copy Markdown
Member

@fi3ework fi3ework commented Mar 3, 2026

Summary

  • fix two mock runtime regressions introduced after v0.8.2:
    • rs.doMock(..., async () => ...) could expose broken exports because Promise factory results were not handled in the runtime module factory path
    • repeated rs.doMock() calls could overwrite original module snapshots, so rs.doUnmock() no longer restored the true original implementation
  • preserve original module snapshots only on the first mock for each module id, and use own-property checks to avoid falsy export edge cases
  • keep async-factory behavior explicit in runtime by recognizing Promise factory results for ESM mock factories
  • add e2e regression tests:
    • e2e/mock/tests/doMockAsyncFactory.test.ts
    • e2e/mock/tests/doUnmockRestoreOriginal.test.ts
  • update EN/ZH API docs for rs.mock / rs.doMock factory type signatures to align with Promise-returning factory support

Flow (Before vs After)

Regression flow (before this PR)

Case A: async doMock factory

rs.doMock(id, async factory)
          |
          v
   res = modFactory()            // Promise
          |
          v
 for (key in res) { ... }        // enumerates Promise, no module exports wired
          |
          v
import mocked module
          |
          v
missing export at runtime (e.g. increment is not a function)

Case B: doUnmock after repeated doMock

1st doMock(id): save original factory F0
2nd doMock(id): overwrite saved factory with mocked factory F1
rs.doUnmock(id): restore F1 (mocked), not F0 (original)
Fixed flow (after this PR)

Case A: async doMock factory

rs.doMock(id, async factory)
          |
          v
   res = modFactory()
          |
          +--> if res is Promise => module.exports = res
          |
          v
webpack async module path resolves Promise and binds exports correctly

Case B: doUnmock after repeated doMock

1st doMock(id): save original snapshot (factory/module)
next doMock(id): DO NOT overwrite original snapshot
rs.doUnmock(id): restore the original factory snapshot

Related

@cloudflare-workers-and-pages
Copy link
Copy Markdown

Deploying rstest with  Cloudflare Pages  Cloudflare Pages

Latest commit: ae57319
Status: ✅  Deploy successful!
Preview URL: https://f2a72577.rstest.pages.dev
Branch Preview URL: https://fix-mock-runtime-async-dounm.rstest.pages.dev

View logs

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: ae57319d63

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +186 to +188
if (isPromise(res)) {
__webpack_module__.exports = res;
return;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Support async rs.mock factories for static named imports

When modFactory() returns a Promise, this path assigns that Promise to module.exports and returns before any ESM export getters are defined. In hoisted rs.mock(...) flows that use static named imports (the common rs.mock pattern), bindings like import { increment } resolve to undefined and fail at call time (... is not a function), so async factory support is still broken for that usage even though the API/docs now advertise Promise-returning factories.

Useful? React with 👍 / 👎.

@fi3ework fi3ework merged commit baea33f into main Mar 4, 2026
31 of 38 checks passed
@fi3ework fi3ework deleted the fix/mock-runtime-async-dounmock branch March 4, 2026 07:15
@9aoy 9aoy mentioned this pull request Mar 9, 2026
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