Vitest with Pinia doesn't call action's store in onMounted hook. #1288
-
Component <template>
Hello
</template>
<script setup>
import { onMounted } from 'vue'
import { useListasSumariasStore } from '@/stores/listasSumarias'
const listasSumariasStore = useListasSumariasStore()
onMounted(() => {
listasSumariasStore.recuperarListasEmElaboracao()
})
</script> Store import { defineStore } from 'pinia'
export const useListasSumariasStore = defineStore({
id: 'listas',
state: () => ({
listas: []
}),
actions: {
recuperarListasEmElaboracao() {
console.log('recuperarListasEmElaboracao()')
}
}
}) Test import { describe, it, expect } from 'vitest'
import { mount } from '@vue/test-utils'
import { createTestingPinia } from '@pinia/testing'
import ListasSumarias from '../ListasSumarias.vue'
describe('ListasSumarias', () => {
it('renders properly', async () => {
const wrapper = mount(ListasSumarias, {
global: {
plugins: [createTestingPinia()]
}
})
expect(wrapper.text()).toContain('Hello')
})
}) The action "recuperarListasEmElaboracao()" isn't called. |
Beta Was this translation helpful? Give feedback.
Answered by
posva
May 12, 2022
Replies: 2 comments 1 reply
-
Actions are stubbed by default: https://pinia.vuejs.org/api/interfaces/pinia_testing.TestingOptions.html#stubactions |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
ffvaz
-
I had the same situation and this is what helped me - just write store = useSomeStore() after component mount. import { createTestingPinia } from "@pinia/testing";
import { useSomeStore} from "@/stores/someStore"; describe("MyPage component", () => {
let wrapper: VueWrapper<any>;
let store: ReturnType<typeof useSomeStore>;
beforeEach(() => {
vi.clearAllMocks();
wrapper = shallowMount(MyPage, {
global: {
plugins: [createTestingPinia()]
},
});
store = useSomeStore();
}); it("Should call loadSomeDate on onMounted hook", () => {
// Act
// Some additional mocks, for example currentRoute
wrapper = shallowMount(MyPage, {
global: {
plugins: [createTestingPinia()]
},
});
store = useSomeStore(); // It helps me!**
// Assert
expect(store .loadSomeDate).toHaveBeenCalled();
}); |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Actions are stubbed by default: https://pinia.vuejs.org/api/interfaces/pinia_testing.TestingOptions.html#stubactions