--- /dev/null
+import { createDom, noGuard, newRouter as createRouter } from '../utils'
+import { RouteRecordRaw } from '../../src/types'
+
+const Home = { template: `<div>Home</div>` }
+const Foo = { template: `<div>Foo</div>` }
+
+const routes: RouteRecordRaw[] = [
+ { path: '/', component: Home },
+ { path: '/foo', component: Foo },
+]
+
+describe('router.beforeEach', () => {
+ beforeAll(() => {
+ createDom()
+ })
+
+ it('calls beforeEach guards on navigation', async () => {
+ const spy = jest.fn()
+ const router = createRouter({ routes })
+ router.beforeResolve(spy)
+ spy.mockImplementationOnce(noGuard)
+ await router.push('/foo')
+ expect(spy).toHaveBeenCalledTimes(1)
+ })
+})
go(distance: number): void
beforeEach(guard: NavigationGuardWithThis<undefined>): () => void
+ beforeResolve(guard: NavigationGuardWithThis<undefined>): () => void
afterEach(guard: PostNavigationGuard): () => void
onError(handler: ErrorHandler): () => void
const matcher = createRouterMatcher(routes, {})
const beforeGuards = useCallbacks<NavigationGuardWithThis<undefined>>()
+ const beforeResolveGuards = useCallbacks<NavigationGuardWithThis<undefined>>()
const afterGuards = useCallbacks<PostNavigationGuard>()
const currentRoute = ref<RouteLocationNormalizedLoaded>(
START_LOCATION_NORMALIZED
// run the queue of per route beforeEnter guards
await runGuardQueue(guards)
+
+ // check global guards beforeEach
+ guards = []
+ for (const guard of beforeResolveGuards.list()) {
+ guards.push(guardToPromiseFn(guard, to, from))
+ }
+
+ await runGuardQueue(guards)
}
function triggerAfterEach(
forward: () => history.go(1),
beforeEach: beforeGuards.add,
+ beforeResolve: beforeResolveGuards.add,
afterEach: afterGuards.add,
onError: errorHandlers.add,