Skip to content

Commit f7f78f0

Browse files
committed
test: migrate type tests from uvr
1 parent c8a6f34 commit f7f78f0

File tree

12 files changed

+340
-123
lines changed

12 files changed

+340
-123
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,9 @@ jobs:
3636
- run: pnpm install
3737
- run: pnpm run lint
3838
- run: pnpm run -r test:types
39-
- run: pnpm run -r test:unit
4039
- run: pnpm run -r build
4140
- run: pnpm run -r build:dts
42-
- run: pnpm run -r test:dts
41+
- run: pnpm run -r test:unit
4342

4443
# e2e tests that that run locally
4544
- run: pnpm run -r test:e2e:ci
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { describe, it } from 'vitest'
2+
import { createRouter, createWebHistory } from '../src'
3+
import { defineComponent, h } from 'vue'
4+
5+
describe('createRouter', () => {
6+
const component = defineComponent({})
7+
8+
const WithProps = defineComponent({
9+
props: {
10+
id: {
11+
type: String,
12+
required: true,
13+
},
14+
},
15+
})
16+
17+
const Foo = defineComponent({
18+
props: {
19+
test: String,
20+
},
21+
setup() {
22+
return {
23+
title: 'homepage',
24+
}
25+
},
26+
render() {
27+
return h('div', `${this.title}: ${this.test}`)
28+
},
29+
})
30+
31+
it('works', () => {
32+
createRouter({
33+
history: createWebHistory(),
34+
routes: [
35+
{ path: '/', component },
36+
{ path: '/foo', component: Foo },
37+
{ path: '/', component: WithProps },
38+
],
39+
parseQuery: search => ({}),
40+
stringifyQuery: query => '',
41+
strict: true,
42+
end: true,
43+
sensitive: true,
44+
scrollBehavior(to, from, savedPosition) {},
45+
})
46+
})
47+
})
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import { describe, it, expectTypeOf } from 'vitest'
2+
import type {
3+
RouteRecordName,
4+
ParamValue,
5+
ParamValueZeroOrMore,
6+
RouteRecordInfo,
7+
RouteLocationNormalizedTypedList,
8+
} from '../src'
9+
10+
// TODO: could we move this to an .d.ts file that is only loaded for tests?
11+
// https://github.com/microsoft/TypeScript/issues/15300
12+
type RouteNamedMap = {
13+
home: RouteRecordInfo<'/', '/', Record<never, never>, Record<never, never>>
14+
'/[other]': RouteRecordInfo<
15+
'/[other]',
16+
'/:other',
17+
{ other: ParamValue<true> },
18+
{ other: ParamValue<false> }
19+
>
20+
'/[name]': RouteRecordInfo<
21+
'/[name]',
22+
'/:name',
23+
{ name: ParamValue<true> },
24+
{ name: ParamValue<false> }
25+
>
26+
'/[...path]': RouteRecordInfo<
27+
'/[...path]',
28+
'/:path(.*)',
29+
{ path: ParamValue<true> },
30+
{ path: ParamValue<false> }
31+
>
32+
'/deep/nesting/works/[[files]]+': RouteRecordInfo<
33+
'/deep/nesting/works/[[files]]+',
34+
'/deep/nesting/works/:files*',
35+
{ files?: ParamValueZeroOrMore<true> },
36+
{ files?: ParamValueZeroOrMore<false> }
37+
>
38+
}
39+
40+
describe('Route Location types', () => {
41+
it('RouteLocationNormalized', () => {
42+
function withRoute(
43+
fn: (
44+
to: RouteLocationNormalizedTypedList<RouteNamedMap>[keyof RouteNamedMap]
45+
) => void
46+
): void
47+
function withRoute<Name extends keyof RouteNamedMap>(
48+
name: Name,
49+
fn: (to: RouteLocationNormalizedTypedList<RouteNamedMap>[Name]) => void
50+
): void
51+
function withRoute<Name extends RouteRecordName>(...args: unknown[]) {}
52+
53+
withRoute('/[name]', to => {
54+
expectTypeOf(to.params).toEqualTypeOf<{ name: string }>()
55+
expectTypeOf(to.params).not.toEqualTypeOf<{ notExisting: string }>()
56+
expectTypeOf(to.params).not.toEqualTypeOf<{ other: string }>()
57+
})
58+
59+
withRoute('/[name]' as keyof RouteNamedMap, to => {
60+
// @ts-expect-error: no all params have this
61+
to.params.name
62+
if (to.name === '/[name]') {
63+
to.params.name
64+
// @ts-expect-error: no param other
65+
to.params.other
66+
}
67+
})
68+
69+
withRoute(to => {
70+
// @ts-expect-error: not all params object have a name
71+
to.params.name
72+
// @ts-expect-error: no route named like that
73+
if (to.name === '') {
74+
}
75+
if (to.name === '/[name]') {
76+
expectTypeOf(to.params).toEqualTypeOf<{ name: string }>()
77+
// @ts-expect-error: no param other
78+
to.params.other
79+
}
80+
})
81+
})
82+
})

packages/router/package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,8 @@
9797
"build:size": "pnpm run build && rollup -c size-checks/rollup.config.mjs",
9898
"dev:e2e": "vite --config e2e/vite.config.mjs",
9999
"test:types": "tsc --build tsconfig.json",
100-
"test:dts": "tsc -p ./test-dts/tsconfig.json",
101-
"test:unit": "vitest --coverage",
102-
"test": "pnpm run test:types && pnpm run test:unit && pnpm run build && pnpm run build:dts && pnpm run test:e2e",
100+
"test:unit": "vitest --coverage run",
101+
"test": "pnpm run test:types && pnpm run build && pnpm run build:dts && pnpm run test:unit && pnpm run test:e2e",
103102
"test:e2e": "pnpm run test:e2e:headless",
104103
"test:e2e:headless": "node e2e/runner.mjs --env chrome-headless",
105104
"test:e2e:native": "node e2e/runner.mjs --env chrome",

packages/router/test-dts/components.test-d.tsx

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@ import {
44
RouterView,
55
createRouter,
66
createMemoryHistory,
7-
expectError,
8-
expectType,
97
} from './index'
8+
import { expectTypeOf } from 'vitest'
109

1110
let router = createRouter({
1211
history: createMemoryHistory(),
@@ -20,13 +19,13 @@ expectError(<RouterLink />)
2019
expectError(<RouterLink to="/" custom="text" />)
2120
// @ts-expect-error: invalid prop
2221
expectError(<RouterLink to="/" replace="text" />)
23-
expectType<JSX.Element>(<RouterLink to="/foo" replace />)
24-
expectType<JSX.Element>(<RouterLink to="/foo" />)
25-
expectType<JSX.Element>(<RouterLink class="link" to="/foo" />)
26-
expectType<JSX.Element>(<RouterLink to={{ path: '/foo' }} />)
27-
expectType<JSX.Element>(<RouterLink to={{ path: '/foo' }} custom />)
22+
expectTypeOf<JSX.Element>(<RouterLink to="/foo" replace />)
23+
expectTypeOf<JSX.Element>(<RouterLink to="/foo" />)
24+
expectTypeOf<JSX.Element>(<RouterLink class="link" to="/foo" />)
25+
expectTypeOf<JSX.Element>(<RouterLink to={{ path: '/foo' }} />)
26+
expectTypeOf<JSX.Element>(<RouterLink to={{ path: '/foo' }} custom />)
2827

2928
// RouterView
30-
expectType<JSX.Element>(<RouterView class="view" />)
31-
expectType<JSX.Element>(<RouterView name="foo" />)
32-
expectType<JSX.Element>(<RouterView route={router.currentRoute.value} />)
29+
expectTypeOf<JSX.Element>(<RouterView class="view" />)
30+
expectTypeOf<JSX.Element>(<RouterView name="foo" />)
31+
expectTypeOf<JSX.Element>(<RouterView route={router.currentRoute.value} />)

packages/router/test-dts/createRouter.test-d.ts

Lines changed: 0 additions & 61 deletions
This file was deleted.
Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,2 @@
11
export * from '../dist/vue-router'
22
// export * from '../src'
3-
4-
export function describe(_name: string, _fn: () => void): void
5-
export function expectType<T>(value: T): void
6-
export function expectError<T>(value: T): void
7-
export function expectAssignable<T, T2 extends T = T>(value: T2): void
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
import { Router, RouteLocationNormalizedLoaded, expectType } from './index'
1+
import { expectTypeOf } from 'vitest'
2+
import { Router, RouteLocationNormalizedLoaded } from './index'
23
import { defineComponent } from 'vue'
34

45
defineComponent({
56
methods: {
67
doStuff() {
7-
expectType<Router>(this.$router)
8-
expectType<RouteLocationNormalizedLoaded>(this.$route)
8+
expectTypeOf<Router>(this.$router)
9+
expectTypeOf<RouteLocationNormalizedLoaded>(this.$route)
910
},
1011
},
1112
})

packages/router/test-dts/meta.test-d.ts

Lines changed: 38 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import { createRouter, createWebHistory, expectType } from './index'
2-
import { createApp, defineComponent } from 'vue'
1+
import { createRouter, createWebHistory } from './index'
2+
import { defineComponent } from 'vue'
3+
import { describe, it, expectTypeOf } from 'vitest'
34

45
const component = defineComponent({})
56

@@ -10,34 +11,41 @@ declare module './index' {
1011
}
1112
}
1213

13-
const router = createRouter({
14-
history: createWebHistory(),
15-
routes: [
16-
{
17-
path: '/',
18-
component,
19-
meta: {
20-
requiresAuth: true,
21-
lol: true,
22-
nested: {
23-
foo: 'bar',
14+
describe('RouteMeta', () => {
15+
it('route creation', () => {
16+
const router = createRouter({
17+
history: createWebHistory(),
18+
routes: [
19+
{
20+
path: '/',
21+
component,
22+
meta: {
23+
requiresAuth: true,
24+
lol: true,
25+
nested: {
26+
foo: 'bar',
27+
},
28+
},
2429
},
25-
},
26-
},
27-
{
28-
path: '/foo',
29-
component,
30-
// @ts-expect-error
31-
meta: {},
32-
},
33-
],
34-
})
30+
{
31+
path: '/foo',
32+
component,
33+
// @ts-expect-error
34+
meta: {},
35+
},
36+
],
37+
})
38+
})
3539

36-
router.beforeEach(to => {
37-
expectType<{ requiresAuth?: Boolean; nested: { foo: string } }>(to.meta)
38-
expectType<unknown>(to.meta.lol)
39-
if (to.meta.nested.foo == 'foo' || to.meta.lol) return false
40+
it('route location in guards', () => {
41+
const router = createRouter({
42+
history: createWebHistory(),
43+
routes: [],
44+
})
45+
router.beforeEach(to => {
46+
expectTypeOf<{ requiresAuth?: Boolean; nested: { foo: string } }>(to.meta)
47+
expectTypeOf<unknown>(to.meta.lol)
48+
if (to.meta.nested.foo == 'foo' || to.meta.lol) return false
49+
})
50+
})
4051
})
41-
42-
const app = createApp({})
43-
app.use(router)

packages/router/test-dts/navigationGuards.test-d.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1+
import { expectTypeOf } from 'vitest'
12
import {
23
createRouter,
34
createWebHistory,
4-
expectType,
55
isNavigationFailure,
66
NavigationFailure,
77
NavigationFailureType,
@@ -45,13 +45,13 @@ router.beforeEach((to, from, next) => {
4545
})
4646

4747
router.afterEach((to, from, failure) => {
48-
expectType<NavigationFailure | undefined | void>(failure)
48+
expectTypeOf<NavigationFailure | undefined | void>(failure)
4949
if (isNavigationFailure(failure)) {
50-
expectType<RouteLocationNormalized>(failure.from)
51-
expectType<RouteLocationRaw>(failure.to)
50+
expectTypeOf<RouteLocationNormalized>(failure.from)
51+
expectTypeOf<RouteLocationRaw>(failure.to)
5252
}
5353
if (isNavigationFailure(failure, NavigationFailureType.cancelled)) {
54-
expectType<RouteLocationNormalized>(failure.from)
55-
expectType<RouteLocationRaw>(failure.to)
54+
expectTypeOf<RouteLocationNormalized>(failure.from)
55+
expectTypeOf<RouteLocationRaw>(failure.to)
5656
}
5757
})

0 commit comments

Comments
 (0)