-
-
Notifications
You must be signed in to change notification settings - Fork 85
/
each.test.ts
182 lines (171 loc) · 5.89 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import faker from 'faker';
import test from 'test';
import * as vest from 'vest';
let testObjects;
describe("Test Vest's `test.each` function", () => {
describe('Sync Tests', () => {
describe('Basic Functionality', () => {
it('Should return all test objects correctly', () => {
vest.create(() => {
testObjects = test.each([
[1, 2, 3],
[2, 7, 9],
])(faker.random.word(), faker.lorem.sentence(), (a, b, c) => {
expect(a + b).toBe(c);
});
})();
expect(testObjects).toHaveLength(2);
expect(testObjects[0].status).not.toBe('FAILED');
expect(testObjects[1].status).not.toBe('FAILED');
});
it('Should mark failed tests as such', () => {
vest.create(() => {
testObjects = test.each([
[5, 5, 10],
[1, 4, 10],
[2, 7, 9],
])(faker.random.word(), faker.lorem.sentence(), (a, b, c) => {
expect(a + b).toBe(c);
});
})();
expect(testObjects).toHaveLength(3);
expect(testObjects[0].status).not.toBe('FAILED');
expect(testObjects[1].status).toBe('FAILED');
expect(testObjects[2].status).not.toBe('FAILED');
});
it('Should include function message when fail test uses function statement', () => {
vest.create(() => {
testObjects = test.each([[5, 4, 10]])(
faker.random.word(),
(a, b, c) => {
return `${a} + ${b} != ${c}`;
},
(a, b, c) => {
expect(a + b).toBe(c);
}
);
})();
expect(testObjects).toHaveLength(1);
expect(testObjects[0].status).toBe('FAILED');
expect(testObjects[0].message).toBe('5 + 4 != 10');
});
it('Should work correctly with exclusions', () => {
const statementFn1 = jest.fn();
const testFn1 = jest.fn();
const testFn2 = jest.fn();
const res = vest.create(() => {
vest.only('test2');
test.each([[5, 4, 10]])('test1', statementFn1, testFn1);
test.each([[5, 4, 10]])('test2', faker.lorem.sentence(), testFn2);
})();
expect(testFn1).not.toHaveBeenCalled(); // skippped field
expect(res.tests.test1.testCount).toBe(0); // skippped field
expect(statementFn1).toHaveBeenCalled();
expect(testFn2).toHaveBeenCalled();
expect(res.tests.test2).toBeDefined();
});
it('Should work correctly with 1d-array', () => {
const res = vest.create(() => {
testObjects = test.each([2, 1, 3])(
faker.random.word(),
faker.lorem.sentence(),
a => {
if (a < 2) {
throw Error();
}
}
);
})();
expect(testObjects).toHaveLength(3);
expect(testObjects[0].status).not.toBe('FAILED');
/* Since fieldName is shared between all result, if one fails we expect all to fail */
expect(res.hasErrors(testObjects[0].fieldName)).toBe(true);
expect(testObjects[1].status).toBe('FAILED');
expect(res.hasErrors(testObjects[1].fieldName)).toBe(true);
expect(testObjects[2].status).not.toBe('FAILED');
expect(res.hasErrors(testObjects[2].fieldName)).toBe(true);
});
it('Should work with fieldName function', () => {
const res = vest.create(() => {
testObjects = test.each([2, 1, 3])(
a => `field${a}`,
faker.lorem.sentence(),
a => {
if (a < 2) {
throw new Error();
}
}
);
})();
expect(testObjects).toHaveLength(3);
expect(testObjects[0].status).not.toBe('FAILED');
expect(testObjects[0].fieldName).toBe('field2');
expect(res.hasErrors(testObjects[0].fieldName)).toBe(false);
expect(testObjects[1].status).toBe('FAILED');
expect(testObjects[1].fieldName).toBe('field1');
expect(res.hasErrors(testObjects[1].fieldName)).toBe(true);
expect(testObjects[2].status).not.toBe('FAILED');
expect(testObjects[2].fieldName).toBe('field3');
expect(res.hasErrors(testObjects[2].fieldName)).toBe(false);
});
});
});
describe('Async Tests', () => {
describe('Basic Functionality', () => {
it('Should finish with no errors', () => {
new Promise<void>(done => {
const validate = vest.create(() => {
test.each([
[1, 2, 3],
[2, 7, 9],
])(
'f1',
faker.lorem.sentence(),
() =>
new Promise<void>(resolve => {
setTimeout(resolve, 100);
})
);
});
validate().done(res => {
expect(res.hasErrors('f1')).toBe(false);
done();
});
});
});
it('Should have errors when finished', () => {
new Promise<void>(done => {
const validate = vest.create(() => {
test.each([
[1, 2, 3],
[2, 1, 9],
])(
'f1',
(a, b, c) => {
return `${a} + ${b} != ${c}`;
},
(a, b, c) =>
new Promise<void>((resolve, reject) => {
setTimeout(() => {
try {
expect(a + b).toBe(c);
} catch (_) {
reject();
}
resolve();
}, 100);
})
);
});
validate().done(res => {
expect(res.hasErrors('f1')).toBe(true);
const errors = res.getErrors('f1');
expect(errors).toHaveLength(1);
expect(errors[0]).toBe('2 + 1 != 9');
done();
});
});
});
});
});
});