Skip to content

fix(rstest): use sync mock factory in mocking example#475

Merged
fi3ework merged 1 commit into
rstackjs:mainfrom
fi3ework:fix/rstest-mock-sync-factory
Jun 24, 2026
Merged

fix(rstest): use sync mock factory in mocking example#475
fi3ework merged 1 commit into
rstackjs:mainfrom
fi3ework:fix/rstest-mock-sync-factory

Conversation

@fi3ework

Copy link
Copy Markdown
Member

Why

The rstest/mocking example fails on rstest 0.9.x (eco-ci run):

Error: [Rstest] An async mock factory is not supported. Use a sync factory;
to keep part of the original module, import it with `with { rstest: 'importActual' }` and spread it in.

The rstest-side change (rejecting async mock factories) is intentional, so the example needs updating.

What

In tests/dynamic-mock.test.ts, the should import actual module inside mock case used an async factory with await rs.importActual(...). Switched it to the supported pattern: load the original module via the with { rstest: 'importActual' } import attribute and use a synchronous factory.

Verify

pnpm test in rstest/mocking — all 60 tests pass.

rstest no longer supports async mock factories. Load the original module via the `with { rstest: 'importActual' }` import attribute and spread it into a sync factory instead.
@fi3ework fi3ework enabled auto-merge (squash) June 24, 2026 03:15
@coderabbitai

coderabbitai Bot commented Jun 24, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: f788e74d-d482-4bcd-9321-2949393f16e1

📥 Commits

Reviewing files that changed from the base of the PR and between 8b6a2f9 and 0a4fbcc.

📒 Files selected for processing (1)
  • rstest/mocking/tests/dynamic-mock.test.ts

📝 Walkthrough

Walkthrough

The test file dynamic-mock.test.ts is updated to simplify the rs.importActual() - Import Original Module test case. A top-level import of formatDate from ../src/utils is added, aliased as actualFormatDate. The rs.doMock call is changed from an async factory that called rs.importActual and spread the entire actual module, to a synchronous factory that sets formatDate directly to actualFormatDate and overrides generateId with a fixed value.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and specifically describes the main change: switching from an async to a synchronous mock factory in the rstest mocking example.
Description check ✅ Passed The description is well-related to the changeset, explaining why the change was needed (rstest 0.9.x compatibility), what was changed (async factory to sync pattern), and verification that tests pass.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@fi3ework fi3ework merged commit ff70854 into rstackjs:main Jun 24, 2026
1 check passed

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

Copy link
Copy Markdown

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: 0a4fbcce9e

ℹ️ 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 +91 to +94
rs.doMock('../src/utils', () => ({
formatDate: actualFormatDate,
generateId: () => 'overridden-id',
}));

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Preserve actual exports in the partial mock

In this partial-mock example, the factory now returns only formatDate and the overridden generateId, so the other real exports from ../src/utils (getTimestamp, sleep, log) are no longer available from the mocked module. A user copying this “use the original module implementation alongside mocked parts” pattern and importing one of those original exports will not get the real implementation; import the actual namespace with the rstest: 'importActual' attribute and spread it before overriding generateId.

Useful? React with 👍 / 👎.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant