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

Find the Vue.js 2 version here.

Testing getters

Testing getters in isolation is straight forward, since they are basically just JavaScript functions. The techniques are similar to testing mutations, more info hereVuex - Getters - 图1, and actions.

The source code for the test described on this page can be found hereVuex - Getters - 图2.

We will look at two getters, which operate on a store that looks like this:

  1. const state = {
  2. dogs: [
  3. { name: "lucky", breed: "poodle", age: 1 },
  4. { name: "pochy", breed: "dalmatian", age: 2 },
  5. { name: "blackie", breed: "poodle", age: 4 }
  6. ]
  7. }

The getters we will test are:

  1. poodles: gets all poodles
  2. poodlesByAge: gets all poodles, and accepts an age argument

Creating the Getters

First, let’s create the getters.

  1. export default {
  2. poodles: (state) => {
  3. return state.dogs.filter(dog => dog.breed === "poodle")
  4. },
  5. poodlesByAge: (state, getters) => (age) => {
  6. return getters.poodles.filter(dog => dog.age === age)
  7. }
  8. }

Nothing too exciting - remember that getters receive other getters as the second argument. Since we already have a poodles getter, we can use that in poodlesByAge. By returning a function in poodlesByAge that takes an argument, we can pass arguments to getters. The poodlesByAge getter can be used like this:

  1. computed: {
  2. puppies() {
  3. return this.$store.getters.poodlesByAge(1)
  4. }
  5. }

Let’s start with a test for poodles.

Writing the Tests

Since a getter is just a JavaScript function that takes a state object as the first argument, the test is very simple. I’ll write my test in a getters.spec.js file, with the following code:

  1. import getters from "../../src/store/getters.js"
  2. const dogs = [
  3. { name: "lucky", breed: "poodle", age: 1 },
  4. { name: "pochy", breed: "dalmatian", age: 2 },
  5. { name: "blackie", breed: "poodle", age: 4 }
  6. ]
  7. const state = { dogs }
  8. describe("poodles", () => {
  9. it("returns poodles", () => {
  10. const actual = getters.poodles(state)
  11. expect(actual).toEqual([ dogs[0], dogs[2] ])
  12. })
  13. })

Vuex automatically passes the state to the getter. Since we are testing the getters in isolation, we have to manually pass the state. Other than that, we are just testing a regular JavaScript function.

poodlesByAge is a bit more interesting. The second argument to a getter is other getters. We are testing poodlesByAge, so we don’t want to involve the implementation of poodles. Instead, we can stub getters.poodles. This will give us more fine grained control over the test.

  1. describe("poodlesByAge", () => {
  2. it("returns poodles by age", () => {
  3. const poodles = [ dogs[0], dogs[2] ]
  4. const actual = getters.poodlesByAge(state, { poodles })(1)
  5. expect(actual).toEqual([ dogs[0] ])
  6. })
  7. })

Instead of actually passing the real poodles getter, we pass in the result it would return. We already know it is working, since we wrote a test for it. This allows us to focus on testing the logic unique to poodlesByAge.

It is possible to have async getters. They can be tested using the same technique as async actions, which you can read about hereVuex - Getters - 图3.

Conclusion

  • getters are just plain JavaScript functions.
  • When testing getters in isolation, you need to pass the state manually.
  • If a getter uses another getter, you should stub the expected return result of the first getter. This will give you more fine grained control over the test, and let you focus on testing the getter in question

The source code for the test described on this page can be found hereVuex - Getters - 图4.