import { RouteComponent } from '../src/types'
import { ticks } from './utils'
import { FunctionalComponent, h } from 'vue'
+import { mockWarn } from 'jest-mock-warn'
function newRouter(options: Partial<RouterOptions> = {}) {
let history = createMemoryHistory()
}
describe('Lazy Loading', () => {
+ mockWarn()
it('works', async () => {
const { component, resolve } = createLazyComponent()
const { router } = newRouter({
expect(spy).toHaveBeenCalledTimes(1)
})
+ it('prints the error when lazy load fails', async () => {
+ const { component, reject } = createLazyComponent()
+ const { router } = newRouter({
+ routes: [{ path: '/foo', component }],
+ })
+
+ const spy = jest.fn()
+
+ reject(new Error('fail'))
+ await router.push('/foo').catch(spy)
+
+ expect(spy).toHaveBeenCalled()
+ expect('fail').toHaveBeenWarned()
+
+ expect(router.currentRoute.value).toMatchObject({
+ path: '/',
+ matched: [],
+ })
+ })
+
it('aborts the navigation if async fails', async () => {
const { component, reject } = createLazyComponent()
const { router } = newRouter({
guard && guards.push(guardToPromiseFn(guard, to, from, record, name))
} else {
// start requesting the chunk already
- let componentPromise: Promise<RouteComponent | null> = (rawComponent as Lazy<
- RouteComponent
- >)()
+ let componentPromise: Promise<
+ RouteComponent | null | undefined | void
+ > = (rawComponent as Lazy<RouteComponent>)()
if (__DEV__ && !('catch' in componentPromise)) {
warn(
)
componentPromise = Promise.resolve(componentPromise as RouteComponent)
} else {
- componentPromise = componentPromise.catch(() => null)
+ // display the error if any
+ componentPromise = componentPromise.catch(
+ __DEV__ ? err => err && warn(err) : console.error
+ )
}
guards.push(() =>
if (!resolved)
return Promise.reject(
new Error(
- `Couldn't resolve component "${name}" for the following record with path "${record.path}"`
+ `Couldn't resolve component "${name}" at "${record.path}"`
)
)
const resolvedComponent = isESModule(resolved)