단위 테스트는 중요한 (그리고 가끔은 무시되는) 개발의 한 부분입니다. 개발 진행 과정과 업무 흐름을 안전하게 하고, 개발에서 발생할 수 있는 실수나 놓치는 부분으로부터 프로젝트를 보호합니다. 따라서, Vue에서는 자체 테스트 도구인 vue-test-utils을 제공합니다. 이 도구는 Vue component 와 상호작용하기 위한 유용한 기능을 제공하고, 널리 사용되고 있는 많은 테스트 도구와 호환됩니다.
Vuetify utilizes Typescript and currently must import and extend the Vue object. This has the potential in some applications to generate a warning $attrs or $listeners is read-only. There is currenty an on-going Github discussion with potential work-arounds in a variety of use-cases. If you have additional questions please join us in our online community.
Information on how to setup a test runner with Vue CLI can be found on the official documentation. At a glance, Vue CLI has getting started repositories for the following test runners:
In order to properly utilize Typescript, Vuetify components extend the Vue object. This has the potential to cause issues as noted in the above alert. Instead of using a localVue instance we must instead install Vuetify globally in the unit tests setup file. This can vary between test runners. Make sure to reference the appropriate documentation on setup files.
If you are not using a setup.js
file, you should add Vue.use(Vuetify)
in the utilities section of your test.
Creating unit tests in Vuetify are similar to vuex and vue-router in that you will use the Vuetify object in a localVue instance and pass an instance to the mount functions options.
Let's create an example test use-case that we might find in our application.
In the example above we have created a custom component with a title prop and a v-btn
that emits a custom event when clicked. We now want to create tests that ensure this behavior works correctly and continues to do so through future changes. The below examples are created with with the Jest test runner.
If you are stuck and have additional questions about testing or need help in general, please join us in our online community.
When writing tests you will often find yourself repeating the same things over and over. In this case, it's beneficial to create helper functions to reduce the duplication for each individual test. Basically, DRYing up our code.
One of the most common duplicated code written in unit tests are the mount functions. This can easily be compacted into a reusable function for each run.
Many of Vuetify's components utilize the global $vuetify
object to derive settings such as default text or breakpoint information. When testing these components, you will need to provide vue-test-utils
with a mock object.
Keep in mind, you only need to stub the services that are being used. such as lang or application. You can also import these services manually.
A complete list of all services available are listed below:
Vuetify passes the data-*
attributes from components to the relevant HTML elements, which allows E2E test framework to locate them easily.
For example, Cypress recommends to add data-cy
attributes to make it easy to target elements.
더 읽을 준비 되었나요? 그렇다면 아래 설명서를 더 읽어보세요!