Top Related Projects
JavaScript Testing utilities for React
🦎 Simple and complete Vue.js testing utilities that encourage good testing practices.
Fast, easy and reliable testing for anything that runs in a browser.
JavaScript API for Chrome and Firefox
Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.
Quick Overview
Vue Test Utils is the official unit testing utility library for Vue.js applications. It provides a set of tools and methods to make testing Vue components easier and more efficient, allowing developers to write comprehensive tests for their Vue applications.
Pros
- Seamless integration with Vue.js ecosystem
- Comprehensive API for mounting and interacting with components
- Supports both Vue 2 and Vue 3
- Easy to use with popular testing frameworks like Jest and Mocha
Cons
- Learning curve for developers new to unit testing
- Documentation can be overwhelming for beginners
- Some edge cases may require complex setups
- Limited support for testing certain advanced Vue features
Code Examples
- Mounting a component:
import { mount } from '@vue/test-utils'
import MyComponent from './MyComponent.vue'
test('mounts a component', () => {
const wrapper = mount(MyComponent)
expect(wrapper.text()).toContain('Welcome to My Component')
})
- Testing component props:
import { mount } from '@vue/test-utils'
import MyComponent from './MyComponent.vue'
test('renders props.msg when passed', () => {
const msg = 'new message'
const wrapper = mount(MyComponent, {
props: { msg }
})
expect(wrapper.text()).toMatch(msg)
})
- Simulating user input:
import { mount } from '@vue/test-utils'
import MyForm from './MyForm.vue'
test('emits form data on submit', async () => {
const wrapper = mount(MyForm)
await wrapper.find('input[type="text"]').setValue('John Doe')
await wrapper.find('form').trigger('submit')
expect(wrapper.emitted('submit')).toBeTruthy()
expect(wrapper.emitted('submit')[0][0]).toEqual({ name: 'John Doe' })
})
Getting Started
To start using Vue Test Utils, first install it in your project:
npm install --save-dev @vue/test-utils
Then, in your test file:
import { mount } from '@vue/test-utils'
import MyComponent from './MyComponent.vue'
describe('MyComponent', () => {
test('renders correctly', () => {
const wrapper = mount(MyComponent)
expect(wrapper.exists()).toBe(true)
})
})
Run your tests using your preferred test runner (e.g., Jest, Mocha).
Competitor Comparisons
JavaScript Testing utilities for React
Pros of Enzyme
- More mature and established testing library with a larger ecosystem
- Supports shallow rendering, which can be beneficial for unit testing
- Offers a wider range of utility methods for component manipulation
Cons of Enzyme
- Primarily designed for React, limiting its use in Vue.js projects
- Requires additional setup and configuration for use with Vue.js
- May have a steeper learning curve for developers new to React testing
Code Comparison
Enzyme (React):
import { shallow } from 'enzyme';
const wrapper = shallow(<MyComponent />);
expect(wrapper.find('.my-class')).toHaveLength(1);
wrapper.find('button').simulate('click');
Vue Test Utils:
import { mount } from '@vue/test-utils';
const wrapper = mount(MyComponent);
expect(wrapper.find('.my-class').exists()).toBe(true);
await wrapper.find('button').trigger('click');
Both libraries provide similar functionality for mounting components and interacting with them in tests. However, Enzyme uses methods like simulate
for events, while Vue Test Utils uses trigger
. Enzyme's shallow
rendering is also distinct from Vue Test Utils' mount
approach.
Vue Test Utils is specifically designed for Vue.js, offering seamless integration and Vue-specific features. Enzyme, while powerful, requires additional setup for Vue.js projects and may not provide the same level of Vue-specific functionality out of the box.
🦎 Simple and complete Vue.js testing utilities that encourage good testing practices.
Pros of vue-testing-library
- Encourages testing from a user's perspective, focusing on behavior rather than implementation details
- Simpler API with fewer methods, making it easier to learn and use
- Promotes more maintainable tests that are less likely to break with implementation changes
Cons of vue-testing-library
- Less flexibility for testing component internals or specific implementation details
- Fewer built-in utilities for complex scenarios compared to Vue Test Utils
- May require more setup for certain types of tests, especially those involving vuex or vue-router
Code Comparison
vue-testing-library:
import { render, fireEvent } from '@testing-library/vue'
import Counter from './Counter.vue'
test('increments value on click', async () => {
const { getByText } = render(Counter)
const button = getByText('Increment')
await fireEvent.click(button)
expect(getByText('Count: 1')).toBeTruthy()
})
Vue Test Utils:
import { mount } from '@vue/test-utils'
import Counter from './Counter.vue'
test('increments value on click', async () => {
const wrapper = mount(Counter)
await wrapper.find('button').trigger('click')
expect(wrapper.text()).toContain('Count: 1')
})
Fast, easy and reliable testing for anything that runs in a browser.
Pros of Cypress
- End-to-end testing framework that can test any web application
- Real-time reloading and debugging capabilities
- Extensive documentation and active community support
Cons of Cypress
- Slower test execution compared to unit testing with Vue Test Utils
- Limited to testing in-browser behavior, not suitable for unit testing Vue components in isolation
Code Comparison
Vue Test Utils:
import { mount } from '@vue/test-utils'
import MyComponent from './MyComponent.vue'
test('MyComponent', async () => {
const wrapper = mount(MyComponent)
await wrapper.find('button').trigger('click')
expect(wrapper.text()).toContain('Clicked')
})
Cypress:
describe('MyComponent', () => {
it('clicks button', () => {
cy.visit('/my-component')
cy.get('button').click()
cy.contains('Clicked')
})
})
Summary
Vue Test Utils is specifically designed for testing Vue components, offering a more lightweight and faster approach for unit testing. It provides methods to mount components in isolation and interact with them programmatically.
Cypress, on the other hand, is a comprehensive end-to-end testing framework that simulates user interactions in a real browser environment. It excels in testing full user flows and integrations but may be overkill for simple component testing.
Choose Vue Test Utils for focused Vue component testing and Cypress for broader, full-application testing scenarios.
JavaScript API for Chrome and Firefox
Pros of Puppeteer
- Broader application: Can test any web application, not limited to Vue.js
- Full browser automation: Allows for end-to-end testing and complex user interactions
- Headless browser support: Enables faster test execution and CI/CD integration
Cons of Puppeteer
- Steeper learning curve: Requires more setup and configuration compared to Vue Test Utils
- Less Vue-specific: Lacks built-in Vue component mounting and interaction utilities
- Higher resource usage: Running a full browser instance can be more resource-intensive
Code Comparison
Vue Test Utils:
import { mount } from '@vue/test-utils'
import MyComponent from './MyComponent.vue'
test('MyComponent', async () => {
const wrapper = mount(MyComponent)
await wrapper.find('button').trigger('click')
expect(wrapper.text()).toContain('Clicked')
})
Puppeteer:
const browser = await puppeteer.launch()
const page = await browser.newPage()
await page.goto('https://example.com')
await page.click('button')
const text = await page.$eval('body', el => el.textContent)
expect(text).toContain('Clicked')
Both Vue Test Utils and Puppeteer are valuable testing tools, but they serve different purposes. Vue Test Utils is specifically designed for testing Vue.js components, offering a streamlined API for mounting and interacting with components. Puppeteer, on the other hand, provides a more general-purpose solution for browser automation and end-to-end testing across any web application.
Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.
Pros of Playwright
- Cross-browser support for Chromium, Firefox, and WebKit
- Powerful API for automating web applications beyond just testing
- Built-in test runner with parallel execution capabilities
Cons of Playwright
- Steeper learning curve for developers familiar with Vue ecosystem
- Less Vue-specific functionality out of the box
- Requires more setup for Vue component testing
Code Comparison
Vue Test Utils:
import { mount } from '@vue/test-utils'
import MyComponent from './MyComponent.vue'
test('MyComponent', async () => {
const wrapper = mount(MyComponent)
expect(wrapper.text()).toContain('Hello')
})
Playwright:
import { test, expect } from '@playwright/test'
test('MyComponent', async ({ page }) => {
await page.goto('/my-component')
await expect(page.locator('body')).toContainText('Hello')
})
Summary
Vue Test Utils is specifically designed for testing Vue components, offering a simpler setup for Vue developers. Playwright, on the other hand, provides a more comprehensive solution for end-to-end testing and browser automation across multiple browsers. While Playwright offers more flexibility and power, it may require more effort to set up for Vue-specific testing scenarios. The choice between the two depends on the project's requirements and the development team's preferences.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
Vue Test Utils
Component testing utils for Vue 2.
Packages
This repository provides the following two packages:
You can install these packages by the following command.
npm install --save-dev @vue/test-utils@1
npm install --save-dev @vue/server-test-utils@1
Peer Dependencies
You need to install vue-template-compiler
which is used to compile components. It should be the same version as the version of Vue you are using.
npm install --save-dev vue-template-compiler
Documentation
Refer to the documentation
Questions
For questions and support please use the Discord chat room or the official forum. The issue list of this repo is exclusively for bug reports and feature requests.
Issues
Please make sure to read the issue reporting requirements before opening an issue. Issues not conforming to the guidelines may be closed immediately.
Contribution
Please make sure to read the Contributing Guide before making a pull request.
Changelog
Changes for each release are documented in the release notes.
Stay In Touch
For the latest releases and announcements, follow on Twitter: @vuejs
License
Top Related Projects
JavaScript Testing utilities for React
🦎 Simple and complete Vue.js testing utilities that encourage good testing practices.
Fast, easy and reliable testing for anything that runs in a browser.
JavaScript API for Chrome and Firefox
Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot