-
Notifications
You must be signed in to change notification settings - Fork 1
/
vitest.setup.ts
37 lines (28 loc) · 980 Bytes
/
vitest.setup.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import kindOf from 'kind-of'
import { expect, it } from 'vitest'
global.buildChallengeTestCases = <Args, Expected>({
cases,
fn,
spreadFn,
}: BuildChallengeTestOptions<Args, Expected>) => {
const expectedTypeof = kindOf(cases[0].expected)
const name = fn?.name ?? spreadFn?.name
const executor = (args: Args) => {
if (fn) return fn(args)
if (spreadFn) return spreadFn(...(args as any))
throw Error('buildChallengeTest requires an executor fn')
}
it(`#T should return ${expectedTypeof} type`, () => {
const result = executor(cases[0].args)
expect(kindOf(result)).toBe(expectedTypeof)
})
const nameTemplate = spreadFn
? `#%# ${name}(${(cases[0].args as any[])
.map((_, i) => '$args.' + i)
.join(', ')}) should return $expected`
: `#%# ${name}($args) should return $expected`
it.each(cases)(nameTemplate, ({ args, expected }) => {
const result = executor(args)
expect(result).toEqual(expected)
})
}