]> git.ipfire.org Git - thirdparty/vuejs/router.git/commitdiff
fix(guards): propagate lazy loading rejections
authorEduardo San Martin Morote <posva13@gmail.com>
Tue, 1 Jun 2021 20:27:04 +0000 (22:27 +0200)
committerEduardo San Martin Morote <posva13@gmail.com>
Tue, 1 Jun 2021 20:27:04 +0000 (22:27 +0200)
__tests__/guards/extractComponentsGuards.spec.ts
__tests__/lazyLoading.spec.ts
src/navigationGuards.ts

index 7301ea47da30f10087c389cac9f72220c08cd42a..7dadb979b0b23af052a521c50f776dd22892a20e 100644 (file)
@@ -33,6 +33,10 @@ const SingleGuardNamed: RouteRecordRaw = {
     other: { ...components.Foo, beforeRouteEnter },
   },
 }
+const ErrorLazyLoad: RouteRecordRaw = {
+  path: '/',
+  component: () => Promise.reject(new Error('custom')),
+}
 
 beforeEach(() => {
   beforeRouteEnter.mockReset()
@@ -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'
+    )
+  })
 })
index 54bfffe8e806a0605122a842dff88a2b91c7a16a..9c95a33a5081508de10e5bdd798cdd1568032999 100644 (file)
@@ -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: '/',
@@ -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: '/',
index 185a22f21603dbf77ca7cfda1981f00f57d64a6f..9b6de947648c894adeb8dac362265da878ac2859 100644 (file)
@@ -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(() =>