@testing-library/vue and @vue/test-utils are the two primary libraries for testing Vue.js applications, but they serve different goals. @vue/test-utils is the official low-level utility library that provides direct access to component instances, lifecycle hooks, and internal state, making it ideal for unit testing component internals. @testing-library/vue is built on top of Vue testing primitives and focuses on testing components from the user's perspective, encouraging queries based on accessibility roles and visible text rather than implementation details. While @vue/test-utils gives you full control over the mounting process, @testing-library/vue abstracts away the mounting logic to prevent tests from becoming too coupled to the component structure.
Both @testing-library/vue and @vue/test-utils help you verify that your Vue components work as expected, but they approach testing from opposite directions. One focuses on the user experience, while the other focuses on the component internals. Let's compare how they handle common testing tasks.
@testing-library/vue encourages testing behavior.
// @testing-library/vue: Query by role
import { render, screen } from '@testing-library/vue'
import Button from './Button.vue'
render(Button, { props: { label: 'Submit' } })
const button = screen.getByRole('button', { name: 'Submit' })
@vue/test-utils exposes implementation details.
// @vue/test-utils: Query by CSS selector
import { mount } from '@vue/test-utils'
import Button from './Button.vue'
const wrapper = mount(Button, { props: { label: 'Submit' } })
const button = wrapper.find('button.submit-btn')
render vs mount@testing-library/vue uses render.
// @testing-library/vue: Render function
import { render } from '@testing-library/vue'
const { container } = render(MyComponent)
// container holds the DOM node
@vue/test-utils uses mount or shallowMount.
// @vue/test-utils: Mount function
import { mount } from '@vue/test-utils'
const wrapper = mount(MyComponent)
// wrapper contains the component instance and DOM
@testing-library/vue prefers accessibility queries.
getByRole, getByText, getByLabelText.// @testing-library/vue: Accessibility query
import { screen } from '@testing-library/vue'
const input = screen.getByLabelText('Username')
const heading = screen.getByRole('heading', { level: 1 })
@vue/test-utils prefers CSS selectors.
find, get, findAll with strings or components.// @vue/test-utils: CSS selector query
import { mount } from '@vue/test-utils'
const wrapper = mount(MyComponent)
const input = wrapper.find('input[name="username"]')
const child = wrapper.findComponent(ChildComponent)
fireEvent vs trigger@testing-library/vue uses fireEvent or userEvent.
// @testing-library/vue: Fire event
import { fireEvent } from '@testing-library/vue'
await fireEvent.click(button)
await fireEvent.update(input, 'new value')
@vue/test-utils uses trigger.
// @vue/test-utils: Trigger event
import { mount } from '@vue/test-utils'
await wrapper.find('button').trigger('click')
await wrapper.find('input').setValue('new value')
vm@testing-library/vue hides the component instance.
vm is not provided by default.// @testing-library/vue: No direct VM access
// You interact only with the DOM
const { container } = render(MyComponent)
// No wrapper.vm available
@vue/test-utils exposes the component instance.
wrapper.vm to check data or call methods.// @vue/test-utils: Direct VM access
import { mount } from '@vue/test-utils'
const wrapper = mount(MyComponent)
expect(wrapper.vm.count).toBe(0)
wrapper.vm.increment()
While the differences are clear, both libraries also share many core ideas and tools. Here are key overlaps:
setup() functions and reactive refs.<script setup>.// Both: Testing Composition API
// Component uses setup()
// Tests mount/render the same way regardless of API style
// Both: Passing props
// @testing-library/vue
render(Component, { props: { id: 1 } })
// @vue/test-utils
mount(Component, { props: { id: 1 } })
await for async setup or data fetching.// Both: Waiting for updates
// @testing-library/vue
await screen.findByText('Loaded')
// @vue/test-utils
await wrapper.vm.$nextTick()
// Both: Global plugins
// @testing-library/vue
render(Component, { global: { plugins: [router] } })
// @vue/test-utils
mount(Component, { global: { plugins: [router] } })
| Feature | @testing-library/vue | @vue/test-utils |
|---|---|---|
| Philosophy | 🧠 User behavior | 🛠️ Implementation details |
| Rendering | 🏗️ render | 🏗️ mount |
| Querying | 🔍 Accessibility roles | 🔍 CSS selectors |
| Interactions | 🖱️ fireEvent | 🖱️ trigger |
| Internals | 🔒 Hidden (vm not exposed) | 🔓 Exposed (wrapper.vm) |
| Best For | ✅ Integration & E2E-like tests | ✅ Unit tests & libraries |
@testing-library/vue is like a black-box tester 📦 — it cares only about inputs and outputs. Ideal for feature tests, accessibility compliance, and ensuring stability during refactors.
@vue/test-utils is like a white-box tester 🔍 — it lets you inspect the inner workings. Perfect for unit testing complex logic, reusable libraries, or when you need to mock deep child components.
Final Thought: Many teams use both. Use @testing-library/vue for high-level feature tests and @vue/test-utils for low-level unit tests where internal state matters. Choose based on what you need to prove.
Choose @testing-library/vue if your priority is testing what the user sees and interacts with, rather than how the component works internally. It is the best fit for integration tests, accessibility testing, and ensuring your UI behaves correctly across changes. This library forces you to write resilient tests that do not break when you refactor internal logic, as long as the user experience remains the same.
Choose @vue/test-utils if you need to test private methods, internal state, or lifecycle hooks directly. It is suitable for unit testing reusable component libraries where you need to mock children deeply or assert on emitted events with precise control. This tool is also necessary if you are testing complex logic that does not render visible UI elements or requires direct access to the component instance.
Simple and complete Vue.js testing utilities that encourage good testing practices.
Vue Testing Library is a lightweight adapter built on top of DOM Testing Library and @vue/test-utils.
This module is distributed via npm and should be installed as one of your
project's devDependencies:
npm install --save-dev @testing-library/vue
This library has peerDependencies listings for Vue 3 and
@vue/compiler-sfc.
You may also be interested in installing jest-dom so you can use the custom
Jest matchers.
If you're using Vue 2, please install version 5 of the library:
npm install --save-dev @testing-library/vue@^5
<template>
<p>Times clicked: {{ count }}</p>
<button @click="increment">increment</button>
</template>
<script>
export default {
name: 'Button',
data: () => ({
count: 0,
}),
methods: {
increment() {
this.count++
},
},
}
</script>
import {render, screen, fireEvent} from '@testing-library/vue'
import Button from './Button'
test('increments value on click', async () => {
// The `render` method renders the component into the document.
// It also binds to `screen` all the available queries to interact with
// the component.
render(Button)
// queryByText returns the first matching node for the provided text
// or returns null.
expect(screen.queryByText('Times clicked: 0')).toBeTruthy()
// getByText returns the first matching node for the provided text
// or throws an error.
const button = screen.getByText('increment')
// Click a couple of times.
await fireEvent.click(button)
await fireEvent.click(button)
expect(screen.queryByText('Times clicked: 2')).toBeTruthy()
})
You might want to install
jest-domto add handy assertions such as.toBeInTheDocument(). In the example above, you could writeexpect(screen.getByText('Times clicked: 0')).toBeInTheDocument().
Using
byTextqueries it's not the only nor the best way to query for elements. Read Which query should I use? to discover alternatives. In the example above,getByRole('button', {name: 'increment'})is possibly the best option to get the button element.
You'll find examples of testing with different situations and popular libraries in the test directory.
Some included are:
Feel free to contribute with more examples!
The more your tests resemble the way your software is used, the more confidence they can give you.
We try to only expose methods and utilities that encourage you to write tests that closely resemble how your Vue components are used.
Utilities are included in this project based on the following guiding principles:
At the end of the day, what we want is for this library to be pretty light-weight, simple, and understandable.
Please note that TypeScript 4.X is required.
The TypeScript type definitions are in the types directory.
If you want to lint test files that use Vue Testing Library, you can use the official plugin: eslint-plugin-testing-library.
Looking to contribute? Look for the Good First Issue label.
Please file an issue for bugs, missing documentation, or unexpected behavior.
Please file an issue to suggest new features. Vote on feature requests by adding a 👍. This helps maintainers prioritize what to work on.
For questions related to using the library, please visit a support community instead of filing an issue on GitHub.