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

Find the Vue.js 2 version here.

Testing Mutations

Testing mutations in isolation is very straight forward, because mutations are just regular JavaScript functions. This page discusses testing mutations in isolation. If you want to test mutations in the context of a component committing a mutation, see hereVuex - Mutations - 图1.

The test used in the following example can be found hereVuex - Mutations - 图2.

Creating the Mutation

Mutations tend to following a set pattern. Get some data, maybe do some processing, then assign the data to the state. Here is the outline of an ADD_POST mutation. Once implemented, it will receive a post object in the payload, and add the post.id to state.postIds. It will also add the post object to the state.posts object, where the key is the post.id. This is a common pattern in apps using Vuex.

We will develop it using TDD. The start of the mutation is as follows:

  1. export default {
  2. SET_POST(state, { post }) {
  3. }
  4. }

Let’s write the test, and let the error messages guide our development:

  1. import mutations from "@/store/mutations.js"
  2. describe("SET_POST", () => {
  3. it("adds a post to the state", () => {
  4. const post = { id: 1, title: "Post" }
  5. const state = {
  6. postIds: [],
  7. posts: {}
  8. }
  9. mutations.SET_POST(state, { post })
  10. expect(state).toEqual({
  11. postIds: [1],
  12. posts: { "1": post }
  13. })
  14. })
  15. })

Running this test with yarn test:unit yields the following failure message:

  1. FAIL tests/unit/mutations.spec.js
  2. SET_POST adds a post to the state
  3. expect(received).toEqual(expected)
  4. Expected value to equal:
  5. {"postIds": [1], "posts": {"1": {"id": 1, "title": "Post"}}}
  6. Received:
  7. {"postIds": [], "posts": {}}

Let’s start by adding the post.id to state.postIds:

  1. export default {
  2. SET_POST(state, { post }) {
  3. state.postIds.push(post.id)
  4. }
  5. }

Now yarn test:unit yields:

  1. Expected value to equal:
  2. {"postIds": [1], "posts": {"1": {"id": 1, "title": "Post"}}}
  3. Received:
  4. {"postIds": [1], "posts": {}}

postIds looks good. Now we just need to add the post to state.posts. Because of how the Vue reactivity system works we cannot simply write post[post.id] = post to add the post. More details can be found hereVuex - Mutations - 图3. Basically, you need to create a new object using Object.assign or the ... operator. We will use the ... operator to assign the post to state.posts:

  1. export default {
  2. SET_POST(state, { post }) {
  3. state.postIds.push(post.id)
  4. state.posts = { ...state.posts, [post.id]: post }
  5. }
  6. }

Now the test passes!

Conclusion

Testing Vuex mutations requires nothing specific to Vue or Vuex, since they are just regular JavaScript functions. Simply import them and test as needed. The only thing to be careful of is Vue’s reactivity caveats, which apply to Vuex as well. You can read more about the reactivity system and common caveats hereVuex - Mutations - 图4.

The page discussed:

  • Vuex mutations are regular JavaScript functions
  • Mutations can, and should, be tested in isolation from the main Vue app

The test used in the above example can be found hereVuex - Mutations - 图5.