'How to test composable function

I am trying to Jest unit testing in Vue 3.

// Home.vue
...
<script lang="ts">
import { defineComponent, ref, watch } from 'vue'
import { useStore } from 'vuex'
...
import useTaskList from '@/composables/useTaskList'

export default defineComponent({
  name: 'TaskHome',
  components: {
    ...
  },
  setup() {
    const {
      fetchTaskList,
    } = useTaskList()
    fetchTaskList()
    ...
})
</script>
// Home.spec.ts
import Home from '../Home.vue'
import TestUtils from '@/lib/testing/TestUtils'

jest.mock('@/composables/useTaskList')

describe('Home', () => {
  describe('when component is mounted', () => {
    it('should fetch task list', async () => {
      const wrapper = await TestUtils.safeMount(Home)
      expect(wrapper.vm.fetchTaskList).toHaveBeenCalled()
    })
  })

But this fails and I get this error message.

    expect(received).toHaveBeenCalled()

    Matcher error: received value must be a mock or spy function

    Received has value: undefined

    > 19 |       expect(wrapper.vm.fetchTaskList).toHaveBeenCalled()

How can I fix this issue?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source