@@ -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<!--
84131Links:
85132-->
0 commit comments