forked from vitest-dev/vitest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
each.test.ts
85 lines (72 loc) · 1.96 KB
/
each.test.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import { describe, expect, test } from 'vitest'
test.each([
[1, 1, 2],
[1, 2, 3],
[2, 1, 3],
])('add(%i, %i) -> %i', (a, b, expected) => {
expect(a + b).toBe(expected)
})
test.each([
['string', true],
['string', false],
])('can be parsed', (a, b) => {
const typedA: string = a
const typedB: boolean = b
expect(typedA).toBeDefined()
expect(typedB).toBeDefined()
})
describe.each([
[1, 1, 2],
[1, 2, 3],
[2, 1, 3],
])('describe add(%i, %i)', (a, b, expected) => {
test(`returns ${expected}`, () => {
expect(a + b).toBe(expected)
})
test(`returned value not be greater than ${expected}`, () => {
expect(a + b).not.toBeGreaterThan(expected)
})
test(`returned value not be less than ${expected}`, () => {
expect(a + b).not.toBeLessThan(expected)
})
})
describe.each([
[1, 'a', '1a'],
[1, 'b', '1b'],
[2, 'c', '2c'],
] as const)('describe concatenate(%i, %s)', (a, b, expected) => {
test(`returns ${expected}`, () => {
// This will fail typechecking if const is not used and/or types for a,b are merged into a union
const typedA: number = a
const typedB: string = b
expect(`${typedA}${typedB}`).toBe(expected)
})
})
describe.each([
{ a: 1, b: 1, expected: 2 },
{ a: 1, b: 2, expected: 3 },
{ a: 2, b: 1, expected: 3 },
])('describe object add($a, $b)', ({ a, b, expected }) => {
test(`returns ${expected}`, () => {
expect(a + b).toBe(expected)
})
test(`returned value not be greater than ${expected}`, () => {
expect(a + b).not.toBeGreaterThan(expected)
})
test(`returned value not be less than ${expected}`, () => {
expect(a + b).not.toBeLessThan(expected)
})
})
// issue #794
describe.each([1, 2, 0])('%s (describe.each 1d)', (num) => {
test(`${num} is a number (describe.each 1d)`, () => {
expect(typeof num).toEqual('number')
})
})
test.each([
[1, 1, 2],
[1, 2, 3],
[2, 1, 3],
])('the index of the test case is %#', (a, b, expected) => {
expect(a + b).toBe(expected)
})