You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hey, firstly thank you for making this plugin, it looks very useful :)
Running this module seems to run the whole nuxt environment, including the plugins, and we have a plugin that makes a api call. We normally just mock the module where we import the api function but it doesn't seem to work in this case. For example:
// somePlugin.ts
import { fetchProducts } from "~/service/products";
export default defineNuxtPlugin(async () => {
const products = fetchProducts();
// do something with the products
});
// SomeComponent.nuxt.spec.js
import { describe, it, expect, vi } from "vitest";
import { shallowMount } from "@vue/test-utils";
import SomeComponent from "../SomeComponent.vue";
// trying to mock the products service so that the real api doesn't get called during the plugins
vi.mock("~/service/products");
describe("SomeComponent", async () => {
it("Is a Vue instance", () => {
const wrapper = shallowMount(SomeComponent);
expect(wrapper.vm).toBeTruthy();
});
});
I assume these mocks only work for the component that we are testing and not for the plugins in the nuxt environment for example. Do you have a suggestion about how to solve this?
Thanks! Indeed we could do this but I personally like not having any test related logic in the actual code so I would definitely prefer somehow moving this to the vitest config or the test files themselves :/
I'm using a plotting library in my project that is installed as a client side only plugin using the name convention (yourplugin.client.ts) since it uses browser API's. When running tests I get errors indicating the plugin is being executed in an environment that doesn't support it. Could it be that I need additional steps to get client side plugins to behave properly? Being able to mock this all together would be great since none of the things I want to test rely on this plugin. This is my code:
@danielroe Would it be possible to have something that would allow us to replace a plugin in tests?
Api would be akin to
// In the test
import { overridePlugin } from 'nuxt-vitest'
it('allows me to do things that have great power', () => {
overridePlugin('something', () => {
...function that would override the plugin and receive the same arguments
})
})
Reacted by Paul Padian, Atsushi Matsue, johanvonbronx, Lilian, Yutaro Hiraoka, Frallen, Falko Krause and Vinod Kumar
Activity
joel-wenzel commentedon May 28, 2023
I had a similar problem, my rest client is setup in a nuxt plugin and a subsequent plugin uses that client to make an app init api call.
In my rest client plugin i have something like the following:
How you actually mock the requests is up to you
gbyesiltas commentedon May 30, 2023
Thanks! Indeed we could do this but I personally like not having any test related logic in the actual code so I would definitely prefer somehow moving this to the vitest config or the test files themselves :/
[-]How to mock modules used in plugins?[/-][+]mocking plugins used within nuxt[/+]danielroe commentedon Jun 2, 2023
it's worth noting that you can mock api routes with
registerEndpoint: https://github.com/danielroe/nuxt-vitest#registerendpoint.There may be other reasons you would want to mock a plugin, and I think it should be possible, so leaving this issue up until we document it.
Nicolas-Yazzoom commentedon Aug 2, 2023
I'm using a plotting library in my project that is installed as a client side only plugin using the name convention (
yourplugin.client.ts) since it uses browser API's. When running tests I get errors indicating the plugin is being executed in an environment that doesn't support it. Could it be that I need additional steps to get client side plugins to behave properly? Being able to mock this all together would be great since none of the things I want to test rely on this plugin. This is my code:Austio commentedon Oct 15, 2023
@danielroe Would it be possible to have something that would allow us to replace a plugin in tests?
Api would be akin to
danielroe commentedon Nov 28, 2023
@adam-gipril Yes, we should build this in in future. (Adding setup files easily.) For now, you can likely implement with this hack:
Frallen commentedon Jul 23, 2024
@Austio
I found solution on YouTube
In short, you need to mock all the elements

MikeBellika commentedon Jun 12, 2025
Managed to get it working based on these docs: https://test-utils.vuejs.org/api/#global-mocks
I have this plugin
I then mock it like this:
Don't know if there are any downsides with this method but it works for me.
Without it I get this error: