|
| 1 | +# Introduction to unit tests |
| 2 | + |
| 3 | +Unit testing is the process of writing automatic tests for units of code. In Vue, components are our units. |
| 4 | + |
| 5 | +Vue Test Utils is geared towards testing Vue components. Let's look at a quick overview. |
| 6 | + |
| 7 | +## Writing unit tests with Vue Test Utils |
| 8 | + |
| 9 | +The process of unit testing Vue components with Vue Test Utils is to mount a component in memory. Mounting a component is a bit like setting the component running. The `mount` and `shallow` methods returns a `Wrapper` object that contains the mounted component, and helper methods to perform assertions on the component and to traverse the render tree. |
| 10 | + |
| 11 | +```js |
| 12 | +import { mount } from 'vue-test-utils' |
| 13 | + |
| 14 | +const wrapper = mount(Component) // returns a Wrapper containing a mounted Component instance |
| 15 | +wrapper.text() // returns text of mounted component inside Wrapper |
| 16 | +wrapper.vm // the mounted Vue instance |
| 17 | +``` |
| 18 | + |
| 19 | +`shallow` stubs the children of a component before it mounts it: |
| 20 | + |
| 21 | +```js |
| 22 | +import { shallow } from 'vue-test-utils' |
| 23 | + |
| 24 | +const wrapper = shallow(Component) // returns a Wrapper containing a mounted Component instance |
| 25 | +wrapper.vm // the mounted Vue instance |
| 26 | +``` |
| 27 | + |
| 28 | +Often, you'll need to pass data to the Component. For example, you might need to pass props to a component. You can do this with mount options: |
| 29 | + |
| 30 | +```js |
| 31 | +import { mount } from 'vue-test-utils' |
| 32 | + |
| 33 | +mount(Component, { |
| 34 | + propsData: { |
| 35 | + aProp: 'some value' |
| 36 | + } |
| 37 | +}) |
| 38 | +``` |
| 39 | + |
| 40 | +*For a full list of options, please see the mount options section of the docs.* |
| 41 | + |
| 42 | +Vue follows a pattern of injecting properties. Normally this is done in a main.js file that creates the Vue instance. This won't be run when you're unit testing, so you'll often need to add properties to the Vue instance yourself. You can do that with the `intercept` option. |
| 43 | + |
| 44 | +```js |
| 45 | +import { mount } from 'vue-test-utils' |
| 46 | + |
| 47 | +const $store = { |
| 48 | + getters: |
| 49 | +} |
| 50 | +mount(Component, { |
| 51 | + intercept: { |
| 52 | + $store // adds the $store object to the Vue instance before mounting component |
| 53 | + } |
| 54 | +}) |
| 55 | +``` |
| 56 | + |
| 57 | +# Testing single file components |
| 58 | + |
| 59 | +Most Vue tests will be testing single file components (SFCs). These files require precompilation before they can be run in Node or in the browser. |
| 60 | + |
| 61 | +The two recommended ways are to test with a Jest preprocessor, or use Webpack. |
| 62 | + |
| 63 | +You can learn how to test SFCs by reading these guides: |
| 64 | + |
| 65 | +- [Testing SFCs with Jest]() |
| 66 | + |
| 67 | +## Knowing what to test |
| 68 | + |
| 69 | +It's impossible to say exactly what you should test and what you shouldn't, but there are some general rules you can follow. |
| 70 | + |
| 71 | +You should write test for custom logic inside Vue components. The best way to test the logic of component is to make sure an input creates the expected output. |
| 72 | + |
| 73 | +For example, imagine we have a Counter component that increments a display counter by 1 each time a button is clicked. The test should perform the click and assert that the counter increased by 1. This ensures that your test is not implementation specific. You can rewrite the logic of the test, and as long as clicking a button still updates the counter text, the test will pass. |
| 74 | + |
| 75 | +## Resourcs |
| 76 | + |
| 77 | +- [Getting started with Jest]() |
| 78 | +- [Testing SFCs with Jest]() |
| 79 | + |
0 commit comments