This book is written for Vue.js 2 and Vue Test Utils v1.

Find the Vue.js 3 version here.

The Composition API

Vue 3 will introduce a new API for create components - the Composition APIComposition API - 图1. To allow users to try it out and get feedback, the Vue team released a plugin that lets us try it out in Vue 2. You can find it hereComposition API - 图2.

Testing a component build with the Composition API should be no different to testing a standard component, since we are not testing the implementation, but the output (what the component does, not how it does it). This article will show a simple example of a component using the Composition API in Vue 2, and how testing strategies are the same as any other component.

The source code for the test described on this page can be found hereComposition API - 图3.

The Component

Below the “Hello, World” of the Composition API, more or less. If you don’t understand something, read the RFCComposition API - 图4 or have a Google; there are lots of resources about the Composition API.

  1. <template>
  2. <div>
  3. <div class="message">{{ uppercasedMessage }}</div>
  4. <div class="count">
  5. Count: {{ state.count }}
  6. </div>
  7. <button @click="increment">Increment</button>
  8. </div>
  9. </template>
  10. <script>
  11. import Vue from 'vue'
  12. import VueCompositionApi from '@vue/composition-api'
  13. Vue.use(VueCompositionApi)
  14. import {
  15. reactive,
  16. computed
  17. } from '@vue/composition-api'
  18. export default {
  19. name: 'CompositionApi',
  20. props: {
  21. message: {
  22. type: String
  23. }
  24. },
  25. setup(props) {
  26. const state = reactive({
  27. count: 0
  28. })
  29. const increment = () => {
  30. state.count += 1
  31. }
  32. return {
  33. state,
  34. increment,
  35. uppercasedMessage: computed(() => props.message.toUpperCase())
  36. }
  37. }
  38. }
  39. </script>

The two things we will need to test here are:

  1. Does clicking the increment button increase state.count by 1?

  2. Does the message received in the props render correctly (transformed to upper case)?

Testing the Props Message

Testing the message is correctly rendered is trivial. We just use propsData to set the value of the prop, as described here.

  1. import { mount } from "@vue/test-utils"
  2. import CompositionApi from "@/components/CompositionApi.vue"
  3. describe("CompositionApi", () => {
  4. it("renders a message", () => {
  5. const wrapper = mount(CompositionApi, {
  6. propsData: {
  7. message: "Testing the composition API"
  8. }
  9. })
  10. expect(wrapper.find(".message").text()).toBe("TESTING THE COMPOSITION API")
  11. })
  12. })

As expected, this is very simple - regardless of the way we are composing out components, we use the same API and same strategies to test. You should be able to change the implementation entirely, and not need to touch the tests. Remember to test outputs (the rendered HTML, usually) based on given inputs (props, triggered events), not the implementation.

Testing the Button Click

Writing a test to ensure clicking the button increments the state.count is equally simple. Notice the test is marked async; read more about why this is required in Simulating User Input.

  1. import { mount } from "@vue/test-utils"
  2. import CompositionApi from "@/components/CompositionApi.vue"
  3. describe("CompositionApi", () => {
  4. it("increments a count when button is clicked", async () => {
  5. const wrapper = mount(CompositionApi, {
  6. propsData: { message: '' }
  7. })
  8. await wrapper.find('button').trigger('click')
  9. expect(wrapper.find(".count").text()).toBe("Count: 1")
  10. })
  11. })

Again, entirely uninteresting - we trigger the click event, and assert that the rendered count increased.

Conclusion

The article demonstrates how testing a component using the Composition API is identical to testing one using the traditional options API. The ideas and concepts are the same. The main point to be learned is when writing tests, make asserions based on inputs and outputs.

It should be possible to refactor any traditional Vue component to use the Composition API without the need to change the unit tests. If you find yourself needing to change your tests when refactoring, you are likely testing the implmentation, not the output.

While an exciting new feature, the Composition API is entirely additive, so there is no immediate need to use it, however regardless of your choice, remember a good unit tests asserts the final state of the component, without considering the implementation details.