Search code examples
javascriptvue.jsunit-testingjestjsmixins

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


I have read the documentation for vue-test-utils and Jest, but I am still unsure about how to properly mock Vue mixins in a Vue component and test the component.


Solution

  • 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],
    })