How to mock Vue Mixins during unit testing using vue-test-utils and jest?

Madhu Sudhan Subedi picture Madhu Sudhan Subedi · Nov 16, 2017 · Viewed 10.8k times · Source

I read vue-utils-test documentation 3 times and documentation of jest too, But I do not get idea how exactly mock the vue mixins in vue component and test the component.

Answer

Edd picture Edd · Nov 21, 2017

There are two ways:

  1. You can use createLocalVue, and register a mixin on that localVue class:
const localVue = createLocalVue()
localVue.mixin(myMixin)

const wrapper = shallow(Post, {
    localVue,
})
  1. You can pass mixins in the mounting options:
const wrapper = shallow(Post, {
    mixins: [myMixin],
})