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

Find the Vue.js 2 version here.

Finding Elements

vue-test-utils provides a number of ways to find and assert the presence of html elements or other Vue components using the find and findComponent methods. The main use of find is asserting a component correctly renders an element or child component.

Note: If you used Vue Test Utils prior to v1, you may remember find working with components as well as DOM elements. Now you use find and findAll for DOM elements, and findComponent and findAllComponents for Vue components. Ther is also a get and getComponent pair, which are exactly the same as find and findComponent, but they will raise an error if they do not find anything. This guide chooses to use find and findComponent.

The source code for the test described on this page can be found hereFinding elements and components - 图1.

Creating the Components

For this example, we will create a <Child> and <Parent> component.

Child:

  1. <template>
  2. <div>Child</div>
  3. </template>
  4. <script>
  5. export default {
  6. name: "Child"
  7. }
  8. </script>

Parent:

  1. <template>
  2. <div>
  3. <span v-show="showSpan">
  4. Parent Component
  5. </span>
  6. <Child v-if="showChild" />
  7. </div>
  8. </template>
  9. <script>
  10. import Child from "./Child.vue"
  11. export default {
  12. name: "Parent",
  13. components: { Child },
  14. data() {
  15. return {
  16. showSpan: false,
  17. showChild: false
  18. }
  19. }
  20. }
  21. </script>

find with querySelector syntax

Regular elements can easily be selected using the syntax used with document.querySelector. vue-test-utils also provides a isVisible method to check if elements conditionally rendered with v-show are visible. Create a Parent.spec.js, and inside add the following test:

  1. import { mount } from "@vue/test-utils"
  2. import '@testing-library/jest-dom'
  3. import Parent from "@/components/Parent.vue"
  4. describe("Parent", () => {
  5. it("does not render a span", () => {
  6. const wrapper = mount(Parent)
  7. expect(wrapper.find("span").element).not.toBeVisible()
  8. })
  9. })

Since v-show="showSpan" defaults to false, we expect the found <span> element’s not to be visible. We are using the awesome @testing-library/jest-dom matchers to validate this - determining visibility is tricky business, so Vue Test Utils leaves it up to another battle tested library. The tests passes when run with yarn test:unit. Next, a test around the case when showSpan is true.

  1. it("does render a span", () => {
  2. const wrapper = mount(Parent, {
  3. data() {
  4. return { showSpan: true }
  5. }
  6. })
  7. expect(wrapper.find("span").element).toBeVisible()
  8. })

It passes!

Finding Components with name and Component

Finding child components is a little different to finding regular HTML elements. There two main ways to assert the presence of child Vue components:

  1. findComponent(Component)
  2. findComponent({ name: "ComponentName" })

These are a bit easier to understand in the context of an example test. Let’s start with the findComponent(Component) syntax. This requires us to import the component, and pass it to the findComponent function.

  1. import Child from "@/components/Child.vue"
  2. it("does not render a Child component", () => {
  3. const wrapper = mount(Parent)
  4. expect(wrapper.findComponent(Child).exists()).toBe(false)
  5. })

The implementation for find and findComponent is quite complex, since it works with the querySelector for DOM elements, as well as several other syntaxes for Vue components. You can see the part of the source that finds children Vue components hereFinding elements and components - 图2. It basically checks the component’s name against each child rendered, and then checks the constructor, and some other properties.

As mentioned in the previous paragraph, the name property is one of the checks done by find when you pass a component. Instead of passing the component, you can simply pass an object with the correct name property. This means you do not need to import the component. Let’s test the case when <Child> should be rendered:

  1. it("renders a Child component", () => {
  2. const wrapper = mount(Parent, {
  3. data() {
  4. return { showChild: true }
  5. }
  6. })
  7. expect(wrapper.findComponent({ name: "Child" }).exists()).toBe(true)
  8. })

It passes! Using the name property can be a little unintuitive, so importing the actual component is an alternative. Another option is to simply add a class or id and query using the querySelector style syntax presented in the first two examples.

findAll and findAllComponents

There are often cases when you want to assert that a number of elements are rendered. A common case is a list of items rendered with v-for. Here is a <ParentWithManyChildren> that renders several <Child> components.

  1. <template>
  2. <div>
  3. <Child v-for="id in [1, 2 ,3]" :key="id" />
  4. </div>
  5. </template>
  6. <script>
  7. import Child from "./Child.vue"
  8. export default {
  9. name: "ParentWithManyChildren",
  10. components: { Child }
  11. }
  12. </script>

We can write a test using findAllComponents to assert three <Child> components are rendered like this:

  1. it("renders many children", () => {
  2. const wrapper = mount(ParentWithManyChildren)
  3. expect(wrapper.findAllComponents(Child).length).toBe(3)
  4. })

Running yarn test:unit shows the test passes. You can use the querySelector syntax with findAll as well.

Conclusion

This page covers:

  • using find and findAll with the querySelector syntax for DOM elements
  • use findComponent and findAllComponents for Vue components
  • use exists to check if something is present, toBeVisible from @testing-library/jest-dom to see if something is present but not visible

The source code for the test described on this page can be found hereFinding elements and components - 图3.