Vue3 Fundamentals · Testing Vue Components

Finding elements and text

Finding elements and text

Mounting gives you a wrapper, but the wrapper is not what you assert on — the rendered elements are. To check what a component shows, you query the wrapper for the elements you care about.

wrapper.find('selector') returns the first element matching a CSS selector, as a wrapper of its own. Call .text() on it to read the text inside, or .exists() to check whether it rendered at all.

const wrapper = mount(App);

expect(wrapper.find('h1').exists()).toBe(true);
expect(wrapper.find('h1').text()).toBe('Testing Vue');

When you expect several elements, wrapper.findAll('selector') returns them all as an array in document order. Map over that array to read each one:

const items = wrapper.findAll('li').map((item) => item.text());

find and findAll take any CSS selector, so you can reach an element by tag, class, or id. That is how a test moves from "something rendered" to "this exact element rendered with this exact text".

Your job is to render the list the Test queries for in App.vue.

Challenge

The page shows an incomplete list. Export an App component from App.vue whose template renders an unordered list (<ul>) with three list items (<li>) reading Learn, Build, and Ship, in that order.

The Test finds the list, then finds every item and reads its text. The starter renders only the first item, so the list is short and the Test fails. Add the missing items.

App.vue
<script setup>
const steps = ['Learn'];
</script>

<template>
  <ul>
    <li v-for="step in steps" :key="step">{{ step }}</li>
  </ul>
</template>

No test run yet

The Test is hidden. Run it to check the Challenge — you only see the result, never the test source.