Skip to content

Commit cdcfe5d

Browse files
authored
Merge pull request #875 from blimmer/vue-testing-library-instantiated-router
docs(vue): update docs/faq for instantiated router
2 parents e4d1c90 + da8074c commit cdcfe5d

File tree

2 files changed

+50
-2
lines changed

2 files changed

+50
-2
lines changed

docs/vue-testing-library/api.mdx

+3-2
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,11 @@ The object definition of a [Vuex](https://vuex.vuejs.org/) store. If a `store`
7171
object is provided, `Vue Testing Library` will import and configure a Vuex
7272
store. If an instantiated Vuex store is passed, it will be used.
7373

74-
##### `routes` (`Array`)
74+
##### `routes` (`Array` | `VueRouter`)
7575

7676
A set of routes for [Vue Router](https://router.vuejs.org/). If `routes` is
77-
provided, the library will import and configure Vue Router.
77+
provided, the library will import and configure Vue Router. If an instantiated
78+
Vue Router is passed, it will be used.
7879

7980
##### `props` (`Object`)
8081

docs/vue-testing-library/faq.mdx

+47
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,53 @@ expect(queryByText('submit')).not.toBeInTheDocument()
8080

8181
</details>
8282

83+
<details>
84+
<summary>Why does my Vue Router state seem to be shared between tests?</summary>
85+
86+
By default, Vue Router uses
87+
[`hash` routing mode](https://router.vuejs.org/api/#mode), which stores route
88+
updates in `window.location`. Test runners, such as Jest, do not reset the JSDOM
89+
environment in between test invocations, so route transitions from previous
90+
tests can leak into subsequent tests, even though a new Vue Router is created
91+
with each call to `render`.
92+
93+
You can work around this in one of two ways:
94+
95+
1. **Pass an instantiated router using `abstract` mode**. `abstract` mode does
96+
not store route information on `window`, so transitions will not leak between
97+
tests. For example:
98+
99+
```js
100+
import { render, fireEvent } from '@testing-library/vue'
101+
import Component from './Component.vue'
102+
import VueRouter from 'vue-router'
103+
104+
test('uses abstract mode for the router', () => {
105+
render(Component, {
106+
routes: new VueRouter({
107+
mode: 'abstract',
108+
routes: [
109+
// Your routes here
110+
],
111+
}),
112+
})
113+
})
114+
```
115+
116+
2. **Reset the window location `afterEach`**. If you don't want to pass an
117+
instantiated Router, you can instead reset the `window.location` after each
118+
test, like this:
119+
120+
```js
121+
afterEach(() => {
122+
window.location.replace('http://localhost')
123+
})
124+
```
125+
126+
This will clear any route transitions stored in the `window` location property.
127+
128+
</details>
129+
83130
<!--
84131
Links:
85132
-->

0 commit comments

Comments
 (0)