Skip to content

Commit a188088

Browse files
cexbrayatposva
authored andcommitted
feat: catch resolve errors
Adds a try/catch around the route resolution to simplify testing scenarios where the developers use `router.push({name: 'hey'})` and do not want to declare the corresponding route, as they just want to check if `push` was called. Fixes #41
1 parent c31cc03 commit a188088

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

__tests__/navigations.spec.ts

+8
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@ describe('Navigations', () => {
1717
expect(wrapper.vm.$router.push).toHaveBeenCalledTimes(1)
1818
})
1919

20+
it('can check calls on push even if the route is not declared', async () => {
21+
const wrapper = mount(Test)
22+
23+
wrapper.vm.$router.push({ name: 'hey' })
24+
expect(wrapper.vm.$router.push).toHaveBeenCalledWith({ name: 'hey' })
25+
expect(wrapper.vm.$router.push).toHaveBeenCalledTimes(1)
26+
})
27+
2028
it('can check calls on replace', async () => {
2129
const wrapper = mount(Test)
2230

src/router.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,13 @@ export function createRouterMock(options: RouterMockOptions = {}): RouterMock {
191191
return pendingNavigation
192192
}
193193

194-
// NOTE: should we trigger a push to reset the internal pending navigation of the router?
195-
router.currentRoute.value = router.resolve(to)
194+
// we try to resolve the navigation
195+
// but catch the error to simplify testing and avoid having to declare
196+
// all the routes in the mock router
197+
try {
198+
// NOTE: should we trigger a push to reset the internal pending navigation of the router?
199+
router.currentRoute.value = router.resolve(to)
200+
} catch (e) {}
196201
return Promise.resolve()
197202
}
198203

0 commit comments

Comments
 (0)