Skip to content

Commit 9acef52

Browse files
committed
fix(errors): throws with invalid route objects
Closes #1892 Throws for missing component, components or redirect and also when components is empty
1 parent 07d0298 commit 9acef52

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

Diff for: src/create-route-map.js

+4
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ function addRouteRecord (
5252
const { path, name } = route
5353
if (process.env.NODE_ENV !== 'production') {
5454
assert(path != null, `"path" is required in a route configuration.`)
55+
assert(route.component != null || route.redirect != null || route.components != null, `One of the following options "component", "components" or "redirect" is required for route "${path}"`)
56+
if (route.components) {
57+
assert(route.components.length, `"components" array cannot be empty for route "${path}"`)
58+
}
5559
assert(
5660
typeof route.component !== 'string',
5761
`route config "component" for path: ${String(path || name)} cannot be a ` +

Diff for: test/unit/specs/create-map.spec.js

+42
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,48 @@ describe('Creating Route Map', function () {
7171
expect(console.warn.calls.argsFor(0)[0]).toMatch('vue-router] Named Route \'bar\'')
7272
})
7373

74+
it('in development, throws if path is missing', function () {
75+
process.env.NODE_ENV = 'development'
76+
expect(() => {
77+
maps = createRouteMap([{ component: Bar }])
78+
}).toThrowError(/"path" is required/)
79+
})
80+
81+
it('in development, throws if component, components or redirect is missing', function () {
82+
process.env.NODE_ENV = 'development'
83+
expect(() => {
84+
maps = createRouteMap([{ path: '/' }])
85+
}).toThrowError(/"component", "components" or "redirect" is required/)
86+
})
87+
88+
it('in development, does not throw if component is provided', function () {
89+
process.env.NODE_ENV = 'development'
90+
expect(() => {
91+
maps = createRouteMap([{ path: '/', component: Bar }])
92+
}).not.toThrow()
93+
})
94+
95+
it('in development, does not throw if components is provided', function () {
96+
process.env.NODE_ENV = 'development'
97+
expect(() => {
98+
maps = createRouteMap([{ path: '/', components: [{ component: Bar, path: '' }] }])
99+
}).not.toThrow()
100+
})
101+
102+
it('in development, does not throw if redirect is provided', function () {
103+
process.env.NODE_ENV = 'development'
104+
expect(() => {
105+
maps = createRouteMap([{ path: '/', redirect: '/home' }])
106+
}).not.toThrow()
107+
})
108+
109+
it('in development, throws if components is empty', function () {
110+
process.env.NODE_ENV = 'development'
111+
expect(() => {
112+
maps = createRouteMap([{ path: '/', components: [] }])
113+
}).toThrowError(/"components" array cannot be empty/)
114+
})
115+
74116
it('in production, it has not logged this warning', function () {
75117
maps = createRouteMap(routes)
76118
expect(console.warn).not.toHaveBeenCalled()

0 commit comments

Comments
 (0)