Skip to content

Commit 43b559a

Browse files
committed
test: use vitest
1 parent e0c49f4 commit 43b559a

9 files changed

+885
-1998
lines changed

__tests__/Promised.spec.ts

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
1+
// @vitest-environment jsdom
12
/* eslint-disable no-unused-vars */
23
import { mount } from '@vue/test-utils'
3-
import { mockWarn } from 'jest-mock-warn'
44
import { PromisedImpl as Promised } from '../src/Promised'
55
import fakePromise from 'faked-promise'
66
import { h } from 'vue'
7+
import {
8+
describe,
9+
it,
10+
expect,
11+
beforeEach,
12+
afterEach,
13+
afterAll,
14+
vi,
15+
} from 'vitest'
16+
import { mockWarn } from './vitest-mock-warn'
717

818
// keep a real setTimeout
919
const timeout = setTimeout
@@ -95,9 +105,9 @@ describe('Promised', () => {
95105

96106
describe('three slots pendingDelay', () => {
97107
beforeEach(() => {
98-
jest.useFakeTimers('modern')
99-
jest.spyOn(global, 'setTimeout')
100-
jest.spyOn(global, 'clearTimeout')
108+
vi.useFakeTimers()
109+
vi.spyOn(global, 'setTimeout')
110+
vi.spyOn(global, 'clearTimeout')
101111
})
102112
afterEach(() => {
103113
// @ts-expect-error: mocked
@@ -107,13 +117,13 @@ describe('Promised', () => {
107117
})
108118

109119
afterAll(() => {
110-
jest.useRealTimers()
120+
vi.useRealTimers()
111121
})
112122

113123
it('displays nothing before the delay', async () => {
114124
let { wrapper } = factory({ pendingDelay: 300 })
115125
expect(wrapper.text()).toBe('')
116-
jest.runAllTimers()
126+
vi.runAllTimers()
117127
await wrapper.vm.$nextTick()
118128
expect(wrapper.text()).toMatch('pending')
119129
})
@@ -192,7 +202,7 @@ describe('Promised', () => {
192202

193203
mockWarn()
194204

195-
it('warns on missing slot', async () => {
205+
it.only('warns on missing slot', async () => {
196206
const [promise, _resolve, reject] = fakePromise<any>()
197207
mount(Promised, {
198208
propsData: { promise, pendingDelay: 0 },
@@ -206,6 +216,6 @@ describe('Promised', () => {
206216
reject(new Error())
207217
await tick()
208218

209-
expect('Missing slot "rejected"').toHaveBeenWarned()
219+
expect(/Missing slot "rejected"/).toHaveBeenWarned()
210220
})
211221
})

__tests__/__snapshots__/Promised.spec.ts.snap

Lines changed: 0 additions & 9 deletions
This file was deleted.

__tests__/vitest-mock-warn.ts

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
// https://github.com/posva/jest-mock-warn/blob/master/src/index.js
2+
3+
import { afterEach, beforeEach, expect, SpyInstance, vi } from 'vitest'
4+
5+
interface CustomMatchers<R = unknown> {
6+
toHaveBeenWarned(): R
7+
toHaveBeenWarnedLast(): R
8+
toHaveBeenWarnedTimes(n: number): R
9+
}
10+
11+
declare module 'vitest' {
12+
interface Assertion<T = any> extends CustomMatchers<T> {}
13+
interface AsymmetricMatchersContaining extends CustomMatchers {}
14+
}
15+
16+
export function mockWarn() {
17+
expect.extend({
18+
toHaveBeenWarned(received: string | RegExp) {
19+
asserted.set(received.toString(), received)
20+
const passed = warn.mock.calls.some((args) =>
21+
typeof received === 'string'
22+
? args[0].indexOf(received) > -1
23+
: received.test(args[0])
24+
)
25+
if (passed) {
26+
return {
27+
pass: true,
28+
message: () => `expected "${received}" not to have been warned.`,
29+
}
30+
} else {
31+
const msgs = warn.mock.calls.map((args) => args[0]).join('\n - ')
32+
return {
33+
pass: false,
34+
message: () =>
35+
`expected "${received}" to have been warned.\n\nActual messages:\n\n - ${msgs}`,
36+
}
37+
}
38+
},
39+
40+
toHaveBeenWarnedLast(received: string | RegExp) {
41+
asserted.set(received.toString(), received)
42+
const lastCall = warn.mock.calls[warn.mock.calls.length - 1][0]
43+
const passed =
44+
typeof received === 'string'
45+
? lastCall.indexOf(received) > -1
46+
: received.test(lastCall)
47+
if (passed) {
48+
return {
49+
pass: true,
50+
message: () => `expected "${received}" not to have been warned last.`,
51+
}
52+
} else {
53+
const msgs = warn.mock.calls.map((args) => args[0]).join('\n - ')
54+
return {
55+
pass: false,
56+
message: () =>
57+
`expected "${received}" to have been warned last.\n\nActual messages:\n\n - ${msgs}`,
58+
}
59+
}
60+
},
61+
62+
toHaveBeenWarnedTimes(received: string | RegExp, n: number) {
63+
asserted.set(received.toString(), received)
64+
let found = 0
65+
warn.mock.calls.forEach((args) => {
66+
const isFound =
67+
typeof received === 'string'
68+
? args[0].indexOf(received) > -1
69+
: received.test(args[0])
70+
if (isFound) {
71+
found++
72+
}
73+
})
74+
75+
if (found === n) {
76+
return {
77+
pass: true,
78+
message: () =>
79+
`expected "${received}" to have been warned ${n} times.`,
80+
}
81+
} else {
82+
return {
83+
pass: false,
84+
message: () =>
85+
`expected "${received}" to have been warned ${n} times but got ${found}.`,
86+
}
87+
}
88+
},
89+
})
90+
91+
let warn: SpyInstance
92+
const asserted = new Map<string, string | RegExp>()
93+
94+
beforeEach(() => {
95+
asserted.clear()
96+
warn = vi.spyOn(console, 'warn')
97+
warn.mockImplementation(() => {})
98+
})
99+
100+
afterEach(() => {
101+
const assertedArray = Array.from(asserted)
102+
const nonAssertedWarnings = warn.mock.calls
103+
.map((args) => args[0])
104+
.filter((received) => {
105+
return !assertedArray.some(([key, assertedMsg]) => {
106+
return typeof assertedMsg === 'string'
107+
? received.indexOf(assertedMsg) > -1
108+
: assertedMsg.test(received)
109+
})
110+
})
111+
warn.mockRestore()
112+
if (nonAssertedWarnings.length) {
113+
nonAssertedWarnings.forEach((warning) => {
114+
console.warn(warning)
115+
})
116+
throw new Error(`test case threw unexpected warnings.`)
117+
}
118+
})
119+
}
120+
121+
declare global {
122+
namespace Vi {
123+
interface JestAssertion<T = any> {
124+
toHaveBeenWarned(): void
125+
toHaveBeenWarnedLast(): void
126+
toHaveBeenWarnedTimes(n: number): void
127+
}
128+
}
129+
}

package.json

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"lint": "prettier -c --parser typescript \"{src,__tests__,e2e}/**/*.[jt]s?(x)\"",
2525
"lint:fix": "pnpm run lint --write",
2626
"test:types": "tsc --build tsconfig.json",
27-
"test:unit": "jest --coverage",
27+
"test:unit": "vitest --coverage",
2828
"test": "pnpm run lint && pnpm run test:types && pnpm run test:unit && pnpm run build && pnpm run build:dts"
2929
},
3030
"files": [
@@ -49,17 +49,16 @@
4949
"@rollup/plugin-node-resolve": "^15.0.2",
5050
"@rollup/plugin-replace": "^5.0.2",
5151
"@rollup/plugin-terser": "^0.4.3",
52-
"@size-limit/preset-small-lib": "^8.1.0",
53-
"@types/jest": "^27.5.2",
52+
"@size-limit/preset-small-lib": "^8.2.4",
5453
"@types/jsdom": "^21.1.1",
5554
"@vitejs/plugin-vue": "^4.2.3",
55+
"@vitest/coverage-c8": "^0.31.1",
5656
"@vue/compiler-sfc": "^3.3.4",
5757
"@vue/server-renderer": "^3.3.4",
5858
"@vue/test-utils": "^2.3.2",
5959
"conventional-changelog-cli": "^2.2.2",
6060
"faked-promise": "^2.2.2",
61-
"jest": "^27.5.1",
62-
"jest-mock-warn": "^1.1.0",
61+
"jsdom": "^22.0.0",
6362
"lint-staged": "^13.2.2",
6463
"mande": "^2.0.6",
6564
"pascalcase": "^2.0.0",
@@ -68,9 +67,9 @@
6867
"rollup": "^3.23.0",
6968
"rollup-plugin-typescript2": "^0.34.1",
7069
"size-limit": "^8.2.4",
71-
"ts-jest": "^27.1.5",
7270
"typescript": "^5.0.4",
7371
"vite": "^4.0.0",
72+
"vitest": "^0.31.1",
7473
"vue": "^3.3.4",
7574
"vue-prism-component": "^2.0.0",
7675
"yorkie": "^2.0.0"

0 commit comments

Comments
 (0)