You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Vue’s single-file components make it straight forward to write unit tests for components in isolation. This lets you develop new features with confidence you are not breaking existing ones, and helps other developers understand what your component does.
Component unit tests have lots of benefits:
Provide documentation on how the component should behave
Save time over testing manually
Reduce bugs in new features
Improve design
Facilitate refactoring
Unit testing is an important part of any serious application. At first, when the vision of an application is not clear, unit testing might slow down development, but once a vision is established and real users will be interacting with the application, unit tests (and other types of automated tests) are absolutely essential to ensure the codebase is maintainable and scalable.
Test example:
<template>
<div>
<divclass="message">
{{ message }}
</div>
Enter your username: <inputv-model="username">
<divv-if="error"class="error"
>
Please enter a username with at least seven letters.
</div>
</div>
</template>
<script>
exportdefault { name:'Foo',data () {return { message:'Welcome to the Vue.js cookbook', username:'' } }, computed: {error () {returnthis.username.trim().length<7 } }}
</script>
The things that we should test are:
is the message rendered?
if error is true, <div class="error"> should be present
if error is false, <div class="error"> should not be present
import{shallowMount}from'@vue/test-utils'importFoofrom'./Foo'constfactory=(values={})=>{returnshallowMount(Foo,{data(){return{
...values}}})}describe('Foo',()=>{it('renders a welcome message',()=>{constwrapper=factory()expect(wrapper.find('.message').text()).toEqual("Welcome to the Vue.js cookbook")})it('renders an error when username is less than 7 characters',()=>{constwrapper=factory({username: ''})expect(wrapper.find('.error').exists()).toBeTruthy()})it('renders an error when username is whitespace',()=>{constwrapper=factory({username: ' '.repeat(7)})expect(wrapper.find('.error').exists()).toBeTruthy()})it('does not render an error when username is 7 characters or more',()=>{constwrapper=factory({username: 'Lachlan'})expect(wrapper.find('.error').exists()).toBeFalsy()})})
The text was updated successfully, but these errors were encountered:
Related to: #83
Reference:
Vue’s single-file components make it straight forward to write unit tests for components in isolation. This lets you develop new features with confidence you are not breaking existing ones, and helps other developers understand what your component does.
Component unit tests have lots of benefits:
Unit testing is an important part of any serious application. At first, when the vision of an application is not clear, unit testing might slow down development, but once a vision is established and real users will be interacting with the application, unit tests (and other types of automated tests) are absolutely essential to ensure the codebase is maintainable and scalable.
Test example:
The things that we should test are:
message
rendered?error
istrue
,<div class="error">
should be presenterror
isfalse
,<div class="error">
should not be presentThe text was updated successfully, but these errors were encountered: