Search code examples
javascriptunit-testingvue.jsvue-test-utils

Vue Unit Testing - Mocking a plugins return value


I have a custom plugin that returns a boolean. Rather than importing the plugin to the test, I want to mock this value. I can mock the plugin easily enough, but how can I change the return value in the test?

const $mq = () => {};    
wrapper = shallowMount(Component, { 
  //...
  mocks: {
    $mq
  }
  //...
});

Test

it ('Test description', () => {
  wrapper.vm.$mq = () => true; // HOW TO MOCK PLUGIN RETURNS???
});

Solution

  • Incase anyone comes along this, you need to change the value before providing it to the mocks option.

    const $mq = () => true;    
    wrapper = shallowMount(Component, { 
      //...
      mocks: {
        $mq
      }
      //...
    });