-
I realised (eventually) that vi.mock silently omits mocking modules which are loaded using
Is there a standard answer for mocking CommonJS style imports that is compatible with vitest? I'm assuming that blending Everything else about my test suite (in a commonJS repository) is working well with no transpilation or build step - I am seamlessly using Typescript to author tests against functions defined using commonjs formalisms. However, the one occasion where I need to mock a required module is turning out to be very costly. To fulfil the requirements of What options do I have for mocking a module loaded with |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 14 replies
-
Vitest doesn't intercept |
Beta Was this translation helpful? Give feedback.
-
Adopting |
Beta Was this translation helpful? Give feedback.
-
Can somebody post some code snipped with a working mock-require implementation? That would be very helpful 👍 |
Beta Was this translation helpful? Give feedback.
-
In case anybody comes late to the party... @sm3sher provided the answer but it will only work with a single CommonJS module. I needed more. I created a full-blown package that shims the module loader and allows mocking of multiple CommonJS modules. At GitHub: https://github.com/jmussman/vitest-mock-commonjs, and at npmjs.com: https://www.npmjs.com/package/vitest-mock-commonjs. More importantly the README delves deep into how it all works if you want to know, and there is an open-source stand-alone example that depends on the package at https://github.com/jmussman/auth0-block-idp-signup. |
Beta Was this translation helpful? Give feedback.
Adopting
mock-require
in the test suite was an effective workaround.