Convert Figma logo to code with AI

vuejs logotest-utils

Vue Test Utils for Vue 3

1,035
245
1,035
30

Top Related Projects

Component Test Utils for Vue 2

🦎 Simple and complete Vue.js testing utilities that encourage good testing practices.

46,847

Fast, easy and reliable testing for anything that runs in a browser.

44,166

Delightful JavaScript Testing.

12,902

Next generation testing framework powered by Vite.

⚙️ Browser devtools extension for debugging Vue.js applications.

Quick Overview

Vue Test Utils is the official unit testing utility library for Vue.js. It provides a set of tools to simplify the process of testing Vue components, making it easier for developers to write and maintain unit 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
  • Extensive documentation and examples

Cons

  • Learning curve for developers new to unit testing
  • Some advanced testing scenarios may require additional setup
  • Performance can be slower compared to other testing libraries
  • Limited support for testing certain Vue features (e.g., transitions)

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('Hello world')
})

Testing user interaction:

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

test('increments count when button is clicked', async () => {
  const wrapper = mount(Counter)
  await wrapper.find('button').trigger('click')
  expect(wrapper.text()).toContain('Count: 1')
})

Testing emitted events:

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

test('emits submit event when clicked', async () => {
  const wrapper = mount(SubmitButton)
  await wrapper.find('button').trigger('click')
  expect(wrapper.emitted().submit).toBeTruthy()
})

Getting Started

  1. Install Vue Test Utils and a test runner (e.g., Jest):

    npm install --save-dev @vue/test-utils jest
    
  2. Create a test file (e.g., MyComponent.spec.js):

    import { mount } from '@vue/test-utils'
    import MyComponent from './MyComponent.vue'
    
    describe('MyComponent', () => {
      test('renders correctly', () => {
        const wrapper = mount(MyComponent)
        expect(wrapper.text()).toContain('Expected text')
      })
    })
    
  3. Run the tests:

    npx jest
    

For more detailed setup instructions and configuration options, refer to the official Vue Test Utils documentation.

Competitor Comparisons

Component Test Utils for Vue 2

Pros of vue-test-utils

  • More comprehensive documentation and examples
  • Better integration with Vue 3 and Composition API
  • Wider community adoption and support

Cons of vue-test-utils

  • Larger bundle size
  • Steeper learning curve for beginners
  • Some features may be overkill for simple projects

Code Comparison

test-utils:

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

test('Component', () => {
  const wrapper = mount(Component)
  expect(wrapper.text()).toContain('Hello')
})

vue-test-utils:

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

describe('Component', () => {
  it('renders correctly', () => {
    const wrapper = mount(Component)
    expect(wrapper.text()).toContain('Hello')
  })
})

The code comparison shows that both libraries use similar syntax for mounting components and asserting their content. However, vue-test-utils often encourages more structured test organization with describe blocks.

vue-test-utils generally offers more advanced features and helpers for complex testing scenarios, while test-utils provides a simpler API that may be sufficient for basic testing needs. The choice between the two depends on the project's complexity and testing requirements.

🦎 Simple and complete Vue.js testing utilities that encourage good testing practices.

Pros of vue-testing-library

  • Encourages testing user behavior rather than implementation details
  • Simpler API with fewer methods to learn
  • Promotes accessibility-friendly testing practices

Cons of vue-testing-library

  • Less Vue-specific functionality compared to test-utils
  • May require more setup for complex component testing scenarios
  • Smaller ecosystem and community compared to test-utils

Code Comparison

test-utils:

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

test('Component', async () => {
  const wrapper = mount(Component)
  await wrapper.find('button').trigger('click')
  expect(wrapper.text()).toContain('Clicked')
})

vue-testing-library:

import { render, fireEvent } from '@testing-library/vue'
import Component from './Component.vue'

test('Component', async () => {
  const { getByRole, getByText } = render(Component)
  await fireEvent.click(getByRole('button'))
  expect(getByText('Clicked')).toBeInTheDocument()
})

Both libraries provide effective ways to test Vue components, but they differ in their approach and API. test-utils offers more Vue-specific features and a larger ecosystem, while vue-testing-library focuses on testing from a user's perspective and encourages more accessible testing practices. The choice between them often depends on project requirements and team preferences.

46,847

Fast, easy and reliable testing for anything that runs in a browser.

Pros of Cypress

  • End-to-end testing framework with real-time browser interaction
  • Built-in debugging tools and time-travel capabilities
  • Automatic waiting and retrying for more stable tests

Cons of Cypress

  • Limited to testing web applications only
  • Slower test execution compared to unit testing frameworks
  • Steeper learning curve for developers new to end-to-end testing

Code Comparison

Cypress test example:

describe('Login', () => {
  it('should log in successfully', () => {
    cy.visit('/login')
    cy.get('#username').type('user@example.com')
    cy.get('#password').type('password123')
    cy.get('button[type="submit"]').click()
    cy.url().should('include', '/dashboard')
  })
})

Vue Test Utils example:

import { mount } from '@vue/test-utils'
import LoginForm from '@/components/LoginForm.vue'

test('emits login event with credentials', async () => {
  const wrapper = mount(LoginForm)
  await wrapper.find('#username').setValue('user@example.com')
  await wrapper.find('#password').setValue('password123')
  await wrapper.find('form').trigger('submit')
  expect(wrapper.emitted().login).toBeTruthy()
})

While Cypress focuses on end-to-end testing with browser interaction, Vue Test Utils is designed for unit testing Vue components. Cypress provides a more comprehensive testing experience but may be overkill for simple component testing. Vue Test Utils offers a lightweight solution for testing Vue-specific functionality but lacks the ability to test full user flows across multiple pages.

44,166

Delightful JavaScript Testing.

Pros of Jest

  • Broader testing framework for JavaScript, not limited to Vue.js
  • Built-in code coverage reporting and mocking capabilities
  • Extensive documentation and large community support

Cons of Jest

  • Steeper learning curve for Vue-specific testing scenarios
  • Requires additional configuration for optimal Vue.js integration
  • May include unnecessary features for Vue-only projects

Code Comparison

Vue Test Utils:

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

test('Component renders correctly', () => {
  const wrapper = mount(Component)
  expect(wrapper.text()).toContain('Hello, World!')
})

Jest:

import React from 'react'
import { render, screen } from '@testing-library/react'
import Component from './Component'

test('Component renders correctly', () => {
  render(<Component />)
  expect(screen.getByText('Hello, World!')).toBeInTheDocument()
})

Summary

Jest is a versatile JavaScript testing framework with broad application, while Vue Test Utils is specifically designed for Vue.js applications. Jest offers more comprehensive features but may require additional setup for Vue projects. Vue Test Utils provides a more streamlined experience for Vue-specific testing but lacks some of Jest's advanced capabilities. The choice between the two depends on project requirements and the development team's familiarity with each tool.

12,902

Next generation testing framework powered by Vite.

Pros of Vitest

  • Faster test execution due to native ESM support and parallelization
  • Broader ecosystem compatibility, supporting various frameworks beyond Vue
  • Built-in code coverage and watch mode features

Cons of Vitest

  • Steeper learning curve for developers familiar with Vue Test Utils
  • Less Vue-specific optimizations and helper functions
  • May require additional configuration for Vue-specific testing scenarios

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')
})

Vitest:

import { mount } from '@vue/test-utils'
import { describe, it, expect } from 'vitest'
import MyComponent from './MyComponent.vue'

describe('MyComponent', () => {
  it('renders correctly', () => {
    const wrapper = mount(MyComponent)
    expect(wrapper.text()).toContain('Hello')
  })
})

While both examples use Vue Test Utils for mounting components, Vitest provides a more flexible testing structure with describe and it blocks, allowing for better organization of test suites. However, the core testing logic remains similar between the two approaches.

⚙️ Browser devtools extension for debugging Vue.js applications.

Pros of devtools-v6

  • Provides a comprehensive debugging and inspection tool for Vue.js applications
  • Offers real-time component tree visualization and state inspection
  • Integrates seamlessly with browser developer tools for enhanced workflow

Cons of devtools-v6

  • Focused on development and debugging rather than testing
  • May have a steeper learning curve for new Vue.js developers
  • Limited utility in automated testing scenarios

Code Comparison

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')
})

devtools-v6:

// No direct code comparison available as devtools-v6 is primarily used as a browser extension
// However, it can be integrated into a Vue.js application for custom debugging:

import { setupDevtoolsPlugin } from '@vue/devtools-api'

setupDevtoolsPlugin({
  id: 'my-awesome-devtools-plugin',
  // Plugin implementation
}, api => {
  // Use the devtools API
})

Summary

test-utils is primarily focused on unit and component testing for Vue.js applications, providing a robust set of utilities for mounting components and asserting their behavior. On the other hand, devtools-v6 is a powerful development and debugging tool that offers real-time inspection and visualization of Vue.js applications. While test-utils is essential for maintaining code quality through automated testing, devtools-v6 excels in providing developers with insights into their application's structure and state during development.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Vue Test Utils

Component testing utils for Vue 3.

Languages

🇫🇷 French version of this README.md

Installation and Usage

  • yarn: yarn add @vue/test-utils --dev
  • npm: npm install @vue/test-utils --save-dev

Get started with the documentation.

Coming from Vue 2 + Test Utils v1?

Check the migration guide. It's still a work in progress. If you find a problem or something that doesn't work that previously did in Vue Test Utils v1, please open an issue.

Documentation

See the docs.

Development

Get started by running pnpm install. You can run the tests with pnpm test. That's it!

Contributing Docs

All the documentation files can be found in packages/docs. It contains the English markdown files while translation(s) are stored in their corresponding <lang> sub-folder(s):

  • fr: French translation.

Besides that, the .vitepress sub-folder contains the config and theme, including the i18n information.

  • pnpm docs:dev: Start the docs dev server.
  • pnpm docs:build: Build the docs.

To add or maintain the translations, we follow the Vue Ecosystem Translation Guidelines.

  • pnpm docs:translation:status [<lang>]: Show the translation status for your language. If you don't specify a language, it will show the status for all languages.
  • pnpm docs:translation:compare <lang>: Compare the docs with the latest checkpoint for your language.
  • pnpm docs:translation:update <lang> [<commit>]: Update the checkpoint for your language. The checkpoint will be set by the latest commit hash. However, you can also specify a commit hash manually.

NPM DownloadsLast 30 Days