-
Notifications
You must be signed in to change notification settings - Fork 244
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
Feature request: global.mocks to also mock into the script setup, not just the template #2119
Comments
I already tested this and have a feature branch for this. |
Hi @jrutila I'm not sure we need this, as Nuxt is bit special. Maybe you're looking for https://github.com/antfu/unplugin-auto-import (see vitest-dev/vitest#1565). You can also check out nuxt-vitest which may help (from Daniel Roe who is one the Nuxt contributor) |
For example https://github.com/danielroe/nuxt-vitest#mocknuxtimport might be what you need |
Thanks for the hints for Nuxt. I will look into them. On the other hand, including the mocked variables in the global doesn't really break anything, and if there are any other special frameworks the users could achieve the global mocking without needing external libraries. In the end, a unit test tool should be strong enough to let you do "stupid" things because you might be dependent on a library that is doing stupid things. |
I think that should already be the case, as |
Check out my pull request. There is a unit test case that shows how it is supposed to work. |
I tried these and have to say they are quite complex to set up for my case as all I want to do is mock the global I will, for now, build my own fork with the feature so that I get forward. Maybe someday Cypress even supports out-of-the-box Nuxt component testing. |
Okay, apparently Cypress.Commands.add("mount", (MountedComponent, options) => {
const fetchMethod = async (_url) => ({
data: "mocked",
})
const fetchPlugin = {
install(_app, _options) {
globalThis.$fetch = fetchMethod
},
}
options.global.plugins.push(fetchPlugin)
return mount(MountedComponent, options)
}) Now the |
Hi @jrutila, |
Yeah, and you can use a plugin to achieve this same.
|
Still going over the thread and understanding the use case. The concern I have is Nuxt and Test Utils would be injecting That said -- I am surprised this doesn't already work. I thought you could mock Vuex using: mocks: {
$store: /* mock store */
} which would be assigned to Edit: yes this works: https://github.com/lmiller1990/vue-testing-handbook/blob/master/demo-app-vue-3/tests/unit/ComponentWithButtons.spec.js#L51-L66. I understand the issue better now - it's not about mocking One option would be a new API, like mount(Comp, {
globalThis: ...
}) But isn't this already supported by test runners like Jest / Vitest? If you are using Cypress, why not just use It'd sure be nice to get first class Nuxt support in Cypress. We talked about it a lot here cypress-io/cypress#23619. Maybe you saw this already? It's complex, since Nuxt does a lot of things under the hood, we need to hook into the process, which isn't exactly a public API. Also, there is the whole SSR element to consider - we don't really have a good way to spin up the Nuxt server and manage all that. I work at Cypress, and on Test Utils, I am well positioned to help solve this issue either here, there, or in some other fashion. I'm curious why
If you do want to contribute to |
Happy to leave this as feature request for now - I'd like to see it addressed more broadly, probably "how best to handle auto injected global variables" or something -- since this is a general problem to solve with the auto import stuff that's becoming popular (personally not a fan - it makes it really hard to onboard people and IDE support is kind of meh, I much prefer just importing the code 🤷 ) |
I would if I would be doing e2e testing with Cypress. But this is now component testing where I have to manually build the nuxt component support. And part of that is mocking, for example, the |
Right, I see. According to unplugin-auto-import this is in Nuxt out of the box. If you add that plugin to your I looked into this. I was able to reproduce what Nuxt does using Nuxt's imports are here: https://github.com/nuxt/nuxt/blob/2edd7a34419e042f6a694929d32178ecb379be51/packages/nuxt/src/imports/presets.ts#L15 but it doesn't have What do you think? |
I am coding with Nuxt and would like to component test following component:
The problem is the
$fetch
call that is with Nuxt freely available to use in the component. I would like to mock the$fetch
method.The test would then be like this:
The solution would be that when I mock the
$fetch
it will become available also in the script setup. At the moment it is only available to use in the template code.The text was updated successfully, but these errors were encountered: