From 2938bff7622c0c487f15a41f8924788087dd936a Mon Sep 17 00:00:00 2001 From: Eduardo San Martin Morote Date: Thu, 18 Sep 2025 17:16:58 +0200 Subject: [PATCH] test: refactor next usage --- packages/router/__tests__/errors.spec.ts | 21 +++++----- .../__tests__/guards/beforeEach.spec.ts | 24 ++++++------ .../__tests__/guards/beforeEnter.spec.ts | 12 +++--- .../__tests__/guards/beforeRouteEnter.spec.ts | 14 +++---- .../__tests__/guards/beforeRouteLeave.spec.ts | 26 ++++++------- .../guards/beforeRouteUpdate.spec.ts | 4 +- .../guards/extractComponentsGuards.spec.ts | 4 +- .../__tests__/initialNavigation.spec.ts | 4 +- packages/router/__tests__/lazyLoading.spec.ts | 10 ++--- .../router/__tests__/multipleApps.spec.ts | 4 +- packages/router/__tests__/router.spec.ts | 38 +++++++++---------- .../router/src/experimental/router.spec.ts | 22 +++++------ 12 files changed, 93 insertions(+), 90 deletions(-) diff --git a/packages/router/__tests__/errors.spec.ts b/packages/router/__tests__/errors.spec.ts index 5678bf43..3abb1677 100644 --- a/packages/router/__tests__/errors.spec.ts +++ b/packages/router/__tests__/errors.spec.ts @@ -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 ) diff --git a/packages/router/__tests__/guards/beforeEach.spec.ts b/packages/router/__tests__/guards/beforeEach.spec.ts index b4e37360..a421a81e 100644 --- a/packages/router/__tests__/guards/beforeEach.spec.ts +++ b/packages/router/__tests__/guards/beforeEach.spec.ts @@ -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') diff --git a/packages/router/__tests__/guards/beforeEnter.spec.ts b/packages/router/__tests__/guards/beforeEnter.spec.ts index 1df476ee..7c9031f5 100644 --- a/packages/router/__tests__/guards/beforeEnter.spec.ts +++ b/packages/router/__tests__/guards/beforeEnter.spec.ts @@ -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('/') diff --git a/packages/router/__tests__/guards/beforeRouteEnter.spec.ts b/packages/router/__tests__/guards/beforeRouteEnter.spec.ts index 43268f2b..cca03c83 100644 --- a/packages/router/__tests__/guards/beforeRouteEnter.spec.ts +++ b/packages/router/__tests__/guards/beforeRouteEnter.spec.ts @@ -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('/') diff --git a/packages/router/__tests__/guards/beforeRouteLeave.spec.ts b/packages/router/__tests__/guards/beforeRouteLeave.spec.ts index 57dd7815..c92726e4 100644 --- a/packages/router/__tests__/guards/beforeRouteLeave.spec.ts +++ b/packages/router/__tests__/guards/beforeRouteLeave.spec.ts @@ -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('/') diff --git a/packages/router/__tests__/guards/beforeRouteUpdate.spec.ts b/packages/router/__tests__/guards/beforeRouteUpdate.spec.ts index d4cbf98e..3efb1d7c 100644 --- a/packages/router/__tests__/guards/beforeRouteUpdate.spec.ts +++ b/packages/router/__tests__/guards/beforeRouteUpdate.spec.ts @@ -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') diff --git a/packages/router/__tests__/guards/extractComponentsGuards.spec.ts b/packages/router/__tests__/guards/extractComponentsGuards.spec.ts index b10a3224..efe5ba50 100644 --- a/packages/router/__tests__/guards/extractComponentsGuards.spec.ts +++ b/packages/router/__tests__/guards/extractComponentsGuards.spec.ts @@ -40,8 +40,8 @@ const ErrorLazyLoad: RouteRecordRaw = { beforeEach(() => { beforeRouteEnter.mockReset() - beforeRouteEnter.mockImplementation((to, from, next) => { - next() + beforeRouteEnter.mockImplementation((to, from) => { + return }) }) diff --git a/packages/router/__tests__/initialNavigation.spec.ts b/packages/router/__tests__/initialNavigation.spec.ts index c30d7fd3..4e2e6378 100644 --- a/packages/router/__tests__/initialNavigation.spec.ts +++ b/packages/router/__tests__/initialNavigation.spec.ts @@ -19,8 +19,8 @@ const routes: RouteRecordRaw[] = [ { path: '/home-before', component, - beforeEnter: (to, from, next) => { - next('/') + beforeEnter: (to, from) => { + return '/' }, }, { path: '/bar', component }, diff --git a/packages/router/__tests__/lazyLoading.spec.ts b/packages/router/__tests__/lazyLoading.spec.ts index 1a821093..178bad95 100644 --- a/packages/router/__tests__/lazyLoading.spec.ts +++ b/packages/router/__tests__/lazyLoading.spec.ts @@ -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 })) ) diff --git a/packages/router/__tests__/multipleApps.spec.ts b/packages/router/__tests__/multipleApps.spec.ts index 664cd08a..1433c64b 100644 --- a/packages/router/__tests__/multipleApps.spec.ts +++ b/packages/router/__tests__/multipleApps.spec.ts @@ -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) diff --git a/packages/router/__tests__/router.spec.ts b/packages/router/__tests__/router.spec.ts index f835f41e..b46ac5eb 100644 --- a/packages/router/__tests__/router.spec.ts +++ b/packages/router/__tests__/router.spec.ts @@ -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 }, }) diff --git a/packages/router/src/experimental/router.spec.ts b/packages/router/src/experimental/router.spec.ts index 4e179f40..ca6a5ec9 100644 --- a/packages/router/src/experimental/router.spec.ts +++ b/packages/router/src/experimental/router.spec.ts @@ -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 () => {}) -- 2.47.3