Writing and Running Component Tests

Tutorial 2 of 5

Writing and Running Component Tests in Vue.js Applications

1. Introduction

This tutorial aims to guide you through the process of writing and running component tests in Vue.js applications. Testing is a critical part of software development that ensures your code runs as expected and helps prevent bugs.

By the end of this tutorial, you will be able to:
- Understand the importance and principles of component testing in Vue.js
- Write tests for individual Vue components
- Run these tests to validate your components

Prerequisites:
- Basic understanding of Vue.js and JavaScript
- Vue CLI installed on your system

2. Step-by-Step Guide

We'll be using Vue Test Utils, the official unit testing utility library for Vue.js. To install it, run the following command:

npm install --save @vue/test-utils

Basic Component Testing

Consider a simple Vue component 'HelloWorld.vue':

<template>
  <div class="hello">
    <h1>{{ msg }}</h1>
  </div>
</template>

<script>
export default {
  name: 'HelloWorld',
  props: {
    msg: String
  }
}
</script>

To test this component, we will:

  1. Import Vue and the Component
  2. Import the vue-test-utils library
  3. Use the mount function to create a wrapper of the Component
  4. Assert that the Component is a Vue instance

Here is a simple test for the HelloWorld component:

// import Vue and the component being tested
import Vue from 'vue'
import HelloWorld from '@/components/HelloWorld.vue'
import { mount } from '@vue/test-utils'

describe('HelloWorld.vue', () => {
  it('is a Vue instance', () => {
    const wrapper = mount(HelloWorld, {
      propsData: {
        msg: 'Hello Vue'
      }
    })
    expect(wrapper.isVueInstance()).toBeTruthy()
  })
})

3. Code Examples

Testing Rendered Output

it('renders props.msg when passed', () => {
  const msg = 'new message'
  const wrapper = mount(HelloWorld, {
    propsData: { msg }
  })
  expect(wrapper.text()).toBe(msg)
})

This test asserts that when the msg prop is passed to the component, it correctly renders the value in the component's template.

Testing User Interaction

it('increments count when button is clicked', () => {
  const wrapper = mount(Counter)
  wrapper.find('button').trigger('click')
  expect(wrapper.vm.count).toBe(1)
})

This test asserts that when the button in the Counter component is clicked, it increments the count data property.

4. Summary

In this tutorial, we learned how to write and run component tests in Vue.js applications. We learned how to test the component output and user interactions.

Next, you might want to learn how to test Vue Router, Vuex and other Vue plugins. You can also write tests for your custom directives and filters.

5. Practice Exercises

  1. Write a test for a Vue component that emits an event.
  2. Write a test for a Vue component that uses Vuex.
  3. Write a test for a Vue component that uses Vue Router.

Remember to run your tests after writing them to ensure they pass. Keep practicing and exploring different scenarios. Happy testing!