Skip to content

Commit 3468621

Browse files
committed
dont use shallowMount because it sucks
1 parent 436912e commit 3468621

15 files changed

+65
-65
lines changed

demo-app/tests/unit/Bilingual.spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import { shallowMount } from "@vue/test-utils"
1+
import { mount } from "@vue/test-utils"
22
import Bilingual from "@/components/Bilingual.vue"
33

44
describe("Bilingual", () => {
55
it("renders successfully", () => {
6-
const wrapper = shallowMount(Bilingual)
6+
const wrapper = mount(Bilingual)
77

88
expect(wrapper.find(".hello").text()).not.toBe("")
99
})

demo-app/tests/unit/ComponentWithButtons.spec.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import Vuex from "vuex"
2-
import { createLocalVue, shallowMount } from "@vue/test-utils"
2+
import { createLocalVue, mount } from "@vue/test-utils"
33
import ComponentWithButtons from "@/components/ComponentWithButtons.vue"
44

55
const localVue = createLocalVue()
@@ -16,7 +16,7 @@ const store = new Vuex.Store({
1616
describe("ComponentWithButtons", () => {
1717

1818
it("commits a mutation when a button is clicked", async () => {
19-
const wrapper = shallowMount(ComponentWithButtons, {
19+
const wrapper = mount(ComponentWithButtons, {
2020
store, localVue
2121
})
2222

@@ -33,7 +33,7 @@ describe("ComponentWithButtons", () => {
3333
const store = new Vuex.Store()
3434
store.dispatch = jest.fn()
3535

36-
const wrapper = shallowMount(ComponentWithButtons, {
36+
const wrapper = mount(ComponentWithButtons, {
3737
store, localVue
3838
})
3939

@@ -49,7 +49,7 @@ describe("ComponentWithButtons", () => {
4949

5050
it("dispatches an action when a button is clicked", async () => {
5151
const mockStore = { dispatch: jest.fn() }
52-
const wrapper = shallowMount(ComponentWithButtons, {
52+
const wrapper = mount(ComponentWithButtons, {
5353
mocks: {
5454
$store: mockStore
5555
}

demo-app/tests/unit/ComponentWithVuex.spec.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import Vuex from "vuex"
2-
import { shallowMount, createLocalVue } from "@vue/test-utils"
2+
import { mount, createLocalVue } from "@vue/test-utils"
33
import ComponentWithVuex from "@/components/ComponentWithVuex.vue"
44
import ComponentWithGetters from "@/components/ComponentWithGetters.vue"
55

@@ -20,13 +20,13 @@ const store = new Vuex.Store({
2020

2121
describe("ComponentWithVuex", () => {
2222
it("renders a username using a real Vuex store", () => {
23-
const wrapper = shallowMount(ComponentWithVuex, { store, localVue })
23+
const wrapper = mount(ComponentWithVuex, { store, localVue })
2424

2525
expect(wrapper.find(".username").text()).toBe("alice")
2626
})
2727

2828
it("renders a username using a mock store", () => {
29-
const wrapper = shallowMount(ComponentWithVuex, {
29+
const wrapper = mount(ComponentWithVuex, {
3030
mocks: {
3131
$store: {
3232
state: { username: "alice" }
@@ -40,13 +40,13 @@ describe("ComponentWithVuex", () => {
4040

4141
describe("ComponentWithGetters", () => {
4242
it("renders a username using a real Vuex getter", () => {
43-
const wrapper = shallowMount(ComponentWithGetters, { store, localVue })
43+
const wrapper = mount(ComponentWithGetters, { store, localVue })
4444

4545
expect(wrapper.find(".fullname").text()).toBe("Alice Doe")
4646
})
4747

4848
it("renders a username using computed mounting options", () => {
49-
const wrapper = shallowMount(ComponentWithGetters, {
49+
const wrapper = mount(ComponentWithGetters, {
5050
mocks: {
5151
$store: {
5252
getters: {
@@ -60,7 +60,7 @@ describe("ComponentWithGetters", () => {
6060
})
6161

6262
it("renders a username using computed mounting options", () => {
63-
const wrapper = shallowMount(ComponentWithGetters, {
63+
const wrapper = mount(ComponentWithGetters, {
6464
computed: {
6565
fullname: () => "Alice Doe"
6666
}

demo-app/tests/unit/Emitter.spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import Emitter from "@/components/Emitter.vue"
2-
import { shallowMount } from "@vue/test-utils"
2+
import { mount } from "@vue/test-utils"
33

44
describe("Emitter", () => {
55
it("emits an event with two arguments", () => {
6-
const wrapper = shallowMount(Emitter)
6+
const wrapper = mount(Emitter)
77

88
wrapper.vm.emitEvent()
99

demo-app/tests/unit/FormSubmitter.spec.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import flushPromises from "flush-promises"
2-
import { shallowMount } from "@vue/test-utils"
2+
import { mount } from "@vue/test-utils"
33
import FormSubmitter from "@/components/FormSubmitter.vue"
44

55
let url = ''
@@ -17,7 +17,7 @@ const mockHttp = {
1717

1818
describe("FormSubmitter", () => {
1919
it("reveals a notification when submitted", async () => {
20-
const wrapper = shallowMount(FormSubmitter)
20+
const wrapper = mount(FormSubmitter)
2121

2222
wrapper.find("[data-username]").setValue("alice")
2323
wrapper.find("form").trigger("submit.prevent")
@@ -28,7 +28,7 @@ describe("FormSubmitter", () => {
2828
})
2929

3030
it("reveals a notification when submitted", async () => {
31-
const wrapper = shallowMount(FormSubmitter, {
31+
const wrapper = mount(FormSubmitter, {
3232
data() {
3333
return {
3434
asyncTest: true

demo-app/tests/unit/NumberRenderer.spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import { shallowMount } from "@vue/test-utils"
1+
import { mount } from "@vue/test-utils"
22
import NumberRenderer from "@/components/NumberRenderer.vue"
33

44
describe("NumberRenderer", () => {
55
it("renders even numbers", () => {
6-
const wrapper = shallowMount(NumberRenderer, {
6+
const wrapper = mount(NumberRenderer, {
77
propsData: {
88
even: true
99
}

demo-app/tests/unit/Parent.spec.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
import { shallowMount } from "@vue/test-utils"
1+
import { mount } from "@vue/test-utils"
22
import Parent from "@/components/Parent.vue"
33
import ParentWithManyChildren from "@/components/ParentWithManyChildren.vue"
44
import Child from "@/components/Child.vue"
55

66
describe("Parent", () => {
77
it("does not render a span", () => {
8-
const wrapper = shallowMount(Parent)
8+
const wrapper = mount(Parent)
99

1010
expect(wrapper.find("span").isVisible()).toBe(false)
1111
})
1212

1313
it("does render a span", () => {
14-
const wrapper = shallowMount(Parent, {
14+
const wrapper = mount(Parent, {
1515
data() {
1616
return { showSpan: true }
1717
}
@@ -21,13 +21,13 @@ describe("Parent", () => {
2121
})
2222

2323
it("does not render a Child component", () => {
24-
const wrapper = shallowMount(Parent)
24+
const wrapper = mount(Parent)
2525

2626
expect(wrapper.find(Child).exists()).toBe(false)
2727
})
2828

2929
it("renders a Child component", () => {
30-
const wrapper = shallowMount(Parent, {
30+
const wrapper = mount(Parent, {
3131
data() {
3232
return { showChild: true }
3333
}
@@ -39,7 +39,7 @@ describe("Parent", () => {
3939

4040
describe("ParentWithManyChildren", () => {
4141
it("renders many children", () => {
42-
const wrapper = shallowMount(ParentWithManyChildren)
42+
const wrapper = mount(ParentWithManyChildren)
4343

4444
expect(wrapper.findAll(Child).length).toBe(3)
4545
})

src/components-with-props.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,13 @@ export default {
5353
We will make an assertion on the message in the case the user does not have admin privileges.
5454

5555
```js
56-
import { shallowMount } from '@vue/test-utils'
56+
import { mount } from '@vue/test-utils'
5757
import SubmitButton from '@/components/SubmitButton.vue'
5858

5959
describe('SubmitButton.vue', () => {
6060
it("displays a non authorized message", () => {
6161
const msg = "submit"
62-
const wrapper = shallowMount(SubmitButton,{
62+
const wrapper = mount(SubmitButton,{
6363
propsData: {
6464
msg: msg
6565
}
@@ -99,14 +99,14 @@ We can see the `msg` prop is processed and the resulting markup is correctly ren
9999
Let's make an assertion on the other possible state, when `isAdmin` is `true`:
100100

101101
```js
102-
import { shallowMount } from '@vue/test-utils'
102+
import { mount } from '@vue/test-utils'
103103
import SubmitButton from '@/components/SubmitButton.vue'
104104

105105
describe('SubmitButton.vue', () => {
106106
it('displays a admin privileges message', () => {
107107
const msg = "submit"
108108
const isAdmin = true
109-
const wrapper = shallowMount(SubmitButton,{
109+
const wrapper = mount(SubmitButton,{
110110
propsData: {
111111
msg,
112112
isAdmin
@@ -147,12 +147,12 @@ Let's refactor the tests adhering to the principle "Don't Repeat Yourself" (DRY)
147147

148148
## Refactor with a Factory Function
149149

150-
In both tests we call `shallowMount` then pass a similar `propsData` object. We can refactor this using a factory function. A factory function is simply a function that returns an object - it _makes_ objects, thus the name "factory" function.
150+
In both tests we call `mount` then pass a similar `propsData` object. We can refactor this using a factory function. A factory function is simply a function that returns an object - it _makes_ objects, thus the name "factory" function.
151151

152152
```js
153153
const msg = "submit"
154154
const factory = (propsData) => {
155-
return shallowMount(SubmitButton, {
155+
return mount(SubmitButton, {
156156
propsData: {
157157
msg,
158158
...propsData
@@ -161,7 +161,7 @@ const factory = (propsData) => {
161161
}
162162
```
163163

164-
The above is a function that will `shallowMount` a `SubmitButton` component. We can pass any props to change as the first argument to `factory`. Let's DRY up the test with the factory function.
164+
The above is a function that will `mount` a `SubmitButton` component. We can pass any props to change as the first argument to `factory`. Let's DRY up the test with the factory function.
165165

166166
```js
167167
describe("SubmitButton", () => {

src/composition-api.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,13 @@ The two things we will need to test here are:
7171
Testing the message is correctly rendered is trivial. We just use `propsData` to set the value of the prop, as described [here](/components-with-props.html).
7272

7373
```js
74-
import { shallowMount } from "@vue/test-utils"
74+
import { mount } from "@vue/test-utils"
7575

7676
import CompositionApi from "@/components/CompositionApi.vue"
7777

7878
describe("CompositionApi", () => {
7979
it("renders a message", () => {
80-
const wrapper = shallowMount(CompositionApi, {
80+
const wrapper = mount(CompositionApi, {
8181
propsData: {
8282
message: "Testing the composition API"
8383
}
@@ -95,13 +95,13 @@ As expected, this is very simple - regardless of the way we are composing out co
9595
Writing a test to ensure clicking the button increments the `state.count` is equally simple. Notice the test is marked `async`; read more about why this is required in [Simulating User Input](simulating-user-input.html#writing-the-test).
9696

9797
```js
98-
import { shallowMount } from "@vue/test-utils"
98+
import { mount } from "@vue/test-utils"
9999

100100
import CompositionApi from "@/components/CompositionApi.vue"
101101

102102
describe("CompositionApi", () => {
103103
it("increments a count when button is clicked", async () => {
104-
const wrapper = shallowMount(CompositionApi, {
104+
const wrapper = mount(CompositionApi, {
105105
propsData: { message: '' }
106106
})
107107

src/computed-properties.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ The `<NumberRenderer>` component will receive an `even` prop, that is a boolean.
1515
The test:
1616

1717
```js
18-
import { shallowMount } from "@vue/test-utils"
18+
import { mount } from "@vue/test-utils"
1919
import NumberRenderer from "@/components/NumberRenderer.vue"
2020

2121
describe("NumberRenderer", () => {
2222
it("renders even numbers", () => {
23-
const wrapper = shallowMount(NumberRenderer, {
23+
const wrapper = mount(NumberRenderer, {
2424
propsData: {
2525
even: true
2626
}
@@ -195,7 +195,7 @@ FAIL tests/unit/NumberRenderer.spec.js
195195

196196
So we need to use `call`, which lets us bind an alternative `this` object, in our case, one with a `even` property.
197197

198-
## To `call` or to `shallowMount`?
198+
## To `call` or to `mount`?
199199

200200
Both techniques presented are useful for testing computed properties. Call can be useful when:
201201

@@ -206,5 +206,5 @@ Of course, you want to make sure the value is correctly rendered as well, so mak
206206

207207
## Conclusion
208208

209-
- computed properties can be using `shallowMount` making assertions on the rendered markup
209+
- computed properties can be using `mount` making assertions on the rendered markup
210210
- complex computed properties can be independently tested by using `call`

src/finding-elements-and-components.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,12 @@ export default {
5757
Regular elements can easily be selected using the syntax used with `document.querySelector`. `vue-test-utils` also provides a `isVisible` method to check if elements conditionally rendered with `v-show` are visible. Create a `Parent.spec.js`, and inside add the following test:
5858

5959
```js
60-
import { mount, shallowMount } from "@vue/test-utils"
60+
import { mount } from "@vue/test-utils"
6161
import Parent from "@/components/Parent.vue"
6262

6363
describe("Parent", () => {
6464
it("does not render a span", () => {
65-
const wrapper = shallowMount(Parent)
65+
const wrapper = mount(Parent)
6666

6767
expect(wrapper.find("span").isVisible()).toBe(false)
6868
})
@@ -73,7 +73,7 @@ Since `v-show="showSpan"` defaults to `false`, we expect the found `<span>` elem
7373

7474
```js
7575
it("does render a span", () => {
76-
const wrapper = shallowMount(Parent, {
76+
const wrapper = mount(Parent, {
7777
data() {
7878
return { showSpan: true }
7979
}
@@ -98,7 +98,7 @@ These are a bit easier to understand in the context of an example test. Let's st
9898
import Child from "@/components/Child.vue"
9999

100100
it("does not render a Child component", () => {
101-
const wrapper = shallowMount(Parent)
101+
const wrapper = mount(Parent)
102102

103103
expect(wrapper.find(Child).exists()).toBe(false)
104104
})
@@ -110,7 +110,7 @@ As mentioned in the previous paragraph, the `name` property is one of the checks
110110

111111
```js
112112
it("renders a Child component", () => {
113-
const wrapper = shallowMount(Parent, {
113+
const wrapper = mount(Parent, {
114114
data() {
115115
return { showChild: true }
116116
}
@@ -148,7 +148,7 @@ We can write a test using `findAll` to assert three `<Child>` components are ren
148148

149149
```js
150150
it("renders many children", () => {
151-
const wrapper = shallowMount(ParentWithManyChildren)
151+
const wrapper = mount(ParentWithManyChildren)
152152

153153
expect(wrapper.findAll(Child).length).toBe(3)
154154
})

src/mocking-global-objects.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,12 @@ export default {
4848
Based on the locale, the correct translation is rendered. Let's try and render the component in a test, without any mocking.
4949

5050
```js
51-
import { shallowMount } from "@vue/test-utils"
51+
import { mount } from "@vue/test-utils"
5252
import Bilingual from "@/components/Bilingual.vue"
5353

5454
describe("Bilingual", () => {
5555
it("renders successfully", () => {
56-
const wrapper = shallowMount(Bilingual)
56+
const wrapper = mount(Bilingual)
5757
})
5858
})
5959
```
@@ -67,12 +67,12 @@ Running this test with `yarn test:unit` throws a huge stack trace. If you look t
6767
This is because we did not install `vue-i18n`, so the global `$t` method does not exist. Let's mock it using the `mocks` mounting option:
6868

6969
```js
70-
import { shallowMount } from "@vue/test-utils"
70+
import { mount } from "@vue/test-utils"
7171
import Bilingual from "@/components/Bilingual.vue"
7272

7373
describe("Bilingual", () => {
7474
it("renders successfully", () => {
75-
const wrapper = shallowMount(Bilingual, {
75+
const wrapper = mount(Bilingual, {
7676
mocks: {
7777
$t: (msg) => msg
7878
}
@@ -109,7 +109,7 @@ Now a real translation will be rendered, despite using a mocked `$t` function. R
109109
```js
110110
describe("Bilingual", () => {
111111
it("renders successfully", () => {
112-
const wrapper = shallowMount(Bilingual)
112+
const wrapper = mount(Bilingual)
113113

114114
console.log(wrapper.html())
115115
})

0 commit comments

Comments
 (0)