-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathtest-try-commit.js
512 lines (426 loc) · 12.4 KB
/
test-try-commit.js
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
import {setTimeout as delay} from 'node:timers/promises';
import {test} from 'tap';
import ContextRef from '../lib/context-ref.js';
import {set as setOptions} from '../lib/worker/options.cjs';
import {newAva} from './helper/ava-test.js';
setOptions({chalkOptions: {level: 0}});
test('try-commit works', async t => {
const ava = newAva();
const instance = ava(async a => {
const result = await a.try(b => b.pass());
t.ok(result.passed);
result.commit();
});
const result = await instance.run();
t.ok(result.passed);
t.equal(instance.assertCount, 1);
});
test('try-commit is bound', async t => {
const ava = newAva();
const result = await ava(async a => {
const {try: tryFn} = a;
const result = await tryFn(b => b.pass());
await result.commit();
}).run();
t.ok(result.passed);
});
test('try-commit discards failed attempt', async t => {
const ava = newAva();
const result = await ava(async a => {
const result = await a.try(b => b.fail());
await result.discard();
await a.pass();
}).run();
t.ok(result.passed);
});
test('try-commit can discard produced result', async t => {
const ava = newAva();
const result = await ava(async a => {
const result = await a.try(b => b.pass());
result.discard();
}).run();
t.notOk(result.passed);
t.ok(result.error);
t.match(result.error.message, /without running any assertions/);
t.equal(result.error.name, 'Error');
});
test('try-commit fails when not all assertions were committed/discarded', async t => {
const ava = newAva();
const result = await ava(async a => {
a.pass();
await a.try(b => b.pass());
}).run();
t.notOk(result.passed);
t.ok(result.error);
t.match(result.error.message, /not all attempts were committed/);
t.equal(result.error.name, 'Error');
});
test('try-commit works with values', async t => {
const testValue1 = 123;
const testValue2 = 123;
const ava = newAva();
const result = await ava(async a => {
const result = await a.try((b, value1, value2) => {
b.is(value1, value2);
}, testValue1, testValue2);
t.ok(result.passed);
result.commit();
}).run();
t.ok(result.passed);
});
test('try-commit is properly counted', async t => {
const ava = newAva();
const instance = ava(async a => {
const result = await a.try(b => {
b.is(1, 1);
b.is(2, 2);
b.pass();
});
t.ok(result.passed);
t.equal(instance.pendingAttemptCount, 1);
result.commit();
t.equal(instance.pendingAttemptCount, 0);
});
const result = await instance.run();
t.ok(result.passed);
t.equal(instance.assertCount, 1);
});
test('try-commit is properly counted multiple', async t => {
const ava = newAva();
const instance = ava(async a => {
const [result1, result2, result3] = await Promise.all([
a.try(b => b.pass()),
a.try(b => b.pass()),
a.try(b => b.pass()),
]);
t.equal(instance.pendingAttemptCount, 3);
result1.commit();
result2.discard();
result3.commit();
t.equal(instance.pendingAttemptCount, 0);
});
const result = await instance.run();
t.ok(result.passed);
t.equal(instance.assertCount, 2);
});
test('try-commit goes as many levels', async t => {
t.plan(5);
const ava = newAva();
const instance = ava(async a => {
t.ok(a.try);
const result = await a.try(async b => {
t.ok(b.try);
const result = await b.try(c => {
t.ok(c.try);
c.pass();
});
result.commit();
});
result.commit();
});
const result = await instance.run();
t.ok(result.passed);
t.equal(instance.assertCount, 1);
});
test('try-commit fails when not committed', async t => {
const ava = newAva();
const result = await ava(async a => {
const result = await a.try(b => b.pass());
t.ok(result.passed);
}).run();
t.notOk(result.passed);
t.ok(result.error);
t.match(result.error.message, /not all attempts were committed/);
t.equal(result.error.name, 'Error');
});
test('try-commit fails when no assertions inside try', async t => {
const ava = newAva();
const result = await ava(async a => {
const result = await a.try(() => {});
t.notOk(result.passed);
t.ok(result.errors);
t.equal(result.errors.length, 1);
const error = result.errors[0];
t.match(error.message, /Test finished without running any assertions/);
t.equal(error.name, 'Error');
result.commit();
}).run();
t.notOk(result.passed);
});
test('try-commit fails when no assertions inside multiple try', async t => {
const ava = newAva();
const result = await ava(async a => {
const [result1, result2] = await Promise.all([
a.try(b => b.pass()),
a.try(() => {}),
]);
result1.commit();
t.ok(result1.passed);
t.notOk(result2.passed);
t.ok(result2.errors);
t.equal(result2.errors.length, 1);
const error = result2.errors[0];
t.match(error.message, /Test finished without running any assertions/);
t.equal(error.name, 'Error');
result2.commit();
}).run();
t.notOk(result.passed);
});
test('test fails when try-commit committed to failed state', async t => {
const ava = newAva();
const result = await ava(async a => {
const result = await a.try(b => b.fail());
t.notOk(result.passed);
result.commit();
}).run();
t.notOk(result.passed);
});
test('try-commit has proper titles, when going in depth and width', async t => {
t.plan(6);
const ava = newAva();
await ava(async a => {
t.equal(a.title, 'test');
await Promise.all([
a.try(async b => {
t.equal(b.title, 'test ─ attempt 1');
await Promise.all([
b.try(c => t.equal(c.title, 'test ─ attempt 1 ─ attempt 1')),
b.try(c => t.equal(c.title, 'test ─ attempt 1 ─ attempt 2')),
]);
}),
a.try(b => t.equal(b.title, 'test ─ attempt 2')),
a.try(b => t.equal(b.title, 'test ─ attempt 3')),
]);
}).run();
});
test('try-commit does not fail when calling commit twice', async t => {
const ava = newAva();
const result = await ava(async a => {
const result = await a.try(b => b.pass());
result.commit();
result.commit();
}).run();
t.ok(result.passed);
t.notOk(result.error);
});
test('try-commit does not fail when calling discard twice', async t => {
const ava = newAva();
const result = await ava(async a => {
const result = await a.try(b => b.pass());
result.discard();
result.discard();
}).run();
t.notOk(result.passed);
t.ok(result.error);
t.match(result.error.message, /Test finished without running any assertions/);
t.equal(result.error.name, 'Error');
});
test('try-commit allows planning inside the try', async t => {
const ava = newAva();
const result = await ava(async a => {
const result = await a.try(b => {
b.plan(3);
b.pass();
b.pass();
b.pass();
});
t.ok(result.passed);
result.commit();
}).run();
t.ok(result.passed);
});
test('try-commit fails when plan is not reached inside the try', async t => {
const ava = newAva();
const result = await ava(async a => {
const result = await a.try(b => {
b.plan(3);
b.pass();
b.pass();
});
t.notOk(result.passed);
result.commit();
}).run();
t.notOk(result.passed);
});
test('plan within try-commit is not affected by assertions outside', async t => {
const ava = newAva();
const result = await ava(async a => {
a.is(1, 1);
a.is(2, 2);
const attempt = a.try(b => {
b.plan(3);
});
const result = await attempt;
t.notOk(result.passed);
result.commit();
}).run();
t.notOk(result.passed);
t.ok(result.error);
t.match(result.error.message, /Planned for 3 assertions, but got 0/);
});
test('assertions within try-commit do not affect plan in the parent test', async t => {
const ava = newAva();
const result = await ava(async a => {
a.plan(2);
const result = await a.try(b => {
b.plan(3);
b.pass();
b.pass();
b.pass();
});
t.ok(result.passed);
result.commit();
}).run();
t.notOk(result.passed);
t.ok(result.error);
t.match(result.error.message, /Planned for 2 assertions, but got 1/);
});
test('test expected to fail will pass with failing try-commit within the test', async t => {
const ava = newAva();
const result = await ava.failing(async a => {
const result = await a.try(b => b.fail());
t.notOk(result.passed);
t.ok(result.errors);
t.equal(result.errors.length, 1);
const error = result.errors[0];
t.match(error.message, /Test failed via `t\.fail\(\)`/);
t.equal(error.name, 'AssertionError');
result.commit();
}).run();
t.ok(result.passed);
});
test('try-commit accepts macros', async t => {
const macro = b => {
t.equal(b.title, 'test ─ Title');
b.pass();
};
macro.title = (providedTitle = '') => `${providedTitle} Title`.trim();
const ava = newAva();
const result = await ava(async a => {
const result = await a.try(macro);
t.ok(result.passed);
result.commit();
}).run();
t.ok(result.passed);
});
test('try-commit abides timeout', async t => {
const ava = newAva();
const result1 = await ava(async a => {
a.timeout(10);
const result = await a.try(async b => {
b.pass();
await delay(200);
});
await result.commit();
}).run();
t.equal(result1.passed, false);
t.match(result1.error.message, /timeout/);
});
test('try-commit fails when it exceeds its own timeout', async t => {
const ava = newAva();
const result = await ava(async a => {
a.timeout(200);
const result = await a.try(async b => {
b.timeout(50);
b.pass();
await delay(100);
});
t.notOk(result.passed);
t.ok(result.errors);
t.equal(result.errors.length, 1);
const error = result.errors[0];
t.match(error.message, /Test timeout exceeded/);
t.equal(error.name, 'Error');
result.discard();
a.pass();
}).run();
t.ok(result.passed);
});
test('try-commit refreshes the timeout on commit/discard', async t => {
const ava = newAva();
const result1 = await ava(async a => {
// Note: Allow for long enough timeouts that the promise tasks execute in time.
a.timeout(2e3);
a.plan(3);
await Promise.all([
delay(1e3).then(() => a.try(b => b.pass())).then(result => result.commit()),
delay(2e3).then(() => a.try(b => b.pass())).then(result => result.commit()),
delay(3e3).then(() => a.try(b => b.pass())).then(result => result.commit()),
delay(4e3),
]);
}).run();
t.equal(result1.passed, true);
});
test('assertions within try-commit do not refresh the timeout', async t => {
const ava = newAva();
const result = await ava(async a => {
a.timeout(15);
a.pass();
// Attempt by itself will refresh timeout, so it has to finish after
// timeout of the test in order to make sure that it does not refresh the
// timeout. However, if assert within attempt is called before test timeout
// expires and will refresh the timeout (which is faulty behavior), then
// the entire test will not fail by timeout.
const result = await a.try(async b => {
await delay(10);
b.is(1, 1);
await delay(10);
});
result.commit();
}).run();
t.notOk(result.passed);
t.ok(result.error);
t.match(result.error.message, /Test timeout exceeded/);
t.equal(result.error.name, 'Error');
});
test('try-commit inherits the test context', async t => {
const context = new ContextRef();
const data = {foo: 'bar'};
context.set(data);
const ava = newAva();
const result = await ava(async a => {
const result = await a.try(b => {
b.pass();
t.strictSame(b.context, data);
});
await result.commit();
}, context).run();
t.equal(result.passed, true);
});
test('assigning context in try-commit does not affect parent', async t => {
const context = new ContextRef();
const data = {foo: 'bar'};
context.set(data);
const ava = newAva();
const result = await ava(async a => {
t.strictSame(a.context, data);
const result = await a.try(b => {
b.pass();
b.context = {bar: 'foo'};
});
result.commit();
t.strictSame(a.context, data);
}, context).run();
t.equal(result.passed, true);
});
test('do not run assertions outside of an active attempt', async t => {
const ava = newAva();
const passing = await ava(async a => {
await a.try(() => {});
a.pass();
}, undefined, 'passing').run();
t.notOk(passing.passed);
t.match(passing.error.message, /Assertion passed, but an attempt is pending. Use the attempt’s assertions instead/);
const pending = await ava(async a => {
await a.try(() => {});
await a.throwsAsync(Promise.reject(new Error('')));
}, undefined, 'pending').run();
t.notOk(pending.passed);
t.match(pending.error.message, /Assertion started, but an attempt is pending. Use the attempt’s assertions instead/);
const failing = await ava(async a => {
await a.try(() => {});
a.fail();
}, undefined, 'failing').run();
t.notOk(failing.passed);
t.match(failing.error.message, /Assertion failed, but an attempt is pending. Use the attempt’s assertions instead/);
});