]> git.ipfire.org Git - thirdparty/vuejs/router.git/commitdiff
test: refactor next usage
authorEduardo San Martin Morote <posva13@gmail.com>
Thu, 18 Sep 2025 15:16:58 +0000 (17:16 +0200)
committerEduardo San Martin Morote <posva13@gmail.com>
Thu, 18 Sep 2025 15:16:58 +0000 (17:16 +0200)
12 files changed:
packages/router/__tests__/errors.spec.ts
packages/router/__tests__/guards/beforeEach.spec.ts
packages/router/__tests__/guards/beforeEnter.spec.ts
packages/router/__tests__/guards/beforeRouteEnter.spec.ts
packages/router/__tests__/guards/beforeRouteLeave.spec.ts
packages/router/__tests__/guards/beforeRouteUpdate.spec.ts
packages/router/__tests__/guards/extractComponentsGuards.spec.ts
packages/router/__tests__/initialNavigation.spec.ts
packages/router/__tests__/lazyLoading.spec.ts
packages/router/__tests__/multipleApps.spec.ts
packages/router/__tests__/router.spec.ts
packages/router/src/experimental/router.spec.ts

index 5678bf43faa874b0280baa02a4b418d8b79da467..3abb1677d8df8a79ea7ad2ce20030bc3256b4ca8 100644 (file)
@@ -86,9 +86,9 @@ describe('Errors & Navigation failures', () => {
 
   it('next("/location") triggers afterEach', async () => {
     await testNavigation(
-      ((to, from, next) => {
-        if (to.path === '/location') next()
-        else next('/location')
+      ((to, from) => {
+        if (to.path === '/location') return
+        else return '/location'
       }) as NavigationGuard,
       undefined
     )
@@ -109,10 +109,13 @@ describe('Errors & Navigation failures', () => {
   it('triggers afterEach if a new navigation happens', async () => {
     const { router } = createRouter()
     const [promise, resolve] = fakePromise()
-    router.beforeEach((to, from, next) => {
+    router.beforeEach(async (to, from) => {
       // let it hang otherwise
-      if (to.path === '/') next()
-      else promise.then(() => next())
+      if (to.path === '/') return
+      else {
+        await promise
+        return
+      }
     })
 
     let from = router.currentRoute.value
@@ -236,9 +239,9 @@ describe('Errors & Navigation failures', () => {
 
     it('next("/location") triggers afterEach with history.back', async () => {
       await testHistoryNavigation(
-        ((to, from, next) => {
-          if (to.path === '/location') next()
-          else next('/location')
+        ((to, from) => {
+          if (to.path === '/location') return
+          else return '/location'
         }) as NavigationGuard,
         undefined
       )
index b4e373606cffa1d1fd3552088f1df446c3fff19b..a421a81e2f53ca0afe0b11e1e3be01fe60d228a4 100644 (file)
@@ -87,10 +87,10 @@ describe('router.beforeEach', () => {
     const spy = vi.fn()
     const router = createRouter({ routes })
     await router.push('/foo')
-    spy.mockImplementation((to, from, next) => {
+    spy.mockImplementation((to, from) => {
       // only allow going to /other
-      if (to.fullPath !== '/other') next('/other')
-      else next()
+      if (to.fullPath !== '/other') return '/other'
+      else return
     })
     router.beforeEach(spy)
     expect(spy).not.toHaveBeenCalled()
@@ -160,11 +160,11 @@ describe('router.beforeEach', () => {
     const spy = vi.fn()
     const router = createRouter({ routes })
     await router.push('/')
-    spy.mockImplementation((to, from, next) => {
+    spy.mockImplementation((to, from) => {
       // only allow going to /other
       const i = Number(to.params.i)
-      if (i >= 3) next()
-      else next(redirectFn(String(i + 1)))
+      if (i >= 3) return
+      else return redirectFn(String(i + 1))
     })
     router.beforeEach(spy)
     expect(spy).not.toHaveBeenCalled()
@@ -210,9 +210,9 @@ describe('router.beforeEach', () => {
   it('waits before navigating', async () => {
     const [promise, resolve] = fakePromise()
     const router = createRouter({ routes })
-    router.beforeEach(async (to, from, next) => {
+    router.beforeEach(async (to, from) => {
       await promise
-      next()
+      return
     })
     const p = router.push('/foo')
     expect(router.currentRoute.value.fullPath).toBe('/')
@@ -227,17 +227,17 @@ describe('router.beforeEach', () => {
     const router = createRouter({ routes })
     const guard1 = vi.fn()
     let order = 0
-    guard1.mockImplementationOnce(async (to, from, next) => {
+    guard1.mockImplementationOnce(async (to, from) => {
       expect(order++).toBe(0)
       await p1
-      next()
+      return
     })
     router.beforeEach(guard1)
     const guard2 = vi.fn()
-    guard2.mockImplementationOnce(async (to, from, next) => {
+    guard2.mockImplementationOnce(async (to, from) => {
       expect(order++).toBe(1)
       await p2
-      next()
+      return
     })
     router.beforeEach(guard2)
     let navigation = router.push('/foo')
index 1df476eee3c2d9b62f6c7f06aebb6fcea8693d2e..7c9031f5c0579ed885708e439b157d1405de99f7 100644 (file)
@@ -157,9 +157,9 @@ describe('beforeEnter', () => {
   it('waits before navigating', async () => {
     const [promise, resolve] = fakePromise()
     const router = createRouter({ routes })
-    beforeEnter.mockImplementationOnce(async (to, from, next) => {
+    beforeEnter.mockImplementationOnce(async (to, from) => {
       await promise
-      next()
+      return
     })
     const p = router.push('/foo')
     expect(router.currentRoute.value.fullPath).toBe('/')
@@ -172,13 +172,13 @@ describe('beforeEnter', () => {
     const [p1, r1] = fakePromise()
     const [p2, r2] = fakePromise()
     const router = createRouter({ routes })
-    beforeEnters[0].mockImplementationOnce(async (to, from, next) => {
+    beforeEnters[0].mockImplementationOnce(async (to, from) => {
       await p1
-      next()
+      return
     })
-    beforeEnters[1].mockImplementationOnce(async (to, from, next) => {
+    beforeEnters[1].mockImplementationOnce(async (to, from) => {
       await p2
-      next()
+      return
     })
     const p = router.push('/multiple')
     expect(router.currentRoute.value.fullPath).toBe('/')
index 43268f2b58874e07246d5d6a42eaf7edd8fca0fa..cca03c833ea472f5cf90e5d6d2b52473775071ce 100644 (file)
@@ -111,9 +111,9 @@ describe('beforeRouteEnter', () => {
 
   it('calls beforeRouteEnter guards on navigation', async () => {
     const router = createRouter({ routes })
-    beforeRouteEnter.mockImplementationOnce((to, from, next) => {
-      if (to.params.n !== 'valid') return next(false)
-      next()
+    beforeRouteEnter.mockImplementationOnce((to, from) => {
+      if (to.params.n !== 'valid') return false
+      return
     })
     await router.push('/guard/valid')
     expect(beforeRouteEnter).toHaveBeenCalledTimes(1)
@@ -183,8 +183,8 @@ describe('beforeRouteEnter', () => {
 
   it('aborts navigation if one of the named views aborts', async () => {
     const router = createRouter({ routes })
-    named.default.mockImplementationOnce((to, from, next) => {
-      next(false)
+    named.default.mockImplementationOnce((to, from) => {
+      return false
     })
     named.other.mockImplementationOnce(noGuard)
     await router.push('/named').catch(err => {}) // catch abort
@@ -204,9 +204,9 @@ describe('beforeRouteEnter', () => {
   it('waits before navigating', async () => {
     const [promise, resolve] = fakePromise()
     const router = createRouter({ routes })
-    beforeRouteEnter.mockImplementationOnce(async (to, from, next) => {
+    beforeRouteEnter.mockImplementationOnce(async (to, from) => {
       await promise
-      next()
+      return
     })
     const p = router.push('/foo')
     expect(router.currentRoute.value.fullPath).toBe('/')
index 57dd78158f13e8fd8f85151ccc31dc1c87e0dbbf..c92726e44ec1b7c04827d3bb606374f94d55d9f1 100644 (file)
@@ -94,9 +94,9 @@ describe('beforeRouteLeave', () => {
 
   it('calls beforeRouteLeave guard on navigation', async () => {
     const router = createRouter({ routes })
-    beforeRouteLeave.mockImplementationOnce((to, from, next) => {
-      if (to.path === 'foo') next(false)
-      else next()
+    beforeRouteLeave.mockImplementationOnce((to, from) => {
+      if (to.path === 'foo') return false
+      else return
     })
     await router.push('/guard')
     expect(beforeRouteLeave).not.toHaveBeenCalled()
@@ -110,8 +110,8 @@ describe('beforeRouteLeave', () => {
 
   it('does not call beforeRouteLeave guard if the view is not mounted', async () => {
     const router = createRouter({ routes })
-    beforeRouteLeave.mockImplementationOnce((to, from, next) => {
-      next()
+    beforeRouteLeave.mockImplementationOnce((to, from) => {
+      return
     })
     await router.push('/guard')
     expect(beforeRouteLeave).not.toHaveBeenCalled()
@@ -158,17 +158,17 @@ describe('beforeRouteLeave', () => {
     await router.push({ name: 'nested-nested-foo' })
     resetMocks()
     let count = 0
-    nested.nestedNestedFoo.mockImplementation((to, from, next) => {
+    nested.nestedNestedFoo.mockImplementation((to, from) => {
       expect(count++).toBe(0)
-      next()
+      return
     })
-    nested.nestedNested.mockImplementation((to, from, next) => {
+    nested.nestedNested.mockImplementation((to, from) => {
       expect(count++).toBe(1)
-      next()
+      return
     })
-    nested.parent.mockImplementation((to, from, next) => {
+    nested.parent.mockImplementation((to, from) => {
       expect(count++).toBe(2)
-      next()
+      return
     })
 
     // simulate a mounted route component
@@ -184,8 +184,8 @@ describe('beforeRouteLeave', () => {
 
   it('can cancel navigation', async () => {
     const router = createRouter({ routes })
-    beforeRouteLeave.mockImplementationOnce(async (to, from, next) => {
-      next(false)
+    beforeRouteLeave.mockImplementationOnce(async (to, from) => {
+      return false
     })
     await router.push('/guard')
     const p = router.push('/')
index d4cbf98eb9626a2bd1e175076bec06d0f7db5e55..3efb1d7ca1b3578b94e861b33a96478b06bfc826 100644 (file)
@@ -68,9 +68,9 @@ describe('beforeRouteUpdate', () => {
   it('waits before navigating', async () => {
     const [promise, resolve] = fakePromise()
     const router = createRouter({ routes })
-    beforeRouteUpdate.mockImplementationOnce(async (to, from, next) => {
+    beforeRouteUpdate.mockImplementationOnce(async (to, from) => {
       await promise
-      next()
+      return
     })
     await router.push('/guard/one')
     const p = router.push('/guard/foo')
index b10a322472504285a2d3f033e4ee87c5741c6ab2..efe5ba500e63325410b1a58bf3b095233c563126 100644 (file)
@@ -40,8 +40,8 @@ const ErrorLazyLoad: RouteRecordRaw = {
 
 beforeEach(() => {
   beforeRouteEnter.mockReset()
-  beforeRouteEnter.mockImplementation((to, from, next) => {
-    next()
+  beforeRouteEnter.mockImplementation((to, from) => {
+    return
   })
 })
 
index c30d7fd3422608fe99b5f591dcfc439144ad59b0..4e2e63787fac92eba19771246efdf94952e0fa4f 100644 (file)
@@ -19,8 +19,8 @@ const routes: RouteRecordRaw[] = [
   {
     path: '/home-before',
     component,
-    beforeEnter: (to, from, next) => {
-      next('/')
+    beforeEnter: (to, from) => {
+      return '/'
     },
   },
   { path: '/bar', component },
index 1a8210937bce606bfcea4857908e266570512a7d..178bad95bb7c6a59420bb151d3066612a2141ab4 100644 (file)
@@ -159,7 +159,7 @@ describe('Lazy Loading', () => {
 
   it('avoid fetching async component if navigation is cancelled through beforeEnter', async () => {
     const { component, resolve } = createLazyComponent()
-    const spy = vi.fn((to, from, next) => next(false))
+    const spy = vi.fn((to, from) => false)
     const { router } = newRouter({
       routes: [
         {
@@ -187,7 +187,7 @@ describe('Lazy Loading', () => {
       ],
     })
 
-    const spy = vi.fn((to, from, next) => next(false))
+    const spy = vi.fn((to, from) => false)
 
     router.beforeEach(spy)
 
@@ -199,7 +199,7 @@ describe('Lazy Loading', () => {
 
   it('invokes beforeRouteEnter after lazy loading the component', async () => {
     const { promise, resolve } = createLazyComponent()
-    const spy = vi.fn((to, from, next) => next())
+    const spy = vi.fn((to, from) => {})
     const component = vi.fn(() =>
       promise.then(() => ({ beforeRouteEnter: spy }))
     )
@@ -215,7 +215,7 @@ describe('Lazy Loading', () => {
 
   it('beforeRouteLeave works on a lazy loaded component', async () => {
     const { promise, resolve } = createLazyComponent()
-    const spy = vi.fn((to, from, next) => next())
+    const spy = vi.fn((to, from) => {})
     const component = vi.fn(() =>
       promise.then(() => ({ beforeRouteLeave: spy }))
     )
@@ -240,7 +240,7 @@ describe('Lazy Loading', () => {
 
   it('beforeRouteUpdate works on a lazy loaded component', async () => {
     const { promise, resolve } = createLazyComponent()
-    const spy = vi.fn((to, from, next) => next())
+    const spy = vi.fn((to, from) => {})
     const component = vi.fn(() =>
       promise.then(() => ({ beforeRouteUpdate: spy }))
     )
index 664cd08afc2529ca0cef695242634c6795aac6ff..1433c64b0af3ab9ad249fce1e89b225b1bd4b1e5 100644 (file)
@@ -34,8 +34,8 @@ describe('Multiple apps', () => {
   it('does not listen to url changes before being ready', async () => {
     const { router, history } = newRouter()
 
-    const spy = vi.fn((to, from, next) => {
-      next()
+    const spy = vi.fn((to, from) => {
+      return
     })
     router.beforeEach(spy)
 
index f835f41e395c5380d09467b8b60d17e25bf99961..b46ac5eb5042a8074f69862daddea33b19f07961 100644 (file)
@@ -20,8 +20,8 @@ const routes: RouteRecordRaw[] = [
   {
     path: '/home-before',
     component: components.Home,
-    beforeEnter: (to, from, next) => {
-      next('/')
+    beforeEnter: (to, from) => {
+      return '/'
     },
   },
   { path: '/search', component: components.Home },
@@ -277,7 +277,7 @@ describe('Router', () => {
 
   it('navigates if the location does not exist', async () => {
     const { router } = await newRouter({ routes: [routes[0]] })
-    const spy = vi.fn((to, from, next) => next())
+    const spy = vi.fn((to, from) => {})
     router.beforeEach(spy)
     await router.push('/idontexist')
     expect(spy).toHaveBeenCalledTimes(1)
@@ -502,7 +502,7 @@ describe('Router', () => {
   describe('alias', () => {
     it('does not navigate to alias if already on original record', async () => {
       const { router } = await newRouter()
-      const spy = vi.fn((to, from, next) => next())
+      const spy = vi.fn((to, from) => {})
       await router.push('/basic')
       router.beforeEach(spy)
       await router.push('/basic-alias')
@@ -511,7 +511,7 @@ describe('Router', () => {
 
     it('does not navigate to alias with children if already on original record', async () => {
       const { router } = await newRouter()
-      const spy = vi.fn((to, from, next) => next())
+      const spy = vi.fn((to, from) => {})
       await router.push('/aliases')
       router.beforeEach(spy)
       await router.push('/aliases1')
@@ -522,7 +522,7 @@ describe('Router', () => {
 
     it('does not navigate to child alias if already on original record', async () => {
       const { router } = await newRouter()
-      const spy = vi.fn((to, from, next) => next())
+      const spy = vi.fn((to, from) => {})
       await router.push('/aliases/one')
       router.beforeEach(spy)
       await router.push('/aliases1/one')
@@ -559,15 +559,15 @@ describe('Router', () => {
       const [p1, r1] = fakePromise()
       const history = createMemoryHistory()
       const router = createRouter({ history, routes })
-      router.beforeEach(async (to, from, next) => {
-        if (to.name !== 'Param') return next()
+      router.beforeEach(async (to, from) => {
+        if (to.name !== 'Param') return
         // the first navigation gets passed target
         if (to.params.p === 'a') {
           await p1
-          target ? next(target) : next()
+          return target || undefined
         } else {
           // the second one just passes
-          next()
+          return
         }
       })
       const from = router.currentRoute.value
@@ -613,17 +613,17 @@ describe('Router', () => {
       await router.push('/p/a')
       await router.push('/p/b')
 
-      router.beforeEach(async (to, from, next) => {
-        if (to.name !== 'Param') return next()
+      router.beforeEach(async (to, from) => {
+        if (to.name !== 'Param') return
         if (to.fullPath === '/foo') {
           await p1
-          next()
+          return
         } else if (from.fullPath === '/p/b') {
           await p2
           // @ts-ignore: same as function above
-          next(target)
+          return target
         } else {
-          next()
+          return
         }
       })
 
@@ -697,7 +697,7 @@ describe('Router', () => {
     it('only triggers guards once with a redirect option', async () => {
       const history = createMemoryHistory()
       const router = createRouter({ history, routes })
-      const spy = vi.fn((to, from, next) => next())
+      const spy = vi.fn((to, from) => {})
       router.beforeEach(spy)
       await router.push('/to-foo')
       expect(spy).toHaveBeenCalledTimes(1)
@@ -974,15 +974,15 @@ describe('Router', () => {
         name: 'dynamic parent',
         end: false,
         strict: true,
-        beforeEnter(to, from, next) {
+        beforeEnter(to, from) {
           if (!removeRoute) {
             removeRoute = router.addRoute('dynamic parent', {
               path: 'child',
               name: 'dynamic child',
               component: components.Foo,
             })
-            next(to.fullPath)
-          } else next()
+            return to.fullPath
+          } else return
         },
       })
 
index 4e179f4099eb60de78eff30fe6b4bb2ab9a45a1c..ca6a5ec9e63e37a641221e7057f04e38310b0aef 100644 (file)
@@ -314,7 +314,7 @@ describe('Experimental Router', () => {
     const homeOnlyRoutes = [experimentalRoutes.find(r => r.name === 'home')!]
     const resolver = createFixedResolver(homeOnlyRoutes)
     const { router } = await newRouter({ resolver })
-    const spy = vi.fn((to, from, next) => next())
+    const spy = vi.fn((_to, _from) => {})
     router.beforeEach(spy)
     await router.push('/idontexist')
     expect(spy).toHaveBeenCalledTimes(1)
@@ -466,15 +466,15 @@ describe('Experimental Router', () => {
       const history = createMemoryHistory()
       const resolver = createFixedResolver(experimentalRoutes)
       const router = experimental_createRouter({ history, resolver })
-      router.beforeEach(async (to, from, next) => {
-        if (to.name !== 'Param') return next()
+      router.beforeEach(async (to, from) => {
+        if (to.name !== 'Param') return
         // the first navigation gets passed target
         if (to.params.p === 'a') {
           await p1
-          target ? next(target) : next()
+          return target || undefined
         } else {
           // the second one just passes
-          next()
+          return
         }
       })
       const from = router.currentRoute.value
@@ -521,17 +521,17 @@ describe('Experimental Router', () => {
       await router.push('/p/a')
       await router.push('/p/b')
 
-      router.beforeEach(async (to, from, next) => {
-        if (to.name !== 'Param' && to.name !== 'Foo') return next()
+      router.beforeEach(async (to, from) => {
+        if (to.name !== 'Param' && to.name !== 'Foo') return
         if (to.fullPath === '/foo') {
           await p1
-          next()
+          return
         } else if (from.fullPath === '/p/b') {
           await p2
           // @ts-ignore: same as function above
-          next(target)
+          return target
         } else {
-          next()
+          return
         }
       })
 
@@ -644,7 +644,7 @@ describe('Experimental Router', () => {
     })
   })
 
-  describe('Dynamic Routing', () => {
+  describe.todo('Dynamic Routing', () => {
     it.skip('resolves new added routes', async () => {})
 
     it.skip('checks if a route exists', async () => {})