Skip to content

Commit ee1efd6

Browse files
committed
feat: add a way to turn off error swallowing
1 parent a188088 commit ee1efd6

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

src/router.ts

+17-2
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,25 @@ export interface RouterMockOptions extends Partial<RouterOptions> {
7474
* START_LOCATION.
7575
*/
7676
initialLocation?: RouteLocationRaw
77+
7778
/**
7879
* Run in-component guards. Defaults to false
7980
*/
8081
runInComponentGuards?: boolean
82+
8183
/**
8284
* Run per-route guards. Defaults to false
8385
*/
8486
runPerRouteGuards?: boolean
87+
88+
/**
89+
* By default the mock will allow you to push to locations without adding all
90+
* the necessary routes so you can still check if `router.push()` was called
91+
* in a specific scenario
92+
* (https://github.com/posva/vue-router-mock/issues/41). Set this to `true` to
93+
* disable that behavior and throw when `router.push()` fails.
94+
*/
95+
noUndeclaredRoutes?: boolean
8596
}
8697

8798
/**
@@ -101,7 +112,7 @@ export function createRouterMock(options: RouterMockOptions = {}): RouterMock {
101112
...options,
102113
})
103114

104-
let { runPerRouteGuards, runInComponentGuards } = options
115+
let { runPerRouteGuards, runInComponentGuards, noUndeclaredRoutes } = options
105116
const initialLocation = options.initialLocation || START_LOCATION
106117

107118
const { push, addRoute, replace } = router
@@ -197,7 +208,11 @@ export function createRouterMock(options: RouterMockOptions = {}): RouterMock {
197208
try {
198209
// NOTE: should we trigger a push to reset the internal pending navigation of the router?
199210
router.currentRoute.value = router.resolve(to)
200-
} catch (e) {}
211+
} catch (error) {
212+
if (noUndeclaredRoutes) {
213+
throw error
214+
}
215+
}
201216
return Promise.resolve()
202217
}
203218

0 commit comments

Comments
 (0)