This book is written for Vue.js 3 and Vue Test Utils v2.

Find the Vue.js 2 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 global.plugins to test $store.state

In a regular Vue app, we install Vuex using app.use(store), which installs a globally available Vuex store in the app. In a unit test, we can do exactly the same thing. Unlike a regular Vue app, we don’t want to share the same Vuex store across every test - we want a fresh one for each test. Let’s see how we can do that. 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 createStore to create a new Vuex store. Then we pass the new store in the component’s global.plugins mounting options. A full test looks like this:

  1. import { createStore } from "vuex"
  2. import { mount } from "@vue/test-utils"
  3. import ComponentWithVuex from "../../src/components/ComponentWithVuex.vue"
  4. const store = createStore({
  5. state() {
  6. return {
  7. username: "alice",
  8. firstName: "Alice",
  9. lastName: "Doe"
  10. }
  11. },
  12. getters: {
  13. fullname: (state) => state.firstName + " " + state.lastName
  14. }
  15. })
  16. describe("ComponentWithVuex", () => {
  17. it("renders a username using a real Vuex store", () => {
  18. const wrapper = mount(ComponentWithVuex, {
  19. global: {
  20. plugins: [store]
  21. }
  22. })
  23. expect(wrapper.find(".username").text()).toBe("alice")
  24. })
  25. })

The tests passes. Creating a new Vuex store every test introduces some boilerplate. The total code required is quite long. If you have a lot of components that use a Vuex store, an alternative is to use the global.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 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 do not recommend one or the other. The first test uses a real Vuex store, so it’s closer to how you app will work in production. That said, it an introduce a lot of boilerplate and if you have a very complex Vuex store you may end up with very large helper methods to create the store that make your tests difficult to understand.

The second approach uses a mock store. On of the good things about this is all the necessary data is declared inside the test, making it easier to understand, and it is a bit more compact. It is less likely to catch regressions in your Vuex store, though. You could delete your entire Vuex store and this test would still pass - not ideal.

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, the test looks like this:

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

The test is very compact - just two lines of code. There is a lot of setup involved, however - we are had to use a Vuex store. Note we are not using the Vuex store our app would be using, we created a minimal one with the basic data needed to supply the fullname getter the component was expecting.

An alternative is to import the real Vuex store you are using in your app, which includes the actual getters. 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, but there is no reason this won’t work.

An alternative would be to write the test using the global.mocks mounting option:

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

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

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 createStore to create a real Vuex store and install it with global.plugins
  • how to test $store.state and getters
  • using the global.mocks mounting option to mock $store.state and getters

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.