|
| 1 | +### Upgrading to V1.0 |
| 2 | + |
| 3 | +After a long time in Beta, Vue Test Utils finally released v1.0! We deprecated some methods that were not helpful, so you might see several deprecation warnings after upgrading. This guide will help you migrate away from them. |
| 4 | + |
| 5 | +You can read the release notes for V1 [here](https://github.com/vuejs/vue-test-utils/releases) or the discussion around the deprecations [here](https://github.com/vuejs/rfcs/pull/161). |
| 6 | + |
| 7 | +### `find` |
| 8 | + |
| 9 | +In beta,`find` could be used to find both DOM nodes (using the `querySelector` syntax) or a component (via a component reference, a `ref` or a `name` option). This behavior is now split into two methods: `find` and `findComponent`. |
| 10 | + |
| 11 | +If you were using this syntax: |
| 12 | + |
| 13 | +- `find(Foo)` |
| 14 | +- `find({ name: 'foo' })` |
| 15 | +- `find({ ref: 'my-ref' })` |
| 16 | + |
| 17 | +Change them to be `findComponent`. |
| 18 | + |
| 19 | +You may continue using `find` on DOM nodes using the `querySelector` syntax. |
| 20 | + |
| 21 | +### `isVueInstance` |
| 22 | + |
| 23 | +This method was deprecated because it tends to encourage testing implementation details, which is a bad practice. Assertions using this can simply be removed; if you really need a substitute, you can do `expect((...).vm).toBeTruthy()`, which is basically what `isVueInstance` was doing. |
| 24 | + |
| 25 | +### `contains` |
| 26 | + |
| 27 | +Tests using `contains` can be replaced with `find` or `findComponent` or `get`. For example, `expect(wrapper.contains('#el')).toBe(true)` may be written as `wrapper.get('#el')`, which will throw an error if the selector is not matched. Another way to write this using `find` is `expect(wrapper.find('#el').element).toBeTruthy()`. |
| 28 | + |
| 29 | +### `is` |
| 30 | + |
| 31 | +You may rewrite tests using `is` to use `element.tagName` instead. For example, `wrapper.find('div').is('div')` may be written as `wrapper.find('div').element.tagName`. |
| 32 | + |
| 33 | +### `isEmpty` |
| 34 | + |
| 35 | +Finding out whether a DOM node is empty is not a Vue specific feature, and it is something that is difficult to get right. Rather than re-invent the wheel, we have decided it's better to delegate to an existing, well tested solution by default. Consider the excellent `toBeEmpty` matchers from [jest-dom](https://github.com/testing-library/jest-dom#tobeempty), for example, if you are using Jest. |
| 36 | + |
| 37 | +### `isVisible` |
| 38 | + |
| 39 | +See `isEmpty` above. Consider using [toBeVisible](https://github.com/testing-library/jest-dom#tobevisible) from `jest-dom` if you are using Jest. For example: |
| 40 | + |
| 41 | +```js |
| 42 | +// old assertion |
| 43 | +expect(wrapper.find('.selector').isVisible()).toBeTruthy() |
| 44 | + |
| 45 | +// new assertion |
| 46 | +// consider making this matcher globally availalbe in your tests! |
| 47 | +import '@testing-library/jest-dom' |
| 48 | + |
| 49 | +expect(wrapper.find('.selector').element).toBeVisible() |
| 50 | +``` |
| 51 | + |
| 52 | +### `name` |
| 53 | + |
| 54 | +Asserting against `name` encourages testing implementation details, which is a bad practice. If you need this feature, though, you can use `vm.$options.name` for Vue components or `element.tagName` for DOM nodes. Again, consider if you really need this test - it's likely you don't. |
| 55 | + |
| 56 | +### `setMethods` and `mountingOptions.methods` |
| 57 | + |
| 58 | +By using `setMethods`, you are mutating the Vue instance - this is not something Vue supports, and may lead to coupled, flaky tests. |
| 59 | + |
| 60 | +There is no straight forward replacement for this. If you have a complex method you would like to stub out, consider moving it another file and using your test runner's stub or mock functionality. |
| 61 | + |
| 62 | +For example, you may want to avoid an API call: |
| 63 | + |
| 64 | +```js |
| 65 | +const Foo = { |
| 66 | + created() { |
| 67 | + this.getData() |
| 68 | + }, |
| 69 | + methods: { |
| 70 | + getData() { |
| 71 | + axios.get('...') |
| 72 | + } |
| 73 | + } |
| 74 | +} |
| 75 | +``` |
| 76 | + |
| 77 | +Instead of doing: |
| 78 | + |
| 79 | +```js |
| 80 | +mount(Foo, { |
| 81 | + methods: { |
| 82 | + getData: () => {} |
| 83 | + } |
| 84 | +}) |
| 85 | +``` |
| 86 | + |
| 87 | +Mock out the `axios` dependency. In Jest, for example, you can do `jest.mock('axios')`. This will prevent the API call, without mutating your Vue component. |
| 88 | + |
| 89 | +If you need more help migrating, you can join the [VueLand server](https://chat.vuejs.org/) on Discord. |
| 90 | + |
| 91 | +### Deprecation Warnings |
| 92 | + |
| 93 | +Deprecation warnings can be silenced. |
| 94 | + |
| 95 | +```js |
| 96 | +import { config } from '@vue/test-utils' |
| 97 | + |
| 98 | +config.showDeprecationWarnings = false |
| 99 | +``` |
0 commit comments