Skip to content

Instantly share code, notes, and snippets.

@acabreragnz
Last active October 11, 2024 11:06
Show Gist options
  • Save acabreragnz/bc04829b9f77d596f21476faf9b94385 to your computer and use it in GitHub Desktop.
Save acabreragnz/bc04829b9f77d596f21476faf9b94385 to your computer and use it in GitHub Desktop.
Mock a pinia store using vitest
import { beforeEach, describe, it, expect } from 'vitest';
import { fn } from '@vitest/spy';
import { useSomeStore } from '@/useSomeStore';
import { createTestingPinia } from '@pinia/testing';
import { setActivePinia } from 'pinia';
describe('useCreditStore - Test with Mocked Getter', () => {
let mockCreditStore: Record<string, any>;
beforeEach(() => {
const pinia = setActivePinia(
createTestingPinia({
createSpy: fn,
})
);
mockCreditStore = useSomeStore(pinia);
});
it('should return the mocked creditAmount', () => {
// Override pinia getter
mockCreditStore.creditAmount = 5000;
expect(mockCreditStore.creditAmount).toBe(5000);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment