Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.

like image 891
Madhu Sudhan Subedi Avatar asked Nov 16 '17 12:11

Madhu Sudhan Subedi


People also ask

How do you test Vue mixins?

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.

What is Vue test utils?

Vue Test Utils is the official unit testing utility library for Vue. js. This is the documentation for Vue Test Utils v1, which targets Vue 2 and earlier.


1 Answers

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], }) 
like image 148
Edward Avatar answered Sep 21 '22 01:09

Edward