Skip to content

Commit

Permalink
feat(warn): warn when routes are not found (#279)
Browse files Browse the repository at this point in the history
* test: add test for route not matched

* test: fix tests

* Apply suggestions from code review

Co-authored-by: Eduardo San Martin Morote <posva@users.noreply.github.com>

* chore: code review suggestion

* chore: remove route for '//not-valid' to prevent future confusion

Co-authored-by: Eduardo San Martin Morote <posva@users.noreply.github.com>
  • Loading branch information
pikax and posva committed May 29, 2020
1 parent bcbd8d9 commit d125356
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
21 changes: 17 additions & 4 deletions __tests__/warnings.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ describe('warnings', () => {
history,
routes: [{ path: '/:p', name: 'p', component }],
})
router.resolve({ path: '/', params: { p: 'p' } })
expect('Path "/" was passed with params').toHaveBeenWarned()
router.resolve({ path: '/p', params: { p: 'p' } })
expect('Path "/p" was passed with params').toHaveBeenWarned()
})

it('does not warn when resolving a route with path, params and name', async () => {
Expand Down Expand Up @@ -130,7 +130,7 @@ describe('warnings', () => {
it('should warn if multiple leading slashes with raw location', async () => {
const router = createRouter({
history: createMemoryHistory(),
routes: [{ path: '/foo', component }],
routes: [{ path: '/', component }],
})

await expect(router.push('//not-valid')).resolves.toBe(undefined)
Expand All @@ -140,7 +140,7 @@ describe('warnings', () => {
it('should warn if multiple leading slashes with object location', async () => {
const router = createRouter({
history: createMemoryHistory(),
routes: [{ path: '/foo', component }],
routes: [{ path: '/', component }],
})

await expect(router.push({ path: '//not-valid' })).resolves.toBe(undefined)
Expand Down Expand Up @@ -174,4 +174,17 @@ describe('warnings', () => {
await expect(router.push({ path: '/foo' })).resolves.toBe(undefined)
expect('"/foo" is a Promise instead of a function').toHaveBeenWarned()
})

it('warns if no route matched', async () => {
const router = createRouter({
history: createMemoryHistory(),
routes: [{ path: '/', name: 'a', component }],
})

await expect(router.push('/foo')).resolves.toBe(undefined)
expect(`No match found for location with path "/foo"`).toHaveBeenWarned()

await expect(router.push({ path: '/foo2' })).resolves.toBe(undefined)
expect(`No match found for location with path "/foo2"`).toHaveBeenWarned()
})
})
11 changes: 10 additions & 1 deletion src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,9 @@ export function createRouter(options: RouterOptions): Router {
warn(
`Location "${rawLocation}" resolved to "${href}". A resolved location cannot start with multiple slashes.`
)
else if (!matchedRoute.matched.length) {
warn(`No match found for location with path "${rawLocation}"`)
}
}

return {
Expand Down Expand Up @@ -290,7 +293,6 @@ export function createRouter(options: RouterOptions): Router {
}

let matchedRoute = matcher.resolve(matcherLocation, currentLocation)

const hash = encodeHash(rawLocation.hash || '')

if (__DEV__ && hash && hash[0] !== '#') {
Expand All @@ -317,6 +319,13 @@ export function createRouter(options: RouterOptions): Router {
warn(
`Location "${rawLocation}" resolved to "${href}". A resolved location cannot start with multiple slashes.`
)
else if (!matchedRoute.matched.length) {
warn(
`No match found for location with path "${
'path' in rawLocation ? rawLocation.path : rawLocation
}"`
)
}
}

return {
Expand Down

0 comments on commit d125356

Please sign in to comment.