title: How to mock a plugin in Jest
body: <p>My unit test is not setup properly, meaning that I am not correctly mocking my imported plugin function.</p>
  
  <p>How do I correctly mock my <code>logData</code> function? In the plugin the function returns undefined and that is by choice. I just need to make sure I <code>console.log</code> whatever is passed to it.</p>
  
  <p>The current error I get is "Cannot spy the logData property because it is not a function; undefined given instead"</p>
  
  <p><strong>logData.js</strong> - the plugin (just a wrapper around console.log statements)</p>
  
  <pre><code>export function logData (dataToLog) {
    const isLoggingData = localStorage.getItem('isLoggingData')
    if (isLoggingData) {
      console.log(dataToLog)
    }
  }
  
  export default {
    install: function (Vue) {
      Vue.prototype.$logData = logData
    }
  }
  </code></pre>
  
  <p><strong>logData.spec.js</strong> - I mocked <code>localStorage</code> but I need to mock the plugin <code>logData</code></p>
  
  <pre><code>import Vue from 'vue'
  import { createLocalVue } from '@vue/test-utils'
  import logData from './logData'
  
  class LocalStorageMock {
    constructor () {
      this.store = {}
    }
    getItem (key) {
      return this.store[key] || null
    }
    setItem (key, value) {
      this.store[key] = value.toString()
    }
    removeItem (key) {
      delete this.store[key]
    }
  }
  
  global.localStorage = new LocalStorageMock()
  
  const localVue = createLocalVue()
  const dataToLog = 'data to be logged'
  localVue.use(logData)
  
  const mockLogDataFunction = jest.spyOn(localVue.prototype, 'logData')
  
  describe('logData plugin', () =&gt; {
    // PASSES
    it('adds a $logData method to the Vue prototype', () =&gt; {
      console.log(wrapper.vm.$logData)
      expect(Vue.prototype.$logData).toBeUndefined()
      expect(typeof localVue.prototype.$logData).toBe('function')
    })
    // Now passes
    it('[positive] console.logs data passed to it', () =&gt; {
      global.localStorage.setItem('isLoggingData', true)
      const spy = jest.spyOn(global.console, 'log')
      localVue.prototype.$logData('data to be logged')
      expect(spy).toHaveBeenCalledWith(dataToLog)
      // expect(mockLogDataFunction).toHaveBeenCalled()
      // console.log(localVue)
      // expect(localVue.prototype.$logData(dataToLog)).toMatch('data to be logged')
    })
    // PASSES
    it('[negative] does not console.log data passed to it', () =&gt; {
      const spy = jest.spyOn(global.console, 'log')
      global.localStorage.removeItem('isLoggingData')
      localVue.prototype.$logData(dataToLog)
      expect(spy).not.toHaveBeenCalled()
  
      // const spy = jest.spyOn(this.$logData, 'console')
      // expect(localVue.prototype.$logData(dataToLog)).toBe(und efined)
      // expect(spy).toBe(undefined)
    })
  })
  </code></pre>