[Vuejs]-Mock .get() Function using Jest on VueJS

0👍

Assuming you have getPost() defined in the Post component’s methods, you can’t use named imports to access getPost. Instead, you’ll have to mount the component, and use the wrapper’s vm:

// Post.spec.js
import { shallowMount } from '@vue/test-utils'
import Post from '@/views/Post.vue'

it('...', () => {
  const wrapper = shallowMount(Post)
  await wrapper.vm.getPost()
  expect(wrapper.vm.post).toEqual(...)
})

Also make sure to return the axios call in getPost() so that it could be awaited:

// Post.vue
export default {
  methods: {
    getPost() {
      this.refreshToken();
        👇
      return http.get(/*...*/)
        .then(/*...*/)
        .catch(/*...*/);
    }
  }
}

Leave a comment