This book is written for Vue.js 2 and Vue Test Utils v1.

Find the Vue.js 3 version here.

Mutations and Actions

The previous guide discussed testing components that use $store.state and $store.getters, which both provide the current state to the component. When asserting a component correctly commits a mutation or dispatches an action, what we really want to do is assert $store.commit and $store.dispatch is called with the correct handler (the mutation or action to call) and payload.

There are two ways to go about this. One is to use a real Vuex store with createLocalVue, and another is to use a mock store. Both these techniques are demonstrated hereVuex in components - mutations and actions - 图1. Let’s see them again, in the context of mutations and actions.

The source code for the test described on this page can be found hereVuex in components - mutations and actions - 图2.

Creating the Component

For these examples, we will test a <ComponentWithButtons> component:

  1. <template>
  2. <div>
  3. <button
  4. class="commit"
  5. @click="handleCommit">
  6. Commit
  7. </button>
  8. <button
  9. class="dispatch"
  10. @click="handleDispatch">
  11. Dispatch
  12. </button>
  13. <button
  14. class="namespaced-dispatch"
  15. @click="handleNamespacedDispatch">
  16. Namespaced Dispatch
  17. </button>
  18. </div>
  19. </template>
  20. <script>
  21. export default {
  22. name: "ComponentWithButtons",
  23. methods: {
  24. handleCommit() {
  25. this.$store.commit("testMutation", { msg: "Test Commit" })
  26. },
  27. handleDispatch() {
  28. this.$store.dispatch("testAction", { msg: "Test Dispatch" })
  29. },
  30. handleNamespacedDispatch() {
  31. this.$store.dispatch("namespaced/very/deeply/testAction", { msg: "Test Namespaced Dispatch" })
  32. }
  33. }
  34. }
  35. </script>

Testing with a real Vuex store

Let’s write a ComponentWithButtons.spec.js with a test for the mutation first. Remember, we want to verify two things:

  1. Did the correct mutation get committed?
  2. Was the payload correct?

We will use createLocalVue to avoid polluting the global Vue instance.

  1. import Vuex from "vuex"
  2. import { createLocalVue, mount } from "@vue/test-utils"
  3. import ComponentWithButtons from "@/components/ComponentWithButtons.vue"
  4. const localVue = createLocalVue()
  5. localVue.use(Vuex)
  6. const mutations = {
  7. testMutation: jest.fn()
  8. }
  9. const store = new Vuex.Store({ mutations })
  10. describe("ComponentWithButtons", () => {
  11. it("commits a mutation when a button is clicked", async () => {
  12. const wrapper = mount(ComponentWithButtons, {
  13. store, localVue
  14. })
  15. await wrapper.find(".commit").trigger("click")
  16. expect(mutations.testMutation).toHaveBeenCalledWith(
  17. {},
  18. { msg: "Test Commit" }
  19. )
  20. })
  21. })

Notice the tests are marked await and call nextTick. See here for more details on why.

There is a lot code in the test above - nothing too exciting is happening, though. We create a localVue and use Vuex, then create a store, passing a Jest mock function (jest.fn()) in place of the testMutation. Vuex mutations are always called with two arguments: the first is the current state, and the second is the payload. Since we didn’t declare any state for the store, we expect it to be called with an empty object. The second argument is expected to be { msg: "Test Commit" }, which is hard coded in the component.

This is a lot of boilerplate code to write, but is a correct and valid way to verify components are behaving correctly. Another alternative that requires less code is using a mock store. Let’s see how to do that for while writing a test to assert testAction is dispatched.

Testing using a mock store

Let’s see the code, then compare and contrast it to the previous test. Remember, we want to verify:

  1. the correct action is dispatched
  2. the payload is correct
  1. it("dispatches an action when a button is clicked", async () => {
  2. const mockStore = { dispatch: jest.fn() }
  3. const wrapper = mount(ComponentWithButtons, {
  4. mocks: {
  5. $store: mockStore
  6. }
  7. })
  8. await wrapper.find(".dispatch").trigger("click")
  9. expect(mockStore.dispatch).toHaveBeenCalledWith(
  10. "testAction" , { msg: "Test Dispatch" })
  11. })

This is a lot more compact than the previous example. No localVue, no Vuex - instead of mocking the function, in the previous where we did testMutation = jest.fn(), we actually mock the dispatch function itself. Since $store.dispatch is just a regular JavaScript function, we are able to do this. Then we assert the correct action handler, testAction, is the first argument, and the second argument, the payload, is correct. We don’t care what the action actually does - that can be tested in isolation. The goal of this test is to simply verify that clicking a button dispatches the correct action with payload.

Whether you use a real store or a mock store is your tests is down to personal preference. Both are correct. The important thing is you are testing your components.

Testing a Namespaced Action (or Mutation)

The third and final example shows another way to test that an action was dispatched (or mutation committed) with the correct arguments. This combined both techniques discussed above - a real Vuex store, and a mocked dispatch method.

  1. it("dispatch a namespaced action when button is clicked", async () => {
  2. const store = new Vuex.Store()
  3. store.dispatch = jest.fn()
  4. const wrapper = mount(ComponentWithButtons, {
  5. store, localVue
  6. })
  7. await wrapper.find(".namespaced-dispatch").trigger("click")
  8. expect(store.dispatch).toHaveBeenCalledWith(
  9. 'namespaced/very/deeply/testAction',
  10. { msg: "Test Namespaced Dispatch" }
  11. )
  12. })

We start by creating a Vuex store, with the module(s) we are interested in. I declare the module namespacedModule inside the test, but in a real world app, you would just import the modules your component depends on. We then replace the dispatch method with a jest.fn mock, and make assertions against that.

Conclusion

In this section we covered:

  1. Using Vuex with a localVue and mocking a mutation
  2. Mocking the Vuex API (dispatch and commit)
  3. Using a real Vuex store with a mock dispatch function

The source code for the test described on this page can be found hereVuex in components - mutations and actions - 图3.