最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Unit Testing Vue 3 Component that uses Pinia with Vue Testing Library - Stack Overflow

programmeradmin0浏览0评论

I am struggling to understand how I can test the rendering of items based on the results of a call to a Pina getter in my Vue 3 app (using the Vue Testing Library). An example of what I want to achieve might be something like:

<div v-for="contact in store.getContacts()">
      <p>{{contact.firstName}} {{ contact.lastName }}</p>
</div>

where I want to test that all the names of the contacts are displayed.

My app uses Vitest and @testing-library/vue so in my test spec for the above test I might have:

import { cleanup, getByRole, render, screen } from "@testing-library/vue";
import { describe, it, vi, beforeEach, afterEach } from "vitest";
import MyComponent from "@/ponents/MyComponent.vue";
import { createTestingPinia } from "@pinia/testing";

describe("MyComponent.vue", () => {

  let getByText: any, getByRole: any;

  beforeEach(() => {
    ({ getByText } = render(MyComponent, {
      global: {
        plugins: [
          createTestingPinia({
            initialState: {
              contacts: [{firstName:"Bill",lastName:"Jones"},{firstName:"Peter",lastName:"Smith"}],
            },
            stubActions: false,
            createSpy: vi.fn,
          }),
        ],
      },
    }));
  });

  afterEach(() => {
    cleanup();
  });
  
  it("should display the first name", () => {
    getByText("Bill Jones");
  });

}); 

in the store I have the following state setup:

interface Student {
  "id": number;
  "firstName": string;
  "lastName": string;
  "house": string;
  "year": number;
  "class": string;
}

interface ContactState {
  contacts: Student[];
}

export const useContactStore = defineStore({
  id: "contact",
  state: ():ContactState => ({
    contacts: [],
}),

and a getter:

getContacts: (state) => {
      return state.contacts;
}

I think I have setup the initial state of the store correctly (??) but the getter does not seem to be working - what else do I need to do here? Do I have to mock the getter or something? Or maybe I am not initialising the store correctly in the test?

(my app is all working fine, it's the test I cannot get to work)

I am struggling to understand how I can test the rendering of items based on the results of a call to a Pina getter in my Vue 3 app (using the Vue Testing Library). An example of what I want to achieve might be something like:

<div v-for="contact in store.getContacts()">
      <p>{{contact.firstName}} {{ contact.lastName }}</p>
</div>

where I want to test that all the names of the contacts are displayed.

My app uses Vitest and @testing-library/vue so in my test spec for the above test I might have:

import { cleanup, getByRole, render, screen } from "@testing-library/vue";
import { describe, it, vi, beforeEach, afterEach } from "vitest";
import MyComponent from "@/ponents/MyComponent.vue";
import { createTestingPinia } from "@pinia/testing";

describe("MyComponent.vue", () => {

  let getByText: any, getByRole: any;

  beforeEach(() => {
    ({ getByText } = render(MyComponent, {
      global: {
        plugins: [
          createTestingPinia({
            initialState: {
              contacts: [{firstName:"Bill",lastName:"Jones"},{firstName:"Peter",lastName:"Smith"}],
            },
            stubActions: false,
            createSpy: vi.fn,
          }),
        ],
      },
    }));
  });

  afterEach(() => {
    cleanup();
  });
  
  it("should display the first name", () => {
    getByText("Bill Jones");
  });

}); 

in the store I have the following state setup:

interface Student {
  "id": number;
  "firstName": string;
  "lastName": string;
  "house": string;
  "year": number;
  "class": string;
}

interface ContactState {
  contacts: Student[];
}

export const useContactStore = defineStore({
  id: "contact",
  state: ():ContactState => ({
    contacts: [],
}),

and a getter:

getContacts: (state) => {
      return state.contacts;
}

I think I have setup the initial state of the store correctly (??) but the getter does not seem to be working - what else do I need to do here? Do I have to mock the getter or something? Or maybe I am not initialising the store correctly in the test?

(my app is all working fine, it's the test I cannot get to work)

Share Improve this question edited Jan 3, 2023 at 9:38 snoob dogg 2,8954 gold badges36 silver badges67 bronze badges asked Aug 23, 2022 at 13:25 Richard ShergoldRichard Shergold 6321 gold badge10 silver badges24 bronze badges 2
  • Could you clarify one thing and maybe show some code, what does your store look like? – Anonymous User Commented Aug 28, 2022 at 6:48
  • I have made some edits above to show my store setup - thanks for looking in – Richard Shergold Commented Aug 28, 2022 at 16:49
Add a ment  | 

1 Answer 1

Reset to default 4

When you create the initial state using createTestingPinia, you also need to state the store id, also called name, which is in your case would be contact as seen in in your defineStore.

So in this case, I think what you should do is change the current code to this:

...
createTestingPinia({
    initialState: {
        contact: {
            contacts: [{firstName:"Bill",lastName:"Jones"},{firstName:"Peter",lastName:"Smith"}]
        },
     },
     stubActions: false,
     createSpy: vi.fn,
}),
...

This is not very well explained in the documentation, however, this should at least initialize the contacts array for you.

发布评论

评论列表(0)

  1. 暂无评论