Last active
          October 11, 2024 11:06 
        
      - 
      
- 
        Save acabreragnz/bc04829b9f77d596f21476faf9b94385 to your computer and use it in GitHub Desktop. 
    Mock a pinia store using vitest
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | 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