-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Implement module mocking in browser mode #3046
Comments
Mocking in the browser is not supported yet. |
Yes, I remember reviewing the original PR that landed the browser runner. My suggestion then would be to throw a more meaningful error for users. |
@sheremet-va have you got any ideas on how best to support browser ESM mocks? We are working on something similar at Cypress for our Maybe we can collaborate and solve ESM mocks in the browser for both Vitest and Cypress. We can put some time and effort into contributing to both Cypress and Vitest, and Vite/esbuild if needed, to get this moving. |
Module mocking is already implemented in webdriverio, as far as I understand. I took a brief glance at the code, and the idea seems solid, but we will probably need to make some adjustments:
But this is about module mocking with a factory/automock. To support import { name } from './some-path.js'
const { something } = await import('./some-path.js')
// ->
import * as __vite_import_1__ from './some-path.js'
const { name } = __vitestModule(__vite_import_1__)
const { something } = __vitestModule(await import('./some-path.js')) Where const modulesCache = new WeakMap()
function __vitestModule(module) {
if (!modulesCache.has(module)) {
// so ESM module loses restrictions
modulesCache.set(module, Object.assign({}, module))
}
return modulesCache.get(module)
} This is what Vite already does for optimized dependencies and for SSR. |
Gotcha... this is pretty much in line with what I was expecting for implementing this in Vitest.
I haven't seen how they handle this, will take a look. I saw another non-runner specific way, using importmaps. You then just combine with something like sinon. Also looking to do some exploration around this - a non-runner specific solution sure would be nice. |
It would be nice, but I don't think it's possible to make it work with the same API Vitest has, unfortunately. The general solution is not DX-friendly. |
To summarize it: the current workaround for mocking or spying in vitest with browser mode enabled is to use libraries like or do you see any other alternatives to this? |
|
Fair point, I was under the impression that those functionalities would also be impacted by this missing implementation. Thank you for further clarifying 🥇 |
this._mocker.queueMock is not a function
We looked into this more at Cypress and arrived at a similar conclusion - the whole "ESM mocking" is hard to solve generally. We will solve it for Vite and webpack via a plugins, I will link the code here once we write it, maybe we can collaborate with the Vitest team to build a general |
@lmiller1990 I believe it'd be a nice idea, so other libraries and applications can use it easily. |
Just a quick plug here that in ESM, importing modules is fetching them via HTTP, and you can use libraries like MSW to intercept module requests and respond to them with whichever cc @sheremet-va |
Hm, this might actually be easier than the current implementation 🤔 The current one also doesn't work if you import original module that imports itself somewhere in the import chain, but we can bypass that with MSW maybe 🤔 |
I've I would would have just known about MSW earlier in my life 😭 this is amazing! |
@sheremet-va, feel free to add me to reviewers if you ever decide to give MSW a shot in module mocking. I will be happy to help you spot mistakes and suggest improvements. @christian-bromann, but you've discovered it now, and that all that matters! ❤️ |
The first version is already implemented (and merged) here: #5853 |
Describe the bug
The following test:
throws an error:
Reproduction
see above
System Info
Used Package Manager
npm
Validations
The text was updated successfully, but these errors were encountered: