credred / karma-vite

A karma plugin. Transform es module by using vite.
MIT License
13 stars 4 forks source link

How to mock a module ? #4

Open bichikim opened 1 year ago

bichikim commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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

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