Skip to content

Pinia plugins not working inside tests #3003

Closed Answered by leonheess
leonheess asked this question in Help and Questions
Discussion options

You must be logged in to vote
import { it, expect, vi } from 'vitest';
import { createPinia, setActivePinia, defineStore } from 'pinia';
import { createApp, defineComponent, h, KeepAlive } from 'vue';

it('tests', async () => {
  const foo = vi.fn();

  const pinia = createPinia().use(({ store }) => foo(store.$id));
  setActivePinia(pinia);

  createApp(defineComponent(() => () => h(KeepAlive, {}))).use(pinia); // this is needed for the store to work

  const useTestStore = defineStore('store', { state: () => ({ foo: 1 }) });
  const store = useTestStore();

  expect(foo).toHaveBeenCalled();
});

Replies: 2 comments 2 replies

Comment options

You must be logged in to vote
1 reply
@leonheess
Comment options

Comment options

You must be logged in to vote
1 reply
@posva
Comment options

Answer selected by leonheess
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #3002 on July 10, 2025 00:36.