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

Find the Vue.js 2 version here.

Setting Props

props can be used with both mount and shallowMount. It is often used to test components that receive props from their parent component.

props is passed into the second argument of either shallowMount or mount, in the following form:

  1. const wrapper = mount(Foo, {
  2. props: {
  3. foo: 'bar'
  4. }
  5. })

Creating the component

Create a simple <SubmitButton> component that has two props: msg and isAdmin. Depending on the value of the isAdmin prop this component will contain a <span> in one of two states:

  • Not Authorized if isAdmin is false (or not passed as a prop)
  • Admin Privileges if isAdmin is true
  1. <template>
  2. <div>
  3. <span v-if="isAdmin">Admin Privileges</span>
  4. <span v-else>Not Authorized</span>
  5. <button>
  6. {{ msg }}
  7. </button>
  8. </div>
  9. </template>
  10. <script>
  11. export default {
  12. name: "SubmitButton",
  13. props: {
  14. msg: {
  15. type: String,
  16. required: true
  17. },
  18. isAdmin: {
  19. type: Boolean,
  20. default: false
  21. }
  22. }
  23. }
  24. </script>

The First Test

We will make an assertion on the message in the case the user does not have admin privileges.

  1. import { mount } from '@vue/test-utils'
  2. import SubmitButton from '@/components/SubmitButton.vue'
  3. describe('SubmitButton.vue', () => {
  4. it("displays a non authorized message", () => {
  5. const msg = "submit"
  6. const wrapper = mount(SubmitButton,{
  7. props: {
  8. msg: msg
  9. }
  10. })
  11. console.log(wrapper.html())
  12. expect(wrapper.find("span").text()).toBe("Not Authorized")
  13. expect(wrapper.find("button").text()).toBe("submit")
  14. })
  15. })

Run the tests with yarn test:unit. The result is:

  1. PASS tests/unit/SubmitButton.spec.js
  2. SubmitButton.vue
  3. displays a non authorized message (15ms)

The result of console.log(wrapper.html()) is also printed:

  1. <div>
  2. <span>Not Authorized</span>
  3. <button>
  4. submit
  5. </button>
  6. </div>

We can see the msg prop is processed and the resulting markup is correctly rendered.

A Second Test

Let’s make an assertion on the other possible state, when isAdmin is true:

  1. import { mount } from '@vue/test-utils'
  2. import SubmitButton from '@/components/SubmitButton.vue'
  3. describe('SubmitButton.vue', () => {
  4. it('displays a admin privileges message', () => {
  5. const msg = "submit"
  6. const isAdmin = true
  7. const wrapper = mount(SubmitButton,{
  8. props: {
  9. msg,
  10. isAdmin
  11. }
  12. })
  13. console.log(wrapper.html())
  14. expect(wrapper.find("span").text()).toBe("Admin Privileges")
  15. expect(wrapper.find("button").text()).toBe("submit")
  16. })
  17. })

Run the test with yarn test:unit and check the results:

  1. PASS tests/unit/SubmitButton.spec.js
  2. SubmitButton.vue
  3. displays a admin privileges message (4ms)

We also outputted the markup with console.log(wrapper.html()):

  1. <div>
  2. <span>Admin Privileges</span>
  3. <button>
  4. submit
  5. </button>
  6. </div>

We can see the isAdmin prop was used to render the correct <span> element.

Refactoring the tests

Let’s refactor the tests adhering to the principle “Don’t Repeat Yourself” (DRY). Since all the tests are passing, we can confidently refactor. As long as the tests all still pass after the refactor, we can be sure we did not break anything.

Refactor with a Factory Function

In both tests we call mount then pass a similar props object. We can refactor this using a factory function. A factory function is simply a function that returns an object - it makes objects, thus the name “factory” function.

  1. const msg = "submit"
  2. const factory = (props) => {
  3. return mount(SubmitButton, {
  4. props: {
  5. msg,
  6. ...props
  7. }
  8. })
  9. }

The above is a function that will mount a SubmitButton component. We can pass any props to change as the first argument to factory. Let’s DRY up the test with the factory function.

  1. describe("SubmitButton", () => {
  2. describe("does not have admin privileges", ()=> {
  3. it("renders a message", () => {
  4. const wrapper = factory()
  5. expect(wrapper.find("span").text()).toBe("Not Authorized")
  6. expect(wrapper.find("button").text()).toBe("submit")
  7. })
  8. })
  9. describe("has admin privileges", ()=> {
  10. it("renders a message", () => {
  11. const wrapper = factory({ isAdmin: true })
  12. expect(wrapper.find("span").text()).toBe("Admin Privileges")
  13. expect(wrapper.find("button").text()).toBe("submit")
  14. })
  15. })
  16. })

Let’s run the tests again. Everything is still passing.

  1. PASS tests/unit/SubmitButton.spec.js
  2. SubmitButton
  3. has admin privileges
  4. renders a message (26ms)
  5. does not have admin privileges
  6. renders a message (3ms)

Since we have a good test suite, we can now easily and confidently refactor.

Conclusion

  • By passing props when mounting a component, you can set the props to be used in the test
  • Factory functions can be used to DRY your tests
  • Instead of props, you can also use setPropsTesting Props - 图1 to set prop values during tests