Skip to content
Tutorials Center
My learning

Topics

Lesson 26 of 38 ยท Testing Stores

Testing Components with createTestingPinia

Test Vue components that use Pinia: mount with createTestingPinia, stub actions with spies, set initialState, override getters and decide when to run real actions.

Store tests prove the store is correct. Component tests prove the component uses it correctly: it shows the right state, and clicking a button calls the right action. For that you do not want the real action running (it might call an API); you want to check that it was called. The official package @pinia/testing gives you createTestingPinia() for exactly this.

The package and Vue Test Utils need npm, so the real code below is read-only. Afterwards you will run a hand-written stand-in that behaves like createTestingPinia in the ways that matter, so you can see the idea working in the browser.

The real setup

Terminal
npm install -D @pinia/testing
JavaScript
import { mount } from "@vue/test-utils";
import { createTestingPinia } from "@pinia/testing";
import { vi, it, expect } from "vitest";
import Counter from "./Counter.vue";
import { useCounterStore } from "../stores/counter";

it("calls increment when the button is clicked", async () => {
  const wrapper = mount(Counter, {
    global: {
      plugins: [createTestingPinia({ createSpy: vi.fn })],
    },
  });

  const counter = useCounterStore();   // same instance the component uses

  await wrapper.find("button").trigger("click");

  expect(counter.increment).toHaveBeenCalledTimes(1);
});

What createTestingPinia does for you:

  1. Creates a pinia and sets it as the active pinia, so useCounterStore() in your test returns the store the component sees.
  2. Stubs every action with a spy (createSpy tells it which kind: vi.fn for Vitest, jest.fn for Jest). The real action body does not run.
  3. Lets you set initial state for any store.
  4. Makes getters writable, so you can force a computed value.

If you use Vitest with globals: true, createSpy is detected automatically and you can omit it.

Initial state

Instead of calling actions to build up state, start the test with the state you want:

JavaScript
createTestingPinia({
  initialState: {
    counter: { count: 20 },        // key is the store id
    cart: { items: [{ id: 1, qty: 2 }] },
  },
});

The state is applied before the component renders, which makes tests short and focused.

Stubbing and not stubbing actions

By default actions are stubbed: they record calls and do nothing. That is perfect when you only want to check that the component calls them. To run real actions, switch stubbing off:

JavaScript
createTestingPinia({ stubActions: false });

Now counter.increment() really changes state, and you can assert on the rendered output. You can still spy on a single action with vi.spyOn(counter, "increment").

You can also give a stubbed action a return value:

JavaScript
counter.fetchUser.mockResolvedValue({ id: 1, name: "Ada" });

Overriding getters

In a test pinia, getters are writable. Set one to a value to test how the component renders it, no matter what the state is:

JavaScript
const counter = useCounterStore();
counter.double = 999;                 // force the getter
await nextTick();
expect(wrapper.text()).toContain("999");

counter.double = undefined;           // go back to the real getter

Plugins in tests

If your app registers plugins, tests should too, or the stores will miss properties:

JavaScript
createTestingPinia({ plugins: [myPlugin] });

A runnable stand-in

The editor below defines createTestingPiniaStandIn, a tiny imitation with initialState and stubActions, then tests a component the way the real code does. The component, the store and Pinia are real. The testing helper and the "click" are hand-written. (The real library supports much more, for example stubbing $patch.)

Component test with a stand-in testing pinia

Test 1 shows the default mode: state starts at 20, the click is recorded, and nothing changes because the action is a stub. Test 2 shows stubActions: false.

Which kind of test when?

You want to know Use
Does the action compute the right result? A store test (previous lesson)
Does the component call the action when clicked? createTestingPinia with stubbed actions
Does the whole feature work together? createTestingPinia({ stubActions: false }) or a real pinia

Keep logic in stores and keep components thin. Components with little logic need only a few tests; stores hold the complicated tests.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does createTestingPinia do to actions by default?

    By default actions are stubbed with spies, so you can assert they were called without running their bodies.
  2. 2. How do you let real actions run in a component test?

    stubActions: false keeps the real action implementations.
  3. 3. What is initialState used for?

    initialState is an object keyed by store id whose values are applied to the state before the component renders.
  4. 4. Why does the test call useCounterStore() after mount?

    createTestingPinia sets the active pinia, so useStore() in the test returns the very store the component received.

Summary

  • createTestingPinia() from @pinia/testing is passed as a plugin to mount().
  • It stubs actions with spies, applies initialState and makes getters writable.
  • Use stubActions: false to run real actions.
  • Register your own plugins in tests with the plugins option.
  • Test store logic in store tests and component wiring with a testing pinia.

Next: Mocking Actions and APIs in Tests