Skip to content

Commit

Permalink
fix(guards): propagate lazy loading rejections
Browse files Browse the repository at this point in the history
  • Loading branch information
posva committed Jun 1, 2021
1 parent 7eb4280 commit 3d465cc
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
11 changes: 11 additions & 0 deletions __tests__/guards/extractComponentsGuards.spec.ts
Expand Up @@ -33,6 +33,10 @@ const SingleGuardNamed: RouteRecordRaw = {
other: { ...components.Foo, beforeRouteEnter },
},
}
const ErrorLazyLoad: RouteRecordRaw = {
path: '/',
component: () => Promise.reject(new Error('custom')),
}

beforeEach(() => {
beforeRouteEnter.mockReset()
Expand Down Expand Up @@ -96,4 +100,11 @@ describe('extractComponentsGuards', () => {
await checkGuards([WrongLazyRoute], 0, 1)
expect('Promise instead of a function').toHaveBeenWarned()
})

it('rejects if lazy load fails', async () => {
await expect(checkGuards([ErrorLazyLoad], 0, 1)).rejects.toHaveProperty(
'message',
'custom'
)
})
})
7 changes: 4 additions & 3 deletions __tests__/lazyLoading.spec.ts
Expand Up @@ -264,7 +264,7 @@ describe('Lazy Loading', () => {
await router.push('/foo').catch(spy)

expect(spy).toHaveBeenCalled()
expect(console.error).toHaveBeenCalledWith(error)
expect(spy).toHaveBeenLastCalledWith(error)

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

parent.resolve()
child.reject()
const error = new Error()
child.reject(error)
await router.push('/foo').catch(spy)

expect(spy).toHaveBeenCalledWith(expect.any(Error))
expect(spy).toHaveBeenCalledWith(error)

expect(router.currentRoute.value).toMatchObject({
path: '/',
Expand Down
3 changes: 0 additions & 3 deletions src/navigationGuards.ts
Expand Up @@ -296,9 +296,6 @@ export function extractComponentsGuards(
`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.`
)
componentPromise = Promise.resolve(componentPromise as RouteComponent)
} else {
// display the error if any
componentPromise = componentPromise.catch(console.error)
}

guards.push(() =>
Expand Down

0 comments on commit 3d465cc

Please sign in to comment.