Skip to content

Commit dfddcbf

Browse files
committed
fix: adapt rest of the unit tests to Vue Test Utils
1 parent b2b3fc8 commit dfddcbf

File tree

1 file changed

+14
-14
lines changed

1 file changed

+14
-14
lines changed

src/v2/guide/unit-testing.md

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,26 +22,29 @@ You don't have to do anything special in your components to make them testable.
2222
message: 'hello!'
2323
}
2424
},
25-
created () {
25+
mounted () {
2626
this.message = 'bye!'
2727
}
2828
}
2929
</script>
3030
```
3131

32-
Then import the component options along with Vue, and you can make many common assertions (here we are using Jasmine/Jest style `expect` assertions just as an example):
32+
Then import the component along with [Vue Test Utils](https://vue-test-utils.vuejs.org/), and you can make many common assertions (here we are using Jest-style `expect` assertions just as an example):
3333

3434
``` js
35-
// Import Vue and the component being tested
36-
import Vue from 'vue'
37-
import MyComponent from 'path/to/MyComponent.vue'
35+
// Import `shallowMount` from Vue Test Utils and the component being tested
36+
import { shallowMount } from '@vue/test-utils'
37+
import MyComponent from './MyComponent.vue'
38+
39+
// Mount the component
40+
const wrapper = shallowMount(MyComponent)
3841

39-
// Here are some Jasmine 2.0 tests, though you can
40-
// use any test runner / assertion library combo you prefer
42+
// Here are some Jest tests, though you can
43+
// use any test runner/assertion library combo you prefer
4144
describe('MyComponent', () => {
4245
// Inspect the raw component options
43-
it('has a created hook', () => {
44-
expect(typeof MyComponent.created).toBe('function')
46+
it('has a mounted hook', () => {
47+
expect(typeof MyComponent.mounted).toBe('function')
4548
})
4649

4750
// Evaluate the results of functions in
@@ -54,15 +57,12 @@ describe('MyComponent', () => {
5457

5558
// Inspect the component instance on mount
5659
it('correctly sets the message when created', () => {
57-
const vm = new Vue(MyComponent).$mount()
58-
expect(vm.message).toBe('bye!')
60+
expect(wrapper.vm.$data.message).toBe('bye!')
5961
})
6062

6163
// Mount an instance and inspect the render output
6264
it('renders the correct message', () => {
63-
const Constructor = Vue.extend(MyComponent)
64-
const vm = new Constructor().$mount()
65-
expect(vm.$el.textContent).toBe('bye!')
65+
expect(wrapper.text()).toBe('bye!')
6666
})
6767
})
6868
```

0 commit comments

Comments
 (0)