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

Find the Vue.js 2 version here.

The Composition API

Vue 3 introduced a new API for create components - the Composition APIComposition API - 图1.

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 - 图2.

The Component

Below the “Hello, World” of the Composition API, more or less. If you don’t understand something, read the RFCComposition API - 图3 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 { reactive, computed } from 'vue'
  12. export default {
  13. name: 'CompositionApi',
  14. props: {
  15. message: {
  16. type: String
  17. }
  18. },
  19. setup(props) {
  20. const state = reactive({
  21. count: 0
  22. })
  23. const increment = () => {
  24. state.count += 1
  25. }
  26. return {
  27. state,
  28. increment,
  29. uppercasedMessage: computed(() => props.message.toUpperCase())
  30. }
  31. }
  32. }
  33. </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 props 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. props: {
  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. props: { 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.