Reputation: 1488
Now, I have the requirement of writing test cases to Pinia store actions which contains 3rd party API's with async/await.
My project has written with the technologies of Vue3 + Vuetify 3 + VITE + Pinia + TypeScript. Kindly someone help me to provide proper configuration to write Jest test cases for the project which is suitable for this combination of technologies. Otherwise, kindly help me to write test cases for testing only PINIA stores which is suitable for 3rd party API integration with async/await calls.
I have also tried the approaches suggested in official PINIA testing article but no luck.
Due to security issues (code is in VDI), I can't post the code here.
Upvotes: 0
Views: 788
Reputation: 1488
Finally, I made a POC and found the solution for writing test case to mock async/await action of PINIA store.
Store.ts
import { defineStore } from "pinia"
import fetch from "node-fetch"
export const useUserStore = defineStore('userStore', {
state: () => ({
}),
actions: {
async getData() {
const data = await fetch('https://jsonplaceholder.typicode.com/users/1')
console.log(data)
return data
}
},
getters: {
},
persist: true
})
test.spec.ts
import { useUserStore } from '../src/store/user'
import { beforeEach, describe, expect, it } from 'vitest';
import { createPinia, setActivePinia } from 'pinia';
beforeEach(() => {
setActivePinia(createPinia())
})
describe('Users Test', () => {
it('Async Await testing with 3rd party API', () => {
const user = useUserStore()
const data = user.getData()
expect(data).toBeTruthy()
})
})
Upvotes: 0