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

Find the Vue.js 3 version here.

Testing Vuex in components

The source code for the test described on this page can be found hereVuex in components - $state and getters - 图1.

Using createLocalVue to test $store.state

In a regular Vue app, we install Vuex using Vue.use(Vuex), and then pass a new Vuex store to the app. If we do the same in a unit test, though, all unit tests will receive the Vuex store - even tests that are not using the store. vue-test-utils provides a createLocalVue method, which provides a temporary Vue instance to be used on a test by test basis. Let’s see how to use it. First, a simple <ComponentWithGetters> component that renders a username in the store’s base state.

  1. <template>
  2. <div>
  3. <div class="username">
  4. {{ username }}
  5. </div>
  6. </div>
  7. </template>
  8. <script>
  9. export default {
  10. name: "ComponentWithVuex",
  11. data() {
  12. return {
  13. username: this.$store.state.username
  14. }
  15. }
  16. }
  17. </script>

We can use createLocalVue to create a temporary Vue instance, and install Vuex. Then we simply pass the new store in the component’s mounting options. A full test looks like this:

  1. import Vuex from "vuex"
  2. import { mount, createLocalVue } from "@vue/test-utils"
  3. import ComponentWithVuex from "@/components/ComponentWithVuex.vue"
  4. const localVue = createLocalVue()
  5. localVue.use(Vuex)
  6. const store = new Vuex.Store({
  7. state: {
  8. username: "alice"
  9. }
  10. })
  11. describe("ComponentWithVuex", () => {
  12. it("renders a username using a real Vuex store", () => {
  13. const wrapper = mount(ComponentWithVuex, {
  14. store,
  15. localVue
  16. })
  17. expect(wrapper.find(".username").text()).toBe("alice")
  18. })
  19. })

The tests passes. Creating a new localVue introduces some boilerplate, and the test is quite long. If you have a lot of components that use a Vuex store, an alternative is to use the mocks mounting option, and simply mock the store.

Using a mock store

Using the mocks mounting options, you can mock the global $store object. This means you do not need to use createLocalVue, or create a new Vuex store. Using this technique, the above test can be rewritten like this:

  1. it("renders a username using a mock store", () => {
  2. const wrapper = mount(ComponentWithVuex, {
  3. mocks: {
  4. $store: {
  5. state: { username: "alice" }
  6. }
  7. }
  8. })
  9. expect(wrapper.find(".username").text()).toBe("alice")
  10. })

I personally prefer this approach. All the necessary data is declared inside the test, and it is a bit more compact. Both techniques are useful, and neither is better or worse than the other.

Testing getters

Using the above techniques, getters are easily tested. First, a component to test:

  1. <template>
  2. <div class="fullname">
  3. {{ fullname }}
  4. </div>
  5. </template>
  6. <script>
  7. export default {
  8. name: "ComponentWithGetters",
  9. computed: {
  10. fullname() {
  11. return this.$store.getters.fullname
  12. }
  13. }
  14. }
  15. </script>

We want to assert that the component correctly renders the user’s fullname. For this test, we don’t care where the fullname comes from, just that the component renders is correctly.

First, using a real Vuex store and createLocalVue, the test looks like this:

  1. const localVue = createLocalVue()
  2. localVue.use(Vuex)
  3. const store = new Vuex.Store({
  4. state: {
  5. firstName: "Alice",
  6. lastName: "Doe"
  7. },
  8. getters: {
  9. fullname: (state) => state.firstName + " " + state.lastName
  10. }
  11. })
  12. it("renders a username using a real Vuex getter", () => {
  13. const wrapper = mount(ComponentWithGetters, { store, localVue })
  14. expect(wrapper.find(".fullname").text()).toBe("Alice Doe")
  15. })

The test is very compact - just two lines of code. There is a lot of setup involved, however - we are bascially rebuilding the Vuex store. An alternative is to import the real Vuex store, with the actual getter. This introduces another dependency to the test though, and when developing a large system, it’s possible the Vuex store might be being developed by another programmer, and has not been implemented yet.

Let’s see how we can write the test using the mocks mounting option:

  1. it("renders a username using computed mounting options", () => {
  2. const wrapper = mount(ComponentWithGetters, {
  3. mocks: {
  4. $store: {
  5. getters: {
  6. fullname: "Alice Doe"
  7. }
  8. }
  9. }
  10. })
  11. expect(wrapper.find(".fullname").text()).toBe("Alice Doe")
  12. })

Now all the required data is contained in the test. Great! I strongly prefer this, since the test is fully contained, and all the knowledge required to understand what the component should do is contained in the test.

We can make the test even more concise though, using the computed mounting option.

Mocking getters using computed

Getters are generally wrapped in computed properties. Remember, this test is all about making sure the component behaves correctly given the current state of the store. We are not testing the implementation of fullname, or to see if getters work. This means we can simply replace real store, or the mock store, using the computed mounting option. The test can be rewritten like this:

  1. it("renders a username using computed mounting options", () => {
  2. const wrapper = mount(ComponentWithGetters, {
  3. computed: {
  4. fullname: () => "Alice Doe"
  5. }
  6. })
  7. expect(wrapper.find(".fullname").text()).toBe("Alice Doe")
  8. })

This is more concise than the two previous tests, and still expresses the component’s intention.

The mapState and mapGetters helper

The above techniques all work in conjuction with Vuex’s mapState and mapGetters helpers. We can update ComponentWithGetters to the following:

  1. import { mapGetters } from "vuex"
  2. export default {
  3. name: "ComponentWithGetters",
  4. computed: {
  5. ...mapGetters([
  6. 'fullname'
  7. ])
  8. }
  9. }

The tests still pass.

Conclusion

This guide discussed:

  • using createLocalVue and a real Vuex store to test $store.state and getters
  • using the mocks mounting option to mock $store.state and getters
  • using the computed mounting option to set the desired value of a Vuex getter

Techniques to test the implentation of Vuex getters in isolation can be found in this guideVuex in components - $state and getters - 图2.

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