A simple component:
<template>
<div>
<p>
{{ count }}
</p>
<button @click="increment" data-test="increment">Increment</button>
</div>
</template>
<script>
export default {
data () {
return {
count: 0
}
},
methods: {
increment () {
this.count++
}
}
}
</script>
And my test:
import TestExperiment from '@/components/TestExperiment'
import { createLocalVue, shallowMount } from '@vue/test-utils'
const localVue = createLocalVue()
describe('testexperiment.test.js', () => {
const cmp = shallowMount(TestExperiment, {
localVue
})
const increment = jest.spyOn(cmp.vm, 'increment')
const incrementButton= cmp.find('[data-test="increment"]')
test('clicking increment button calls increent', () => {
expect(incrementButton.exists()).toBe(true)
incrementButton.trigger('click')
// Checking call here fails:
// expect(increment).toHaveBeenCalled()
// Function was still obviously called
expect(cmp.vm.count).toBe(1)
incrementButton.trigger('click')
// Checking call here passes:
expect(increment).toHaveBeenCalled()
})
})
As you can see, I trigger a click on incrementButton twice.
After the first call, if I test if the method 'increment' was called, it returns false. However, count is indeed incremented. After the second call, it registers that it was in fact called (if I test how many times it was called, it asserts that it was called one time, even if count is 2, clearly having been incremented twice).
What am I missing about how Jest/Vue works?