Skip to content

How can I modify the value of state in pinia in vue3 component test and affect the component? #1130

Discussion options

You must be logged in to vote

This has been resolved using jest.mock().

import { mount } from "@vue/test-utils";
import { createPinia } from "pinia";
import HelloWorld from "@/components/HelloWorld.vue";

jest.mock("@/stores/test", () => {
    const { defineStore } = require("pinia");
    const useTestStore = defineStore("test", { state: () => ({ title: "xxxxx" }) });
    return { useTestStore };
});

test("pinia in component test", () => {
    const wrapper = mount(HelloWorld, {
        global: { plugins: [createPinia()] },
    });
    expect(wrapper.text()).toBe("xxxxx");
});

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@ichbinbobby
Comment options

@lance-cheng-asus
Comment options

Answer selected by AilurusRP
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
3 participants