1
1
'use strict' ;
2
2
var test = require ( 'tap' ) . test ;
3
+ var Promise = global . Promise = require ( 'bluebird' ) ;
4
+ var delay = require ( 'delay' ) ;
3
5
var _ava = require ( '../lib/test' ) ;
4
6
5
- function delay ( val , time ) {
6
- return new Promise ( function ( resolve ) {
7
- setTimeout ( function ( ) {
8
- resolve ( val ) ;
9
- } , time ) ;
7
+ // This is a helper some boilerplate for testing `t.throws`
8
+ function delayReject ( ms , message ) {
9
+ return delay ( ms ) . then ( function ( ) {
10
+ throw new Error ( message ) ;
10
11
} ) ;
11
12
}
12
13
@@ -364,10 +365,10 @@ test('throws and doesNotThrow work with promises', function (t) {
364
365
var asyncCalled = false ;
365
366
ava ( function ( a ) {
366
367
a . plan ( 2 ) ;
367
- a . throws ( delay ( Promise . reject ( new Error ( 'foo' ) ) , 10 ) , 'foo' ) ;
368
- a . doesNotThrow ( delay ( Promise . resolve ( ) . then ( function ( ) {
368
+ a . throws ( delayReject ( 10 , 'foo' ) , 'foo' ) ;
369
+ a . doesNotThrow ( delay ( 20 ) . then ( function ( ) {
369
370
asyncCalled = true ;
370
- } ) , 20 ) ) ;
371
+ } ) ) ;
371
372
} ) . run ( ) . then ( function ( a ) {
372
373
t . ifError ( a . assertError ) ;
373
374
t . is ( a . planCount , 2 ) ;
@@ -377,10 +378,10 @@ test('throws and doesNotThrow work with promises', function (t) {
377
378
} ) ;
378
379
} ) ;
379
380
380
- test ( 't.end is called when a promise passed to t.throws hasn\'t resolved yet ' , function ( t ) {
381
+ test ( 'waits for t.throws to resolve after t.end is called ' , function ( t ) {
381
382
ava . cb ( function ( a ) {
382
383
a . plan ( 1 ) ;
383
- a . doesNotThrow ( delay ( Promise . resolve ( ) , 10 ) , 'foo' ) ;
384
+ a . doesNotThrow ( delay ( 10 ) , 'foo' ) ;
384
385
a . end ( ) ;
385
386
} ) . run ( ) . then ( function ( a ) {
386
387
t . ifError ( a . assertError ) ;
@@ -390,10 +391,10 @@ test('t.end is called when a promise passed to t.throws hasn\'t resolved yet', f
390
391
} ) ;
391
392
} ) ;
392
393
393
- test ( 't.end is called when a promise passed to t.throws hasn\'t rejected yet ' , function ( t ) {
394
+ test ( 'waits for t.throws to reject after t.end is called ' , function ( t ) {
394
395
ava . cb ( function ( a ) {
395
396
a . plan ( 1 ) ;
396
- a . throws ( delay ( Promise . reject ( new Error ( 'foo' ) ) , 10 ) , 'foo' ) ;
397
+ a . throws ( delayReject ( 10 , 'foo' ) , 'foo' ) ;
397
398
a . end ( ) ;
398
399
} ) . run ( ) . then ( function ( a ) {
399
400
t . ifError ( a . assertError ) ;
@@ -403,10 +404,10 @@ test('t.end is called when a promise passed to t.throws hasn\'t rejected yet', f
403
404
} ) ;
404
405
} ) ;
405
406
406
- test ( 'test returns a promise that resolves before a promise passed to t.throws resolves' , function ( t ) {
407
+ test ( 'waits for t.throws to resolve after the promise returned from the test resolves' , function ( t ) {
407
408
ava ( function ( a ) {
408
409
a . plan ( 1 ) ;
409
- a . doesNotThrow ( delay ( Promise . resolve ( ) , 10 ) , 'foo' ) ;
410
+ a . doesNotThrow ( delay ( 10 ) , 'foo' ) ;
410
411
return Promise . resolve ( ) ;
411
412
} ) . run ( ) . then ( function ( a ) {
412
413
t . ifError ( a . assertError ) ;
@@ -416,10 +417,10 @@ test('test returns a promise that resolves before a promise passed to t.throws r
416
417
} ) ;
417
418
} ) ;
418
419
419
- test ( 'test returns a promise that resolves before a promise passed to t.throws rejects ' , function ( t ) {
420
+ test ( 'waits for t.throws to reject after the promise returned from the test resolves ' , function ( t ) {
420
421
ava ( function ( a ) {
421
422
a . plan ( 1 ) ;
422
- a . throws ( delay ( Promise . reject ( new Error ( 'foo' ) ) , 10 ) , 'foo' ) ;
423
+ a . throws ( delayReject ( 10 , 'foo' ) , 'foo' ) ;
423
424
return Promise . resolve ( ) ;
424
425
} ) . run ( ) . then ( function ( a ) {
425
426
t . ifError ( a . assertError ) ;
@@ -433,8 +434,8 @@ test('multiple resolving and rejecting promises passed to t.throws/t.doesNotThro
433
434
ava ( function ( a ) {
434
435
a . plan ( 6 ) ;
435
436
for ( var i = 0 ; i < 3 ; ++ i ) {
436
- a . throws ( delay ( Promise . reject ( new Error ( 'foo' ) ) , 10 ) , 'foo' ) ;
437
- a . doesNotThrow ( delay ( Promise . resolve ( ) , 10 ) , 'foo' ) ;
437
+ a . throws ( delayReject ( 10 , 'foo' ) , 'foo' ) ;
438
+ a . doesNotThrow ( delay ( 10 ) , 'foo' ) ;
438
439
}
439
440
} ) . run ( ) . then ( function ( a ) {
440
441
t . ifError ( a . assertError ) ;
@@ -447,8 +448,8 @@ test('multiple resolving and rejecting promises passed to t.throws/t.doesNotThro
447
448
test ( 'number of assertions matches t.plan when the test exits, but before all promises resolve another is added' , function ( t ) {
448
449
ava ( function ( a ) {
449
450
a . plan ( 2 ) ;
450
- a . throws ( delay ( Promise . reject ( new Error ( 'foo' ) ) , 10 ) , 'foo' ) ;
451
- a . doesNotThrow ( delay ( Promise . resolve ( ) , 10 ) , 'foo' ) ;
451
+ a . throws ( delayReject ( 10 , 'foo' ) , 'foo' ) ;
452
+ a . doesNotThrow ( delay ( 10 ) , 'foo' ) ;
452
453
setTimeout ( function ( ) {
453
454
a . throws ( Promise . reject ( new Error ( 'foo' ) ) , 'foo' ) ;
454
455
} , 5 ) ;
@@ -460,11 +461,11 @@ test('number of assertions matches t.plan when the test exits, but before all pr
460
461
} ) ;
461
462
} ) ;
462
463
463
- test ( 'number of assertions doesn\'t t. plan when the test exits, but before all promises resolve another is added' , function ( t ) {
464
+ test ( 'number of assertions doesn\'t match plan when the test exits, but before all promises resolve another is added' , function ( t ) {
464
465
ava ( function ( a ) {
465
466
a . plan ( 3 ) ;
466
- a . throws ( delay ( Promise . reject ( new Error ( 'foo' ) ) , 10 ) , 'foo' ) ;
467
- a . doesNotThrow ( delay ( Promise . resolve ( ) , 10 ) , 'foo' ) ;
467
+ a . throws ( delayReject ( 10 , 'foo' ) , 'foo' ) ;
468
+ a . doesNotThrow ( delay ( 10 ) , 'foo' ) ;
468
469
setTimeout ( function ( ) {
469
470
a . throws ( Promise . reject ( new Error ( 'foo' ) ) , 'foo' ) ;
470
471
} , 5 ) ;
0 commit comments