Vue3 Fundamentals · Testing Vue Components

Mounting a component

Mounting a component

A component is only a definition until something renders it. Before you can test what a component shows, you have to mount it — put it into a DOM and let Vue run its setup and its template.

@vue/test-utils gives you mount for that. Import it, import your component from App.vue as App, and call mount(App). It returns a wrapper: an object that holds the rendered output and lets you inspect it.

import { mount } from '@vue/test-utils';

const wrapper = mount(App);

mount renders the component into a detached element, away from the page, so a test never disturbs the real document. Everything the component rendered lives inside the returned wrapper.

Because the component is mounted, its <script setup> has run and its template has produced real elements. If the template renders a heading, the wrapper contains that heading. If it renders nothing, the wrapper is empty.

Your job is to give the Test something to mount: export an App component from App.vue whose template renders the heading the Test looks for.

Challenge

A component that renders nothing cannot be tested. Export an App component from App.vue whose template renders a level-one heading (<h1>) with the text Testing Vue.

The Test mounts your component and checks that the wrapper contains an <h1>. The starter renders a paragraph instead, so the heading is missing and the Test fails. Change the element to a heading and keep the text.

App.vue
<script setup>
const title = 'Testing Vue';
</script>

<template>
  <p>{{ title }}</p>
</template>

No test run yet

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