Skip to content

Commit 3d465cc

Browse files
committed
fix(guards): propagate lazy loading rejections
1 parent 7eb4280 commit 3d465cc

File tree

3 files changed

+15
-6
lines changed

3 files changed

+15
-6
lines changed

__tests__/guards/extractComponentsGuards.spec.ts

+11
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ const SingleGuardNamed: RouteRecordRaw = {
3333
other: { ...components.Foo, beforeRouteEnter },
3434
},
3535
}
36+
const ErrorLazyLoad: RouteRecordRaw = {
37+
path: '/',
38+
component: () => Promise.reject(new Error('custom')),
39+
}
3640

3741
beforeEach(() => {
3842
beforeRouteEnter.mockReset()
@@ -96,4 +100,11 @@ describe('extractComponentsGuards', () => {
96100
await checkGuards([WrongLazyRoute], 0, 1)
97101
expect('Promise instead of a function').toHaveBeenWarned()
98102
})
103+
104+
it('rejects if lazy load fails', async () => {
105+
await expect(checkGuards([ErrorLazyLoad], 0, 1)).rejects.toHaveProperty(
106+
'message',
107+
'custom'
108+
)
109+
})
99110
})

__tests__/lazyLoading.spec.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ describe('Lazy Loading', () => {
264264
await router.push('/foo').catch(spy)
265265

266266
expect(spy).toHaveBeenCalled()
267-
expect(console.error).toHaveBeenCalledWith(error)
267+
expect(spy).toHaveBeenLastCalledWith(error)
268268

269269
expect(router.currentRoute.value).toMatchObject({
270270
path: '/',
@@ -307,10 +307,11 @@ describe('Lazy Loading', () => {
307307
const spy = jest.fn()
308308

309309
parent.resolve()
310-
child.reject()
310+
const error = new Error()
311+
child.reject(error)
311312
await router.push('/foo').catch(spy)
312313

313-
expect(spy).toHaveBeenCalledWith(expect.any(Error))
314+
expect(spy).toHaveBeenCalledWith(error)
314315

315316
expect(router.currentRoute.value).toMatchObject({
316317
path: '/',

src/navigationGuards.ts

-3
Original file line numberDiff line numberDiff line change
@@ -296,9 +296,6 @@ export function extractComponentsGuards(
296296
`Component "${name}" in record with path "${record.path}" is a function that does not return a Promise. If you were passing a functional component, make sure to add a "displayName" to the component. This will break in production if not fixed.`
297297
)
298298
componentPromise = Promise.resolve(componentPromise as RouteComponent)
299-
} else {
300-
// display the error if any
301-
componentPromise = componentPromise.catch(console.error)
302299
}
303300

304301
guards.push(() =>

0 commit comments

Comments
 (0)