Skip to content

feat: more mock api #290

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

feat: more mock api #290

wants to merge 1 commit into from

Conversation

fi3ework
Copy link
Member

@fi3ework fi3ework commented Jun 18, 2025

Summary

Support most mock APIs.

Related Links

  • rs.mock
  • rs.importActual
  • rs.doMock
  • rs.importMock
  • rs.mockRequire
  • rs.requireActual
  • rs.doMockRequire
  • rs.requireMock
  • rs.resetModules
  • rs.unmock
  • rs.doUnmock

Checklist

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

Copy link

netlify bot commented Jun 18, 2025

Deploy Preview for rstest-dev ready!

Name Link
🔨 Latest commit 3c7da85
🔍 Latest deploy log https://app.netlify.com/projects/rstest-dev/deploys/68625c65e6b733000801609f
😎 Deploy Preview https://deploy-preview-290--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 more_mock_api branch 2 times, most recently from 1b13390 to 8fce7ef Compare June 24, 2025 12:23
@fi3ework fi3ework force-pushed the more_mock_api branch 9 times, most recently from f60d30b to b3a5824 Compare June 30, 2025 07:50
@fi3ework
Copy link
Member Author

It's good to be reviewed now. Although there is still more work to do, it can handle most common mock functionalities. This PR will be able to migrate Rslib mock test cases tested by linking them locally on my machine.

@fi3ework fi3ework marked this pull request as ready for review June 30, 2025 09:49
@fi3ework fi3ework requested a review from 9aoy June 30, 2025 09:49
const { RuntimeModule } = compiler.webpack;
class RetestImportRuntimeModule extends RuntimeModule {
constructor() {
super('rstest runtime');
Copy link
Contributor

Choose a reason for hiding this comment

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

Finally know where this rstest runtime came from 😆
image

@@ -43,30 +43,41 @@ export const createRstestUtilities: () => RstestUtilities = () => {
return rstest;
},
mock: () => {
// TODO
// The actual implementation is managed by the built-in Rstest plugin.
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe we can throw error, if some dynamic case is not processed by Rstest plugin.

Copy link
Member Author

@fi3ework fi3ework Jun 30, 2025

Choose a reason for hiding this comment

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

That will be the very ideal target, however, the hard part is: you can mock a module in a test entry file while import it in a nested imported source file, and they're isolated in compile time. So we had to throw an error in runtime.

I think this is still feasible with the following setups in the runtime solution:

  1. store all mocked module ids in runtime.
  2. monkey patch __webpack_require__ (it can't be customized render by plugin IMO) to diversion mocked module and non found module, throw a user-friendly error for the later case.

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