Simple and complete Vue DOM testing utilities that encourage good testing practices.
npm install @testing-library/vueSimple 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.
[Read the docs][docs] | [Edit the docs][docs-edit]
[![Coverage Status][coverage-badge]][coverage]
[![GitHub version][github-badge]][github]
[![npm version][npm-badge]][npm]
[![Discord][discord-badge]][discord]
[![MIT License][license-badge]][license]
- Installation
- A basic example
- More examples
- Guiding Principles
- Docs
- Typings
- ESLint support
- Issues
- 🐛 Bugs
- 💡 Feature Requests
- ❓ Questions
- License
- Contributors
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][jest-dom].
If you're using Vue 2, please install version 5 of the library:
``
npm install --save-dev @testing-library/vue@^5
` Times clicked: {{ count }}html
`
`js
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.screen
// It also binds to 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-dom][jest-dom] to add handy assertions such.toBeInTheDocument()
> as . In the example above, you could writeexpect(screen.getByText('Times clicked: 0')).toBeInTheDocument()
> .
> Using byText queries it's not the only nor the best way to query forgetByRole('button', {name: 'increment'})
> elements. Read [Which query should I use?][which-query] to discover
> alternatives. In the example above,
> 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][test-directory].
Some included are:
- [vuex][vuex-example]vue-router
- [][vue-router-example]vee-validate
- [][vee-validate-example]vue-i18n
- [][vue-i18n-example]vuetify`][vuetify-example]
- [
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.][guiding-principle]
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:
1. If it relates to rendering components, it deals with DOM nodes rather than
component instances, nor should it encourage dealing with component
instances.
2. It should be generally useful for testing individual Vue components or full
Vue applications.
3. Utility implementations and APIs should be simple and flexible.
At the end of the day, what we want is for this library to be pretty
light-weight, simple, and understandable.
[Read the docs][docs] | [Edit the docs][docs-edit]
_Please note that TypeScript 4.X is required._
The TypeScript type definitions are in the [types][types-directory] directory.
If you want to lint test files that use Vue Testing Library, you can use the
official plugin: [eslint-plugin-testing-library][eslint-plugin-testing-library].
_Looking to contribute? Look for the [Good First Issue][good-first-issue]
label._
Please [file an issue][add-issue-bug] for bugs, missing documentation, or
unexpected behavior.
[See Bugs][bugs]
Please [file an issue][add-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.
- [Discord][discord]
[MIT][license]
























[coverage-badge]: https://img.shields.io/codecov/c/github/testing-library/vue-testing-library.svg
[coverage]: https://codecov.io/github/testing-library/vue-testing-library
[github-badge]: https://badge.fury.io/gh/testing-library%2Fvue-testing-library.svg
[github]: https://badge.fury.io/gh/testing-library%2Fvue-testing-library
[npm-badge]: https://badge.fury.io/js/%40testing-library%2Fvue.svg
[npm]: https://badge.fury.io/js/%40testing-library%2Fvue
[license-badge]: https://img.shields.io/github/license/testing-library/vue-testing-library.svg
[license]: https://github.com/testing-library/vue-testing-library/blob/master/LICENSE
[discord]: https://testing-library.com/discord
[discord-badge]: https://img.shields.io/discord/723559267868737556.svg?label=&logo=discord&logoColor=ffffff&color=7389D8&labelColor=6A7EC2&style=flat-square
[jest-dom]: https://github.com/testing-library/jest-dom
[which-query]: https://testing-library.com/docs/guide-which-query
[guiding-principle]: https://twitter.com/kentcdodds/status/977018512689455106
[good-first-issue]: https://github.com/testing-library/vue-testing-library/issues?q=is%3Aissue+is%3Aopen+label%3A%22good+first+issue%22
[docs]: https://testing-library.com/vue
[docs-edit]: https://github.com/testing-library/testing-library-docs
[eslint-plugin-testing-library]: https://github.com/testing-library/eslint-plugin-testing-library
[bugs]: https://github.com/testing-library/vue-testing-library/issues?q=is%3Aissue+is%3Aopen+label%3Abug+sort%3Acreated-desc
[add-issue-bug]: https://github.com/testing-library/vue-testing-library/issues/new?assignees=&labels=bug&template=bug_report.md&title=
[add-issue]: (https://github.com/testing-library/vue-testing-library/issues/new)
[types-directory]: https://github.com/testing-library/vue-testing-library/blob/main/types
[test-directory]: https://github.com/testing-library/vue-testing-library/blob/main/src/__tests__
[vuex-example]: https://github.com/testing-library/vue-testing-library/blob/main/src/__tests__/vuex.js
[vue-router-example]: https://github.com/testing-library/vue-testing-library/blob/main/src/__tests__/vue-router.js
[vee-validate-example]: https://github.com/testing-library/vue-testing-library/blob/main/src/__tests__/validate-plugin.js
[vue-i18n-example]: https://github.com/testing-library/vue-testing-library/blob/main/src/__tests__/translations-vue-i18n.js
[vuetify-example]: https://github.com/testing-library/vue-testing-library/blob/main/src/__tests__/vuetify.js