I want to test the testMethod in Test.vue, but testMethod had used mixin which import from App.js.
Test.vue
<script>
export default {
methods: {
testMethod() {
return 'get'+this.getTestMixin();
}
},
};
</script>
mixins/common.js
export default {
methods: {
getTestMixin() {
return 'test';
},
},
};
How to mock the mixin? I tried to mock the mixin like following but failed, any idea where am I doing wrong?
function getTestMixin() {
return 'test';
}
const localVue = createLocalVue()
localVue.mixin(getTestMixin)
const wrapper = shallowMount(Test, {
localVue,
})
error message is like following:
TypeError: Cannot read property 'props' of undefined
46 | beforeEach(() => {
> 47 | wrapper = shallowMount(Test, {
| ^
48 | localVue,
49 | });
50 | });
Mixins can be tested by creating a mock component and registering the mixin on the component. Mixins can be applied globally or locally on a component. You can register filters and mixins before running unit tests. You can test filters and mixins in components by testing component output.
As mentioned earlier, a mixin is just a Javascript (or TypeScript) file with a Vue component config object. export default { data() { return { /* data */ } }, methods: { some_method() { } }, // etc. } We can export the component directly (like above), or we can store it in a variable and export that variable.
Mixins can be mocked if they are replaced before shallow mount. Something like:
const localVue = createLocalVue();
const mockMixin = {
methods: {
mixinMethod() {
return 'test';
},
}
}
const MockedTestComponent = { ...TestComponent, mixins: [mockMixin] };
const wrapper = shallowMount(MockedTestComponent, {
localVue,
});
expect(wrapper.vm.mixinMethod()).toEqual('test');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With