my stack:
cypress
@vue/test-utils
component in vue3 in composition api (<script setup />)
My test, i would like to spy a method inside a component, and spy action vuex inside a method in component.
myComponent
testView.vue
<template>
<div class="test" @click="a()">test </test>
<template>
<script setup>
function a() {
// logic
b()
}
function b() {
// logic
// launch dispatch in simple action in vuex
dispatch('actionA')
}
</script>
test.cy.js
describe('ClientView.vue', () => {
it('test', async () => {
const store = createStore({
state() {
return {
varA: null,
}
},
actions: {
actionA(context, newMessage) {
return Promise.resolve()
},
},
})
const mockRoute = createRouter({ history: createWebHistory(), routes: [] })
mockRoute.currentRoute.value.name = 'testUrl'
mockRoute.currentRoute.value.params = undefined
const wrapper = mount(testView, {
global: {
mocks: {
$route: mockRoute,
},
plugins: [store, mockRoute]
},
})
const spyA = cy.spy(wrapper.vm, 'a').as('a')
const spyB = cy.spy(wrapper.vm, 'b').as('bSpy')
// simulate click and launch function a()
// I see in my log, the function A, b and the action is called
wrapper.find('test').trigger('click')
expect(spyA ).to.be.called
expect(spyB ).to.be.called
expect(stubB ).to.be.called
})
})
My errors
spyA is called ok
expected spyB to have been called at least once, but it was never called
expected stubB to have been called at least once, but it was never called
My declaration of my spy and stub for b is not correct ? i don't understand why.
// If a modify my template
testView.vue
<template>
<div class="test" @click="a()">test </test>
<template>
<script setup>
function a() {
// logic
this.b()
}
function b() {
// logic
// launch dispatch in simple action in vuex
dispatch('actionA')
}
</script>
My test work ... but ... if i run my frontend 'this' is not autorize.
Error frontend
Uncaught TypeError: Cannot read properties of undefined (reading 'b')
Cypress has a problem with the version of vue3 in composition api ?
wrapper.vmprovides access to internal functions. Do you see it when inspecting?