forked from vitest-dev/vitest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
each.test.ts
164 lines (137 loc) · 3.68 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import { afterAll, 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)
})
test.each([
[1, 2, 3],
[4, 5, 9],
])('return a promise like result %#', async (a, b, expected) => {
const promiseResolver = (first: number, second: number) => {
return new Promise((resolve) => {
setTimeout(() => resolve(first + second), 1)
})
}
const result = await promiseResolver(a, b)
expect(result).toBe(expected)
})
describe('context on test and describe - todo/skip', () => {
let count = 0
describe.todo.each([1])('todo describe', () => {
test('this is todo test', () => {
count++
})
})
describe.skip.each([1])('todo describe', () => {
test('this is todo test', () => {
count++
})
})
test.skip.each([1])('todo test', () => {
count++
})
afterAll(() => {
expect(count).toBe(0)
})
})
describe('context with each - concurrent', () => {
describe.concurrent.each([[1, 1, 2], [1, 2, 3], [1, 3, 4]])('block', (number1, number2, number3) => {
test('numbered test', ({ expect }) => {
expect(number1 + number2).toBe(number3)
})
})
})
describe('not all arguments are array describe.each', () => {
const results = [null, [null]]
let i = 0
describe.each([null, [null]])('null is null', (value) => {
test('null is null', () => {
expect(value).toEqual(results[i++])
})
})
})
describe('not all arguments are array test.each', () => {
const results = [null, [null]]
let i = 0
test.each([null, [null]])('null is null', (value) => {
expect(value).toEqual(results[i++])
})
})
test.each([
null,
])('value is null', (value) => {
expect(value).toBeNull()
})
test.each([
[null, null],
[null, null],
])('if all cases are arrays of equal length, treats array elements as arguments', (value) => {
expect(value).toBeNull()
})