Skip to content
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

How to mock a module ? #4

Open
bichikim opened this issue Nov 18, 2022 · 3 comments
Open

How to mock a module ? #4

bichikim opened this issue Nov 18, 2022 · 3 comments

Comments

@bichikim
Copy link

bichikim commented Nov 18, 2022

Hi :)

Thank you for this awesome library.

Please tell me how to mock a module.

For example, karma-typescript-mock can mock a module.

@credred
Copy link
Owner

credred commented Nov 18, 2022

I have no idea about the mock module by using vite now. I will investigate how karma-typescript-mock and other libraries implement mock module.

By the way, currently, do you want global mocks or in-module mocks?

@credred
Copy link
Owner

credred commented Nov 23, 2022

Vite uses the es module, so we cannot directly modify the import results at runtime. That is, we cannot implement the mock module only at runtime like karma-typescript-mock.

But vitest realizes module mocking in es module. Its principle is to use vite ssr to rewrite the import statement at compile time, and change its module loading method to runtime loading. But we can't use this function directly in the browser. So in order to implement this function, we need to implement the es module mock function ourselves. it will take some time

Therefore, in order to realize this function, we need to implement the es module mock function ourselves. This will take some time

@bichikim
Copy link
Author

I want the in-module mocks :) Thx your kind reply

I will research for the es module mock function you mentioned :)

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

No branches or pull requests

2 participants