Find the Vue.js 2 version here.

The source code for the test described on this page can be found .

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.

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. const wrapper = mount(ComponentWithVuex, {
  18. global: {
  19. plugins: [store]
  20. }
  21. })
  22. expect(wrapper.find(".username").text()).toBe("alice")
  23. })
  24. })

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 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:

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. return this.$store.getters.fullname
  11. }
  12. }
  13. }

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:

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 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 above techniques all work in conjuction with Vuex’s mapState and mapGetters helpers. We can update ComponentWithGetters to the following:

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

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