Unit tests are an important (and sometimes ignored) part of developing applications. They help us secure or processes and work flows, ensuring that the most critical parts of our projects are protected from accidental mistakes or oversights in our development. It's because of that that Vue has its own testing utility called vue-test-utils. It provides useful features for interacting with Vue components and works with many popular test runners.

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.

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.