Skip to content

docs: clarify mock behavior in vitest migration guide#912

Merged
9aoy merged 2 commits intomainfrom
auto-mock-diff
Jan 28, 2026
Merged

docs: clarify mock behavior in vitest migration guide#912
9aoy merged 2 commits intomainfrom
auto-mock-diff

Conversation

@9aoy
Copy link
Copy Markdown
Collaborator

@9aoy 9aoy commented Jan 28, 2026

Summary

Clarifies the auto-mocking behavior of Vitest and Rstest in the migration guide.

  • Vitest's vi.mock() without a factory first checks for a __mocks__ directory before falling back to auto-mocking.
  • Rstest's rs.mock() requires the explicit { mock: true } option for auto-mocking and only checks for __mocks__ otherwise.

Related Links

Checklist

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

Clarifies the auto-mocking behavior of Vitest and Rstest in the migration guide.

- Explains that Vitest's vi.mock() without a factory first checks for a __mocks__ directory before falling back to auto-mocking.
- Explains that Rstest's rs.mock() requires the explicit { mock: true } option for auto-mocking and only checks for __mocks__ otherwise.
- Updates both the English and Chinese documentation.
@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello @9aoy, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request enhances the Vitest migration guide by providing crucial clarifications on how module auto-mocking functions differently between Vitest and Rstest. It aims to prevent confusion for users migrating their testing setups by explicitly outlining the distinct behaviors of vi.mock() and rs.mock() when dealing with module mocks and the __mocks__ directory.

Highlights

  • Vitest vi.mock() behavior: Clarified that Vitest's vi.mock() implicitly attempts to load a manual mock from __mocks__ before falling back to auto-mocking.
  • Rstest rs.mock() behavior: Explained that Rstest's rs.mock() explicitly requires the { mock: true } option for auto-mocking, otherwise it only checks for a __mocks__ directory.
  • Documentation update: Added a new section "Auto-mocking modules" to the Vitest migration guide, detailing the differences in mocking behavior between Vitest and Rstest in both English and Chinese documentation.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request clarifies the auto-mocking behavior in the Vitest migration guide by adding a new section that compares Vitest and Rstest. The explanation is clear and the code examples are helpful. I've found a broken link in the Chinese documentation and suggested a minor wording improvement for the English version to enhance clarity. Overall, a great addition to the documentation.

});
```

这种区别很重要:Vitest 默认提供了隐式的自动模拟回退,而 Rstest 则需要显式配置才能实现相同的行为。更多详细信息,请参阅 [`rs.mock()` 的 `{ mock: true }` 选项](/api/runtime-api/rstest/mock-modules#with-mock-true-option)。
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

high

The link to the API documentation appears to be broken. The anchor #with-mock-true-option points to an ID that doesn't exist on the Chinese page. The correct anchor for the header '使用 { mock: true } 选项' is #使用-mock-true-选项. Please update the link to fix it.

这种区别很重要:Vitest 默认提供了隐式的自动模拟回退,而 Rstest 则需要显式配置才能实现相同的行为。更多详细信息,请参阅 [`rs.mock()` 的 `{ mock: true }` 选项](/api/runtime-api/rstest/mock-modules#使用-mock-true-选项)。

});
```

Rstest handles this differently. Calling `rs.mock()` with only the module path will _only_ look for a mock in the `__mocks__` directory and will error if one isn't found. To achieve auto-mocking, you must explicitly pass the `{ mock: true }` option.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

The phrase 'will error' is a bit informal for documentation. Using 'throws an error' would be more precise and professional.

Rstest handles this differently. Calling `rs.mock()` with only the module path will _only_ look for a mock in the `__mocks__` directory and throws an error if one isn't found. To achieve auto-mocking, you must explicitly pass the `{ mock: true }` option.

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

cloudflare-workers-and-pages bot commented Jan 28, 2026

Deploying rstest with  Cloudflare Pages  Cloudflare Pages

Latest commit: 2e2f5ed
Status:⚡️  Build in progress...

View logs

@9aoy 9aoy merged commit 348a2c3 into main Jan 28, 2026
5 of 6 checks passed
@9aoy 9aoy deleted the auto-mock-diff branch January 28, 2026 03:21
@fi3ework fi3ework mentioned this pull request Feb 3, 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.

1 participant