Skip to content

Commit b203b0d

Browse files
committed
Add missing public methods API docs
1 parent dfe411c commit b203b0d

File tree

3 files changed

+70
-25
lines changed

3 files changed

+70
-25
lines changed

docs/api/index.md

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ sidebar: auto
44

55
# API Reference
66

7-
## mount()
7+
## mount
88

99
Creates a Wrapper that contains the mounted and rendered Vue component to test.
1010

@@ -1584,11 +1584,47 @@ The `Vue` app instance. You can access all of the [instance methods](https://v3.
15841584
`vm` is only available on a `VueWrapper`.
15851585
:::
15861586

1587-
## Global Config
1587+
## shallowMount
15881588

1589-
### config
1589+
Creates a Wrapper that contains the mounted and rendered Vue component to test with all children stubbed out.
15901590

1591-
#### config.global
1591+
**Signature:**
1592+
1593+
```ts
1594+
interface MountingOptions<Props, Data = {}> {
1595+
attachTo?: HTMLElement | string
1596+
attrs?: Record<string, unknown>
1597+
data?: () => {} extends Data ? any : Data extends object ? Partial<Data> : any
1598+
props?: (RawProps & Props) | ({} extends Props ? null : never)
1599+
slots?: { [key: string]: Slot } & { default?: Slot }
1600+
global?: GlobalMountOptions
1601+
shallow?: boolean
1602+
}
1603+
1604+
function mount(Component, options?: MountingOptions): VueWrapper
1605+
```
1606+
1607+
**Details:**
1608+
1609+
`shallowMount` behaves exactly like `mount`, but it stubs all child components by default. Essentially, `shallowMount(Component)` is an alias of `mount(Component, { shallow: true })`.
1610+
1611+
## flushPromises
1612+
1613+
**Signature:**
1614+
1615+
```ts
1616+
flushPromises(): Promise<unknown>
1617+
```
1618+
1619+
**Details:**
1620+
1621+
`flushPromises` flushes al resolved promise handlers. This helps make sure async operations such as promises or DOM updates have happened before asserting against them.
1622+
1623+
Check out [Making HTTP requests](../guide/advanced/http-requests.md) to see an example of `flushPromises` in action.
1624+
1625+
## config
1626+
1627+
### config.global
15921628

15931629
**Signature:**
15941630

docs/guide/advanced/async-suspense.md

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
You may have noticed some other parts of the guide using `await` when calling some methods on `wrapper`, such as `trigger` and `setValue`. What's that all about?
44

5-
You might know Vue updates reactively; when you change a value, the DOM is automatically updated to reflect the latest value. Vue does this _asynchronously_. In contrast, a test runner like Jest runs _synchronously_. This can cause some surprising results in tests. Let's look at some strategies to ensure Vue is updating the DOM as expected when we run our tests.
5+
You might know Vue updates reactively: when you change a value, the DOM is automatically updated to reflect the latest value. [Vue does these updates asynchronously](https://v3.vuejs.org/guide/change-detection.html#async-update-queue). In contrast, a test runner like Jest runs _synchronously_. This can cause some surprising results in tests.
6+
7+
Let's look at some strategies to ensure Vue is updating the DOM as expected when we run our tests.
68

79
## A Simple Example - Updating with `trigger`
810

@@ -11,7 +13,7 @@ Let's re-use the `<Counter>` component from [event handling](../essentials/event
1113
```js
1214
const Counter = {
1315
template: `
14-
Count: {{ count }}
16+
<p>Count: {{ count }}</p>
1517
<button @click="handleClick">Increment</button>
1618
`,
1719
data() {
@@ -39,11 +41,15 @@ test('increments by 1', () => {
3941
})
4042
```
4143

42-
Surprisingly, this fails! The reason is although `count` is increased, Vue will not update the DOM until the next "tick" or "render cycle". For this reason, the assertion will be called before Vue updates the DOM. This has to do with the concept of "macrotasks", "microtasks" and the JavaScript Event Loop. You can read more details and see a simple example [here](https://javascript.info/event-loop#macrotasks-and-microtasks).
44+
Surprisingly, this fails! The reason is although `count` is increased, Vue will not update the DOM until the next event loop "tick. For this reason, the assertion (`expect()...`) will be called before Vue updates the DOM.
45+
46+
:::tip
47+
If you want to learn more about this core JavaScript behavior, read about the [Event Loop and its macrotasks and microtasks](https://javascript.info/event-loop#macrotasks-and-microtasks).
48+
:::
4349

44-
Implementation details aside, how can we fix this? Vue actually provides a way for us to wait until the DOM is updated: `nextTick`:
50+
Implementation details aside, how can we fix this? Vue actually provides a way for us to wait until the DOM is updated: `nextTick`.
4551

46-
```js {7}
52+
```js {1,7}
4753
import { nextTick } from 'vue'
4854

4955
test('increments by 1', async () => {
@@ -56,7 +62,9 @@ test('increments by 1', async () => {
5662
})
5763
```
5864

59-
Now the test will pass, because we ensure the next "tick" has executed updated the DOM before the assertion runs. Since `await nextTick()` is common, VTU provides a shortcut. Methods than cause the DOM to update, such as `trigger` and `setValue` return `nextTick`! So you can just `await` those directly:
65+
Now the test will pass, because we ensure the next "tick" has executed updated the DOM before the assertion runs.
66+
67+
Since `await nextTick()` is common, Vue Test Utils provides a shortcut. Methods than cause the DOM to update, such as `trigger` and `setValue` return `nextTick`, so you can just `await` those directly:
6068

6169
```js {4}
6270
test('increments by 1', async () => {
@@ -70,44 +78,45 @@ test('increments by 1', async () => {
7078

7179
## Resolving Other Asynchronous Behavior
7280

73-
`nextTick` is useful to ensure some change in reactivty data is reflected in the DOM before continuing the test. However, sometimes you may want to ensure other, non Vue-related asynchronous behavior is completed, too. A common example is a function that returns a `Promise` that will lead to a change in the DOM. Perhaps you mocked your `axios` HTTP client using `jest.mock`:
81+
`nextTick` is useful to ensure some change in reactive data is reflected in the DOM before continuing the test. However, sometimes you may want to ensure other, non Vue-related asynchronous behavior is completed, too.
82+
83+
A common example is a function that returns a `Promise`. Perhaps you mocked your `axios` HTTP client using `jest.mock`:
7484

7585
```js
7686
jest.mock('axios', () => ({
7787
get: () => Promise.resolve({ data: 'some mocked data!' })
7888
}))
7989
```
8090

81-
In this case, Vue has no knowledge of the unresolved Promise, so calling `nextTick` will not work - your assertion may run before it is resolved. For scenarios like this, you can use `[flush-promises](https://www.npmjs.com/package/flush-promises)`, which causes all outstanding promises to resolve immediately.
91+
In this case, Vue has no knowledge of the unresolved Promise, so calling `nextTick` will not work - your assertion may run before it is resolved. For scenarios like this, Vue Test Utils exposes [`flushPromises`](../../api/#flushPromises), which causes all outstanding promises to resolve immediately.
8292

8393
Let's see an example:
8494

85-
```js
86-
import flushPromises from 'flush-promises'
95+
```js{1,12}
96+
import { flushPromises } from '@vue/test-utils'
8797
import axios from 'axios'
8898
8999
jest.mock('axios', () => ({
90-
get: () =>
91-
new Promise((resolve) => {
92-
resolve({ data: 'some mocked data!' })
93-
})
100+
get: () => Promise.resolve({ data: 'some mocked data!' })
94101
}))
95102
96103
test('uses a mocked axios HTTP client and flush-promises', async () => {
97104
// some component that makes a HTTP called in `created` using `axios`
98105
const wrapper = mount(AxiosComponent)
99106
100-
await flushPromises() // axios promise is resolved immediately!
107+
await flushPromises() // axios promise is resolved immediately
101108
102-
// assertions!
109+
// after line above, axios request has resolved with the mocked data.
103110
})
104111
```
105112

106-
> If you haven't tested Components with API requests before, you can learn more in [HTTP Requests](./http-requests).
113+
:::tip
114+
If you want to learn more about testing requests on Components, make sure you check [Making HTTP Requests](http-requests.md) guide.
115+
:::
107116

108117
## Conclusion
109118

110-
- Vue updates the DOM asynchronously; tests runner execute code synchronously.
119+
- Vue updates the DOM asynchronously; tests runner execute code synchronously instead.
111120
- Use `await nextTick()` to ensure the DOM has updated before the test continues
112-
- Functions that might update the DOM, like `trigger` and `setValue` return `nextTick`, so you should `await` them.
113-
- Use `flush-promises` to resolve any unresolved promises from non-Vue dependencies.
121+
- Functions that might update the DOM (like `trigger` and `setValue`) return `nextTick`, so you need `await` them.
122+
- Use `flush-promises` from Vue Test Utils to resolve any unresolved promises from non-Vue dependencies (such as API requests).

docs/guide/essentials/easy-to-test.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Think in terms of inputs and outputs from a user perspective. Roughly, this is e
2222
| Events | Emitted events (using `$emit`) |
2323
| Side Effects | Such as `console.log` or API calls |
2424

25-
### Everything else is implementation details
25+
**Everything else is implementation details**.
2626

2727
Notice how this list does not include elements such as internal methods, intermediate states or even data.
2828

0 commit comments

Comments
 (0)