/
test-try-commit.js
589 lines (488 loc) · 13.8 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
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
'use strict';
require('../lib/chalk').set();
require('../lib/worker/options').set({chalkOptions: {level: 0}});
const {test} = require('tap');
const delay = require('delay');
const ContextRef = require('../lib/context-ref');
const {withExperiments} = require('./helper/ava-test');
const ava = withExperiments({tryAssertion: true});
test('try-commit works', async t => {
const instance = ava(async a => {
const res = await a.try(b => b.pass());
t.true(res.passed);
res.commit();
});
const result = await instance.run();
t.true(result.passed);
t.is(instance.assertCount, 1);
});
test('try-commit is bound', async t => {
const result = await ava(async a => {
const {try: tryFn} = a;
const res = await tryFn(b => b.pass());
await res.commit();
}).run();
t.true(result.passed);
});
test('try-commit discards failed attempt', async t => {
const result = await ava(async a => {
const res = await a.try(b => b.fail());
await res.discard();
await a.pass();
}).run();
t.true(result.passed);
});
test('try-commit can discard produced result', async t => {
const result = await ava(async a => {
const res = await a.try(b => b.pass());
res.discard();
}).run();
t.false(result.passed);
t.ok(result.error);
t.match(result.error.message, /without running any assertions/);
t.is(result.error.name, 'Error');
});
test('try-commit fails when not all assertions were committed/discarded', async t => {
const result = await ava(async a => {
a.pass();
await a.try(b => b.pass());
}).run();
t.false(result.passed);
t.ok(result.error);
t.match(result.error.message, /not all attempts were committed/);
t.is(result.error.name, 'Error');
});
test('try-commit works with values', async t => {
const testValue1 = 123;
const testValue2 = 123;
const result = await ava(async a => {
const res = await a.try((b, val1, val2) => {
b.is(val1, val2);
}, testValue1, testValue2);
t.true(res.passed);
res.commit();
}).run();
t.true(result.passed);
});
test('try-commit is properly counted', async t => {
const instance = ava(async a => {
const res = await a.try(b => {
b.is(1, 1);
b.is(2, 2);
b.pass();
});
t.true(res.passed);
t.is(instance.pendingAttemptCount, 1);
res.commit();
t.is(instance.pendingAttemptCount, 0);
});
const result = await instance.run();
t.true(result.passed);
t.is(instance.assertCount, 1);
});
test('try-commit is properly counted multiple', async t => {
const instance = ava(async a => {
const [res1, res2, res3] = await Promise.all([
a.try(b => b.pass()),
a.try(b => b.pass()),
a.try(b => b.pass())
]);
t.is(instance.pendingAttemptCount, 3);
res1.commit();
res2.discard();
res3.commit();
t.is(instance.pendingAttemptCount, 0);
});
const result = await instance.run();
t.true(result.passed);
t.is(instance.assertCount, 2);
});
test('try-commit goes as many levels', async t => {
t.plan(5);
const instance = ava(async a => {
t.ok(a.try);
const res1 = await a.try(async b => {
t.ok(b.try);
const res = await b.try(c => {
t.ok(c.try);
c.pass();
});
res.commit();
});
res1.commit();
});
const result = await instance.run();
t.true(result.passed);
t.is(instance.assertCount, 1);
});
test('try-commit fails when not committed', async t => {
const result = await ava(async a => {
const res = await a.try(b => b.pass());
t.true(res.passed);
}).run();
t.false(result.passed);
t.ok(result.error);
t.match(result.error.message, /not all attempts were committed/);
t.is(result.error.name, 'Error');
});
test('try-commit fails when no assertions inside try', async t => {
const result = await ava(async a => {
const res = await a.try(() => {});
t.false(res.passed);
t.ok(res.errors);
t.is(res.errors.length, 1);
const error = res.errors[0];
t.match(error.message, /Test finished without running any assertions/);
t.is(error.name, 'Error');
res.commit();
}).run();
t.false(result.passed);
});
test('try-commit fails when no assertions inside multiple try', async t => {
const result = await ava(async a => {
const [res1, res2] = await Promise.all([
a.try(b => b.pass()),
a.try(() => {})
]);
res1.commit();
t.true(res1.passed);
t.false(res2.passed);
t.ok(res2.errors);
t.is(res2.errors.length, 1);
const error = res2.errors[0];
t.match(error.message, /Test finished without running any assertions/);
t.is(error.name, 'Error');
res2.commit();
}).run();
t.false(result.passed);
});
test('test fails when try-commit committed to failed state', async t => {
const result = await ava(async a => {
const res = await a.try(b => b.fail());
t.false(res.passed);
res.commit();
}).run();
t.false(result.passed);
});
test('try-commit has proper titles, when going in depth and width', async t => {
t.plan(6);
await ava(async a => {
t.is(a.title, 'test');
await Promise.all([
a.try(async b => {
t.is(b.title, 'test ─ attempt 1');
await Promise.all([
b.try(c => t.is(c.title, 'test ─ attempt 1 ─ attempt 1')),
b.try(c => t.is(c.title, 'test ─ attempt 1 ─ attempt 2'))
]);
}),
a.try(b => t.is(b.title, 'test ─ attempt 2')),
a.try(b => t.is(b.title, 'test ─ attempt 3'))
]);
}).run();
});
test('try-commit does not fail when calling commit twice', async t => {
const result = await ava(async a => {
const res = await a.try(b => b.pass());
res.commit();
res.commit();
}).run();
t.true(result.passed);
t.false(result.error);
});
test('try-commit does not fail when calling discard twice', async t => {
const result = await ava(async a => {
const res = await a.try(b => b.pass());
res.discard();
res.discard();
}).run();
t.false(result.passed);
t.ok(result.error);
t.match(result.error.message, /Test finished without running any assertions/);
t.is(result.error.name, 'Error');
});
test('try-commit allows planning inside the try', async t => {
const result = await ava(async a => {
const res = await a.try(b => {
b.plan(3);
b.pass();
b.pass();
b.pass();
});
t.true(res.passed);
res.commit();
}).run();
t.true(result.passed);
});
test('try-commit fails when plan is not reached inside the try', async t => {
const result = await ava(async a => {
const res = await a.try(b => {
b.plan(3);
b.pass();
b.pass();
});
t.false(res.passed);
res.commit();
}).run();
t.false(result.passed);
});
test('plan within try-commit is not affected by assertions outside', async t => {
const result = await ava(async a => {
a.is(1, 1);
a.is(2, 2);
const attempt = a.try(b => {
b.plan(3);
});
const res = await attempt;
t.false(res.passed);
res.commit();
}).run();
t.false(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 result = await ava(async a => {
a.plan(2);
const res = await a.try(b => {
b.plan(3);
b.pass();
b.pass();
b.pass();
});
t.true(res.passed);
res.commit();
}).run();
t.false(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 result = await ava.failing(async a => {
const res = await a.try(b => b.fail());
t.false(res.passed);
t.ok(res.errors);
t.is(res.errors.length, 1);
const error = res.errors[0];
t.match(error.message, /Test failed via `t\.fail\(\)`/);
t.is(error.name, 'AssertionError');
res.commit();
}).run();
t.true(result.passed);
});
test('try-commit works with callback test', async t => {
const result = await ava.cb(a => {
a
.try(b => b.pass())
.then(res => {
res.commit();
a.end();
});
}).run();
t.true(result.passed);
});
test('try-commit works with failing callback test', async t => {
const result = await ava.cb.failing(a => {
a
.try(b => b.fail())
.then(res => {
t.false(res.passed);
t.ok(res.errors);
t.is(res.errors.length, 1);
const error = res.errors[0];
t.match(error.message, /Test failed via `t\.fail\(\)`/);
t.is(error.name, 'AssertionError');
res.commit();
})
.then(() => {
a.end();
});
}).run();
t.true(result.passed);
});
test('try-commit does not allow to use .end() in attempt when parent is callback test', async t => {
const result = await ava.cb(a => {
a
.try(b => {
b.pass();
b.end();
})
.then(res => {
res.commit();
a.end();
});
}).run();
t.false(result.passed);
t.ok(result.error);
t.match(result.error.message, /Error thrown in test/);
t.is(result.error.name, 'AssertionError');
t.match(result.error.values[0].formatted, /t\.end.*not supported/);
});
test('try-commit does not allow to use .end() in attempt when parent is regular test', async t => {
const result = await ava(async a => {
const res = await a.try(b => {
b.pass();
b.end();
});
res.commit();
}).run();
t.false(result.passed);
t.ok(result.error);
t.match(result.error.message, /Error thrown in test/);
t.is(result.error.name, 'AssertionError');
t.match(result.error.values[0].formatted, /t\.end.*not supported/);
});
test('try-commit accepts macros', async t => {
const macro = b => {
t.is(b.title, 'test ─ Title');
b.pass();
};
macro.title = (providedTitle = '') => `${providedTitle} Title`.trim();
const result = await ava(async a => {
const res = await a.try(macro);
t.true(res.passed);
res.commit();
}).run();
t.true(result.passed);
});
test('try-commit accepts multiple macros', async t => {
const macros = [b => b.pass(), b => b.fail()];
const result = await ava(async a => {
const [res1, res2] = await a.try(macros);
t.true(res1.passed);
res1.commit();
t.false(res2.passed);
res2.discard();
}).run();
t.true(result.passed);
});
test('try-commit returns results in the same shape as when implementations are passed', async t => {
const result = await ava(async a => {
const [res1, res2, res3] = await Promise.all([
a.try(b => b.pass()),
a.try([b => b.pass()]),
a.try([b => b.pass(), b => b.fail()])
]);
t.match(res1, {passed: true});
res1.commit();
t.is(res2.length, 1);
t.match(res2, [{passed: true}]);
res2[0].commit();
t.is(res3.length, 2);
t.match(res3, [{passed: true}, {passed: false}]);
res3[0].commit();
res3[1].discard();
}).run();
t.true(result.passed);
});
test('try-commit abides timeout', async t => {
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.is(result1.passed, false);
t.match(result1.error.message, /timeout/);
});
test('try-commit fails when it exceeds its own timeout', async t => {
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.false(result.passed);
t.ok(result.errors);
t.is(result.errors.length, 1);
const error = result.errors[0];
t.match(error.message, /Test timeout exceeded/);
t.is(error.name, 'Error');
result.discard();
a.pass();
}).run();
t.true(result.passed);
});
test('try-commit refreshes the timeout on commit/discard', async t => {
const result1 = await ava.cb(a => {
a.timeout(100);
a.plan(3);
setTimeout(() => a.try(b => b.pass()).then(result => result.commit()), 50);
setTimeout(() => a.try(b => b.pass()).then(result => result.commit()), 100);
setTimeout(() => a.try(b => b.pass()).then(result => result.commit()), 150);
setTimeout(() => a.end(), 200);
}).run();
t.is(result1.passed, true);
});
test('assertions within try-commit do not refresh the timeout', async t => {
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 res = await a.try(async b => {
await delay(10);
b.is(1, 1);
await delay(10);
});
res.commit();
}).run();
t.false(result.passed);
t.ok(result.error);
t.match(result.error.message, /Test timeout exceeded/);
t.is(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 result = await ava(async a => {
const res = await a.try(b => {
b.pass();
t.strictDeepEqual(b.context, data);
});
await res.commit();
}, context).run();
t.is(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 result = await ava(async a => {
t.strictDeepEqual(a.context, data);
const res = await a.try(b => {
b.pass();
b.context = {bar: 'foo'};
});
res.commit();
t.strictDeepEqual(a.context, data);
}, context).run();
t.is(result.passed, true);
});
test('do not run assertions outside of an active attempt', async t => {
const passing = await ava(async a => {
await a.try(() => {});
a.pass();
}).run();
t.false(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('')));
}).run();
t.false(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();
}).run();
t.false(failing.passed);
t.match(failing.error.message, /Assertion failed, but an attempt is pending. Use the attempt’s assertions instead/);
});