- 인스턴스 프로퍼티 추가하기
- Form Validation
- Editable SVG Icon Systems
- Create a CMS-Powered Blog
- Unit Testing Vue Components
- Creating Custom Scroll Directives
- Debugging in VS Code
- Using Axios to Consume APIs
- Avoiding Memory Leaks
- Client-Side Storage
- Packaging Vue Components for npm
- Dockerize Vue.js App
- Practical use of scoped slots with GoogleMaps
Unit Testing Vue Components
Unit testing is a fundamental part of software development. Unit tests execute the smallest units of code in isolation, in order to increase ease of adding new features and track down bugs. 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.
This simple example tests whether some text is rendered:
The above code snippet shows how to test whether an error message is rendered based on the length of the username. It demonstrates the general idea of unit testing Vue components: render the component, and assert that the markup matches the state of the component.
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
Automated testing allows large teams of developers to maintain complex codebases.
Vue Test Utils is the official library for unit testing Vue components. The vue-cli
webpack template comes with either Karma or Jest, both well supported test runners, and there are some guides in the Vue Test Utils documentation.
Unit tests should be:
- Fast to run
- Easy to understand
- Only test a single unit of work
Let’s continue building on the previous example, while introducing the idea of a factory function to make our test more compact and readable. The component should:
- show a ‘Welcome to the Vue.js cookbook’ greeting.
- prompt the user to enter their username
- display an error if the entered username is less than seven letters
Let’s take a look at the component code first:
The things that we should test are:
- is the
<div class="error">should be present
<div class="error">should not be present
And our first attempt at test:
There are some problems with the above:
- a single test is making assertions about different things
- difficult to tell the different states the component can be in, and what should be rendered
The below example improves the test by:
- only making one assertion per
- having short, clear test descriptions
- providing only the minimum data required for the test
- refactoring duplicated logic (creating the
wrapperand setting the
usernamevariable) into a factory function
Points to note:
At the top, we declare the factory function which merges the
values object into
data and returns a new
wrapper instance. This way, we don’t need to duplicate
const wrapper = shallowMount(Foo) in every test. Another great benefit to this is when more complex components with a method or computed property you might want to mock or stub in every test, you only need to declare it once.
The above test is fairly simple, but in practice Vue components often have other behaviors you want to test, such as:
- making API calls
- committing or dispatching mutations or actions with a
- testing interaction
There are more complete examples showing such tests in the Vue Test Utils guides.
Snapshot tests save the markup of your Vue component, and compare to the new one generated each time the test runs. If something changes, the developer is notified, and can decide if the change was intentional (the component was updated) or accidental (the component is behaving incorrectly).
End to end tests ensure a number of components interact well together. They are more high level. Some examples might be testing if a user can sign up, log in, and update their username. These are slower to run than unit tests or snapshot tests.
Unit tests are most useful during development, either to help a developer think about how to design a component, or refactor an existing component, and are often run every time code is changed.
Higher level tests, such as end to end tests, run much slower. These usually run pre-deploy, to ensure each part of the system is working together correctly.
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.