Skip to content
tezvyn:
Menu

Unit Testing Vue Components with Vitest

Source: test-utils.vuejs.orgEasyHow cards are made

Think of it as a two-part system: Vue Test Utils mounts your component in a virtual environment, and a runner like Vitest executes the test and reports pass/fail. Use this to verify a component's logic in isolation.

Why it exists

To verify the behavior of individual Vue components without needing to run the entire application in a browser. This allows for fast, isolated feedback on component logic, ensuring that changes don't break its core functionality and contract.

The mental model

Think of it as a lab environment for your component. Vue Test Utils (@vue/test-utils) provides the workbench and tools (mount, find, trigger) to place your component under a microscope. Vitest is the lab technician who runs the predefined experiment (your test file) and records the results (pass or fail). Neither can do the job alone.

How it works

A typical test involves three steps. First, you import mount from @vue/test-utils and the component you want to test. Second, inside a test block provided by Vitest, you call mount(MyComponent, { props: {...} }) to create an in-memory instance, which returns a wrapper object. Third, you use the wrapper API to interact with or inspect the component (e.g., wrapper.text(), wrapper.find('button')) and use Vitest's expect function to assert that the outcome is correct.

When to use it

Use this combination for classic unit testing. For example: verifying that a component renders the correct text based on its props; ensuring a v-if directive correctly shows or hides an element; or confirming that a user click emits the expected event with the correct payload. It's for testing the component's contract in isolation.

When not to use it

Avoid this for testing interactions between multiple complex components or for full end-to-end user flows. Tools like Cypress or Playwright (in their E2E mode) are better suited for testing how the entire application behaves in a real browser, including navigation and network interactions.

One canonical example

A canonical test checks if a component displays a message passed via props. You import mount from Vue Test Utils, expect and test from Vitest, and your component. Inside the test, you define a message string and create a wrapper by calling mount(MessageComponent, { props: { msg: your_message } }). The final step is the assertion: expect(wrapper.text()).toContain(your_message). This confirms the component correctly rendered its prop.

Interview question

Which scenario is best suited for unit testing Vue components using Vue Test Utils and Vitest?

  • a.Ensuring an entire application's navigation and network requests function correctly.
  • b.Profiling the rendering performance of a component to identify bottlenecks.
  • c.Evaluating the complex interactions between several different components within a larger view.
  • d.Verifying that a component renders specific content based on its input properties.Correct
Why?

The card explicitly states that this combination is for "classic unit testing" to "verify the behavior of individual Vue components" such as "verifying that a component renders the correct text based on its props." Options B and C describe scenarios for end-to-end or integration testing, not isolated unit testing.

Just read this? Test yourself on what you have been reading.

Read the original → test-utils.vuejs.org

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on vue — each one lists the topics its interview covers.

See open roles