Skip to content

Commit c8a6f34

Browse files
committed
test: migrate to vitest
1 parent 2510170 commit c8a6f34

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+840
-1891
lines changed

packages/router/__tests__/RouterLink.spec.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* @jest-environment jsdom
2+
* @vitest-environment jsdom
33
*/
44
import { RouterLink } from '../src/RouterLink'
55
import { RouteQueryAndHash, MatcherLocationRaw } from '../src/types'
@@ -16,6 +16,7 @@ import { RouteRecordNormalized } from '../src/matcher/types'
1616
import { routerKey } from '../src/injectionSymbols'
1717
import { tick } from './utils'
1818
import { mount } from '@vue/test-utils'
19+
import { vi, describe, expect, it } from 'vitest'
1920

2021
const records = {
2122
home: {} as RouteRecordNormalized,
@@ -366,9 +367,9 @@ async function factory(
366367
return this.history.base + to.fullPath
367368
},
368369
options: {} as Partial<RouterOptions>,
369-
resolve: jest.fn(),
370-
push: jest.fn().mockResolvedValue(resolvedLocation),
371-
replace: jest.fn().mockResolvedValue(resolvedLocation),
370+
resolve: vi.fn(),
371+
push: vi.fn().mockResolvedValue(resolvedLocation),
372+
replace: vi.fn().mockResolvedValue(resolvedLocation),
372373
}
373374
router.resolve.mockReturnValueOnce(resolvedLocation)
374375

@@ -800,7 +801,7 @@ describe('RouterLink', () => {
800801
})
801802

802803
it('allows adding more click listeners', async () => {
803-
const onClick = jest.fn()
804+
const onClick = vi.fn()
804805
const { router, wrapper } = await factory(
805806
START_LOCATION_NORMALIZED,
806807
{ to: locations.basic.string, onClick },
@@ -944,8 +945,8 @@ describe('RouterLink', () => {
944945
return this.history.base + to.fullPath
945946
},
946947
options: {} as Partial<RouterOptions>,
947-
resolve: jest.fn(),
948-
push: jest.fn().mockResolvedValue(resolvedLocation),
948+
resolve: vi.fn(),
949+
push: vi.fn().mockResolvedValue(resolvedLocation),
949950
}
950951
router.resolve.mockReturnValueOnce(resolvedLocation)
951952

packages/router/__tests__/RouterView.spec.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
/**
2-
* @jest-environment jsdom
2+
* @vitest-environment jsdom
33
*/
44
import { RouterView } from '../src/RouterView'
55
import { components, RouteLocationNormalizedLoose } from './utils'
66
import { START_LOCATION_NORMALIZED } from '../src/location'
77
import { markRaw } from 'vue'
88
import { createMockedRoute } from './mount'
9-
import { mockWarn } from 'jest-mock-warn'
109
import { mount } from '@vue/test-utils'
1110
import { RouteLocationNormalized } from '../src'
11+
import { describe, expect, it } from 'vitest'
12+
import { mockWarn } from './vitest-mock-warn'
1213

1314
// to have autocompletion
1415
function createRoutes<T extends Record<string, RouteLocationNormalizedLoose>>(
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
// Jest Snapshot v1, https://goo.gl/fbAQLP
1+
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
22

3-
exports[`RouterLink v-slot provides information on v-slot 1`] = `"<a aria-current="page" href="/home" class="router-link-active router-link-exact-active"><span> route: {"href":"/home","fullPath":"/home","path":"/home","params":{},"meta":{},"query":{},"hash":"","matched":[{}],"name":"home"} href: "/home" isActive: "true" isExactActive: "true" </span></a>"`;
3+
exports[`RouterLink > v-slot > provides information on v-slot 1`] = `"<a aria-current="page" href="/home" class="router-link-active router-link-exact-active"><span> route: {"href":"/home","fullPath":"/home","path":"/home","params":{},"meta":{},"query":{},"hash":"","matched":[{}],"name":"home"} href: "/home" isActive: "true" isExactActive: "true" </span></a>"`;

packages/router/__tests__/__snapshots__/RouterView.spec.ts.snap

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
// Jest Snapshot v1, https://goo.gl/fbAQLP
1+
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
22

3-
exports[`RouterView displays deeply nested views 1`] = `
3+
exports[`RouterView > displays deeply nested views 1`] = `
44
"<div>
55
<h2>Nested</h2>
66
<div>
@@ -10,19 +10,19 @@ exports[`RouterView displays deeply nested views 1`] = `
1010
</div>"
1111
`;
1212

13-
exports[`RouterView displays nested views 1`] = `
13+
exports[`RouterView > displays nested views 1`] = `
1414
"<div>
1515
<h2>Nested</h2>
1616
<div>Foo</div>
1717
</div>"
1818
`;
1919

20-
exports[`RouterView v-slot passes a Component and route 1`] = `
20+
exports[`RouterView > v-slot > passes a Component and route 1`] = `
2121
"<span>home</span>
2222
<div>Home</div>"
2323
`;
2424

25-
exports[`RouterView warnings does not warn RouterView is wrapped 1`] = `
25+
exports[`RouterView > warnings > does not warn RouterView is wrapped 1`] = `
2626
"<div>
2727
<div>Home</div>
2828
</div>"

packages/router/__tests__/encoding.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
encodeQueryValue,
66
// decode,
77
} from '../src/encoding'
8+
import { describe, expect, it } from 'vitest'
89

910
describe('Encoding', () => {
1011
// all ascii chars with a non ascii char at the beginning

packages/router/__tests__/errors.spec.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ import type {
1313
RouteLocationRaw,
1414
RouteLocationNormalized,
1515
} from '../src/typed-routes'
16-
import { mockWarn } from 'jest-mock-warn'
1716
import { START_LOCATION_NORMALIZED } from '../src/location'
17+
import { vi, describe, expect, it, beforeEach } from 'vitest'
18+
import { mockWarn } from './vitest-mock-warn'
1819

1920
const routes: Readonly<RouteRecordRaw>[] = [
2021
{ path: '/', component: components.Home },
@@ -25,8 +26,8 @@ const routes: Readonly<RouteRecordRaw>[] = [
2526
{ path: '/async', component: () => Promise.reject('failed') },
2627
]
2728

28-
const onError = jest.fn()
29-
const afterEach = jest.fn()
29+
const onError = vi.fn()
30+
const afterEach = vi.fn()
3031
function createRouter() {
3132
const history = createMemoryHistory()
3233
const router = newRouter({

packages/router/__tests__/guards/afterEach.spec.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { createDom, newRouter as createRouter } from '../utils'
2-
import { RouteRecordRaw } from 'src/types'
2+
import { RouteRecordRaw } from '../../src/types'
3+
import { vi, describe, expect, it, beforeAll } from 'vitest'
34

45
const Home = { template: `<div>Home</div>` }
56
const Foo = { template: `<div>Foo</div>` }
@@ -24,7 +25,7 @@ describe('router.afterEach', () => {
2425
})
2526

2627
it('calls afterEach guards on push', async () => {
27-
const spy = jest.fn()
28+
const spy = vi.fn()
2829
const router = createRouter({ routes })
2930
router.afterEach(spy)
3031
await router.push('/foo')
@@ -37,7 +38,7 @@ describe('router.afterEach', () => {
3738
})
3839

3940
it('can be removed', async () => {
40-
const spy = jest.fn()
41+
const spy = vi.fn()
4142
const router = createRouter({ routes })
4243
const remove = router.afterEach(spy)
4344
remove()
@@ -46,7 +47,7 @@ describe('router.afterEach', () => {
4647
})
4748

4849
it('calls afterEach guards on multiple push', async () => {
49-
const spy = jest.fn()
50+
const spy = vi.fn()
5051
const router = createRouter({ routes })
5152
await router.push('/nested')
5253
router.afterEach(spy)
@@ -67,8 +68,8 @@ describe('router.afterEach', () => {
6768
})
6869

6970
it('removing an afterEach guard within one does not affect others', async () => {
70-
const spy1 = jest.fn()
71-
const spy2 = jest.fn()
71+
const spy1 = vi.fn()
72+
const spy2 = vi.fn()
7273
const router = createRouter({ routes })
7374
router.afterEach(spy1)
7475
const remove = router.afterEach(spy2)

packages/router/__tests__/guards/beforeEach.spec.ts

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import fakePromise from 'faked-promise'
22
import { createDom, tick, noGuard, newRouter as createRouter } from '../utils'
33
import { RouteRecordRaw } from '../../src/types'
44
import { RouteLocationRaw } from '../../src'
5+
import { vi, describe, expect, it, beforeAll } from 'vitest'
56

67
const Home = { template: `<div>Home</div>` }
78
const Foo = { template: `<div>Foo</div>` }
@@ -32,7 +33,7 @@ describe('router.beforeEach', () => {
3233
})
3334

3435
it('calls beforeEach guards on navigation', async () => {
35-
const spy = jest.fn()
36+
const spy = vi.fn()
3637
const router = createRouter({ routes })
3738
router.beforeEach(spy)
3839
spy.mockImplementationOnce(noGuard)
@@ -41,7 +42,7 @@ describe('router.beforeEach', () => {
4142
})
4243

4344
it('can be removed', async () => {
44-
const spy = jest.fn()
45+
const spy = vi.fn()
4546
const router = createRouter({ routes })
4647
const remove = router.beforeEach(spy)
4748
remove()
@@ -51,7 +52,7 @@ describe('router.beforeEach', () => {
5152
})
5253

5354
it('does not call beforeEach guard if we were already on the page', async () => {
54-
const spy = jest.fn()
55+
const spy = vi.fn()
5556
const router = createRouter({ routes })
5657
await router.push('/foo')
5758
router.beforeEach(spy)
@@ -61,7 +62,7 @@ describe('router.beforeEach', () => {
6162
})
6263

6364
it('calls beforeEach guards on navigation between children routes', async () => {
64-
const spy = jest.fn()
65+
const spy = vi.fn()
6566
const router = createRouter({ routes })
6667
await router.push('/nested')
6768
router.beforeEach(spy)
@@ -83,7 +84,7 @@ describe('router.beforeEach', () => {
8384
})
8485

8586
it('can redirect to a different location', async () => {
86-
const spy = jest.fn()
87+
const spy = vi.fn()
8788
const router = createRouter({ routes })
8889
await router.push('/foo')
8990
spy.mockImplementation((to, from, next) => {
@@ -125,7 +126,7 @@ describe('router.beforeEach', () => {
125126
return
126127
})
127128

128-
const spy = jest.spyOn(history, 'pushState')
129+
const spy = vi.spyOn(history, 'pushState')
129130
await router.push({ path: '/', state: { a: 'a' } })
130131
expect(spy).toHaveBeenCalledTimes(1)
131132
// called before redirect
@@ -142,7 +143,7 @@ describe('router.beforeEach', () => {
142143
const router = createRouter({ routes })
143144
await router.push('/foo')
144145

145-
const spy = jest.spyOn(history, 'pushState')
146+
const spy = vi.spyOn(history, 'pushState')
146147
await router.push({ path: '/redirect', state: { a: 'a' } })
147148
expect(spy).toHaveBeenCalledTimes(1)
148149
// called before redirect
@@ -156,7 +157,7 @@ describe('router.beforeEach', () => {
156157
})
157158

158159
async function assertRedirect(redirectFn: (i: string) => RouteLocationRaw) {
159-
const spy = jest.fn()
160+
const spy = vi.fn()
160161
const router = createRouter({ routes })
161162
await router.push('/')
162163
spy.mockImplementation((to, from, next) => {
@@ -185,7 +186,7 @@ describe('router.beforeEach', () => {
185186
})
186187

187188
it('is called when changing params', async () => {
188-
const spy = jest.fn()
189+
const spy = vi.fn()
189190
const router = createRouter({ routes: [...routes] })
190191
await router.push('/n/2')
191192
spy.mockImplementation(noGuard)
@@ -196,7 +197,7 @@ describe('router.beforeEach', () => {
196197
})
197198

198199
it('is not called with same params', async () => {
199-
const spy = jest.fn()
200+
const spy = vi.fn()
200201
const router = createRouter({ routes: [...routes] })
201202
await router.push('/n/2')
202203
spy.mockImplementation(noGuard)
@@ -224,15 +225,15 @@ describe('router.beforeEach', () => {
224225
const [p1, r1] = fakePromise()
225226
const [p2, r2] = fakePromise()
226227
const router = createRouter({ routes })
227-
const guard1 = jest.fn()
228+
const guard1 = vi.fn()
228229
let order = 0
229230
guard1.mockImplementationOnce(async (to, from, next) => {
230231
expect(order++).toBe(0)
231232
await p1
232233
next()
233234
})
234235
router.beforeEach(guard1)
235-
const guard2 = jest.fn()
236+
const guard2 = vi.fn()
236237
guard2.mockImplementationOnce(async (to, from, next) => {
237238
expect(order++).toBe(1)
238239
await p2
@@ -256,7 +257,7 @@ describe('router.beforeEach', () => {
256257
})
257258

258259
it('adds meta information', async () => {
259-
const spy = jest.fn()
260+
const spy = vi.fn()
260261
const router = createRouter({ routes })
261262
router.beforeEach(spy)
262263
spy.mockImplementationOnce(noGuard)

packages/router/__tests__/guards/beforeEnter.spec.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
import fakePromise from 'faked-promise'
22
import { createDom, noGuard, tick, newRouter as createRouter } from '../utils'
33
import { RouteRecordRaw } from '../../src/types'
4+
import { vi, describe, expect, it, beforeAll, beforeEach } from 'vitest'
45

56
const Home = { template: `<div>Home</div>` }
67
const Foo = { template: `<div>Foo</div>` }
78

8-
const beforeEnter = jest.fn()
9-
const beforeEnters = [jest.fn(), jest.fn()]
9+
const beforeEnter = vi.fn()
10+
const beforeEnters = [vi.fn(), vi.fn()]
1011
const nested = {
11-
parent: jest.fn(),
12-
nestedEmpty: jest.fn(),
13-
nestedA: jest.fn(),
14-
nestedAbs: jest.fn(),
15-
nestedNested: jest.fn(),
16-
nestedNestedFoo: jest.fn(),
17-
nestedNestedParam: jest.fn(),
12+
parent: vi.fn(),
13+
nestedEmpty: vi.fn(),
14+
nestedA: vi.fn(),
15+
nestedAbs: vi.fn(),
16+
nestedNested: vi.fn(),
17+
nestedNestedFoo: vi.fn(),
18+
nestedNestedParam: vi.fn(),
1819
}
1920

2021
const routes: RouteRecordRaw[] = [

packages/router/__tests__/guards/beforeResolve.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { createDom, noGuard, newRouter as createRouter } from '../utils'
22
import { RouteRecordRaw } from '../../src/types'
3+
import { vi, describe, expect, it, beforeAll } from 'vitest'
34

45
const Home = { template: `<div>Home</div>` }
56
const Foo = { template: `<div>Foo</div>` }
@@ -15,7 +16,7 @@ describe('router.beforeEach', () => {
1516
})
1617

1718
it('calls beforeEach guards on navigation', async () => {
18-
const spy = jest.fn()
19+
const spy = vi.fn()
1920
const router = createRouter({ routes })
2021
router.beforeResolve(spy)
2122
spy.mockImplementationOnce(noGuard)

0 commit comments

Comments
 (0)