title: How to Unit Test a Method in a Vue.js Component using jest
body: <p>I'm trying to unit test a component method. The question <a href="https://stackoverflow.com/questions/51520835/vue-and-jest-unit-testing-components">here</a> does not lay out how to access the component method from a unit test. </p>
  
  <p>Specifically, given my Vue component below, how do I access <code>doSomeWork()</code> from my unit test?</p>
  
  <p><em>Vue component:</em></p>
  
  <pre class="lang-html prettyprint-override"><code>&lt;template&gt;
      &lt;div id="ThisStuff"&gt;
          &lt;span&gt;
              Some other stuff is going on here
          &lt;/span&gt;
      &lt;/div&gt;
  &lt;/template&gt;
  
  &lt;script&gt;
      import foo from 'bar'
  
      export default {
          props: {
              ObjectWithStuffInIt: [
                  {
                      id: 1
                      bar: false
                  },
                  {
                      id: 2
                      bar: false
                  },
              ]
          },
          data: {
              foo: "foo"
          },
          methods: {
              doSomeWork: function() {
                  for (var i = 0; i &lt; ObjectWithStuffInIt.length; i++) { 
                      if (foo === "diddly") {
                          ObjectWithStuffInIt[i].bar = true;
                      }
                  }
              }
          }
      }
  &lt;/script&gt;
  </code></pre>
  
  <p><em>My test code:</em></p>
  
  <pre class="lang-js prettyprint-override"><code>import {createLocalVue, shallow} from 'vue-test-utils'
  import ThisVueFile.test.js from '../../thisPlace/ThatPlace/ThisVueFile.vue'
  import Vuex from 'vuex'
  
  const localVue = createLocalVue()
  localVue.use(Vuex);
  
  describe('ThisVueFile.test.js', () =&gt; {
      let user;
      let store;
  
      beforeEach(() =&gt; {
          let getters = {
              user: () =&gt; user
          }
  
          store = new Vuex.Store({ getters })
      })
  
      // I need to fill propsData: with some local data here 
      //     because it is server data
      // I need to have access to the method
      // I need to use local data for `foo` in the test. 
  
      it(' When foo is set to -diddly- then set bar to true ', () =&gt; {
          foo = "diddly";
          // run the method in the component here 
          doSomeWork();
  
          expect(OjbectWithStuffInIt[0].bar.equals(true));
      })
  })
  </code></pre>