29

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.

Penny Liu
  • 15,447
  • 5
  • 79
  • 98
Madhu Sudhan Subedi
  • 469
  • 1
  • 6
  • 13

3 Answers3

22

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],
})
Penny Liu
  • 15,447
  • 5
  • 79
  • 98
Edward
  • 5,148
  • 2
  • 29
  • 42
  • 3
    If I wanna mock the plugin used by component,what's the next step? – 葛厚德 Dec 10 '17 at 08:24
  • 2
    This does not work for me, and i don't see anything in the vue-test-utils code related to this. How can i mock the mixin that i register into a SPC? No matter what i do the mixin's `mounted` method always runs. – mlohbihler Nov 09 '18 at 22:51
  • 4
    Then how do you call the methods? with `this.myMethod`? – Jalil May 27 '19 at 10:00
  • 1
    @Edd how do these techniques relate to globally registered mixins vs locally imported and registered mixins? I want to mock all instances of a locally used mixin as simply as possible, but not sure the best approach – Damon Jul 24 '19 at 13:23
  • 6
    The provided answer does not mock the mixin. It only explains how to install the mixin in the localVue instance. – onekiloparsec Mar 14 '21 at 13:08
  • 1
    who has solution in vue3, vue3 didn't has local vue – Lonyui Nov 06 '21 at 12:48
1

For those using Vue 3 with Vue Test Utils, then you just need to mock the individual method, for example with Jest. Pass in your myMixin as normal, and then spy on the method you want to mock:

    const wrapper = mount(Post, {
        global: {
            mixins: [myMixin],
        },
    } as any)

    jest.spyOn(wrapper.vm, 'myMixinMethodToMock').mockImplementation()

Note that Jest mocks it without caring that the method is on the mixin, not the Vue component.

Paul F. Wood
  • 1,453
  • 16
  • 19
0

I managed to mock the mixin methods with jest spies like this:

/// MyComponent.spec.js
describe('MyComponent', () => {
  let wrapper
  let localVue
  let store
  let spies = {}
  
  beforeEach(async () => {
    spies.mixinMethodName = jest.spyOn(MyComponent[1].methods, 'spies.mixinMethodName')
    ({ localVue, store } = (... custom factory ...)
    wrapper = await shallowMount(MyComponent, { localVue, store })
  })

  it('check mixin methods calls', () => {
    expect(spies.mixinMethodName).toHaveBeenCalled()
  })
})

Of course the spies object and its attached method can be customized as your wish.

The weakness of this method is that it relies on the order of the mixins entered in the real Vue component. For this example, this looks like:

/// MyComponent.vue
<script>
  export default {
    components: { ...components... },
    mixins: [mixin1, mixin2ToBeTested],
    data () {}
    ....
}
</script>
onekiloparsec
  • 2,013
  • 21
  • 32