@@ -450,10 +450,12 @@ test('throws and notThrows work with promises', t => {
450
450
let result ;
451
451
ava ( a => {
452
452
a . plan ( 2 ) ;
453
- a . throws ( delay . reject ( 10 , new Error ( 'foo' ) ) , 'foo' ) ;
454
- a . notThrows ( delay ( 20 ) . then ( ( ) => {
455
- asyncCalled = true ;
456
- } ) ) ;
453
+ return Promise . all ( [
454
+ a . throws ( delay . reject ( 10 , new Error ( 'foo' ) ) , 'foo' ) ,
455
+ a . notThrows ( delay ( 20 ) . then ( ( ) => {
456
+ asyncCalled = true ;
457
+ } ) )
458
+ ] ) ;
457
459
} , null , r => {
458
460
result = r ;
459
461
} ) . run ( ) . then ( passed => {
@@ -495,143 +497,97 @@ test('cb test that throws sync', t => {
495
497
t . end ( ) ;
496
498
} ) ;
497
499
498
- test ( 'waits for t.throws to resolve after t.end is called ' , t => {
500
+ test ( 'multiple resolving and rejecting promises passed to t.throws/t.notThrows ' , t => {
499
501
let result ;
500
- ava . cb ( a => {
501
- a . plan ( 1 ) ;
502
- a . notThrows ( delay ( 10 ) , 'foo' ) ;
503
- a . end ( ) ;
502
+ ava ( a => {
503
+ a . plan ( 6 ) ;
504
+ const promises = [ ] ;
505
+ for ( let i = 0 ; i < 3 ; i ++ ) {
506
+ promises . push (
507
+ a . throws ( delay . reject ( 10 , new Error ( 'foo' ) ) , 'foo' ) ,
508
+ a . notThrows ( delay ( 10 ) , 'foo' )
509
+ ) ;
510
+ }
511
+ return Promise . all ( promises ) ;
504
512
} , null , r => {
505
513
result = r ;
506
514
} ) . run ( ) . then ( passed => {
507
515
t . is ( passed , true ) ;
508
- t . is ( result . result . planCount , 1 ) ;
509
- t . is ( result . result . assertCount , 1 ) ;
516
+ t . is ( result . result . planCount , 6 ) ;
517
+ t . is ( result . result . assertCount , 6 ) ;
510
518
t . end ( ) ;
511
519
} ) ;
512
520
} ) ;
513
521
514
- test ( 'waits for t.throws to reject after t.end is called ' , t => {
522
+ test ( 'fails if test ends while there are pending assertions ' , t => {
515
523
let result ;
516
- ava . cb ( a => {
517
- a . plan ( 1 ) ;
518
- a . throws ( delay . reject ( 10 , new Error ( 'foo' ) ) , 'foo' ) ;
519
- a . end ( ) ;
524
+ const passed = ava ( a => {
525
+ a . throws ( Promise . reject ( new Error ( ) ) ) ;
520
526
} , null , r => {
521
527
result = r ;
522
- } ) . run ( ) . then ( passed => {
523
- t . is ( passed , true ) ;
524
- t . is ( result . result . planCount , 1 ) ;
525
- t . is ( result . result . assertCount , 1 ) ;
526
- t . end ( ) ;
527
- } ) ;
528
- } ) ;
528
+ } ) . run ( ) ;
529
529
530
- test ( 'waits for t.throws to resolve after the promise returned from the test resolves' , t => {
531
- let result ;
532
- ava ( a => {
533
- a . plan ( 1 ) ;
534
- a . notThrows ( delay ( 10 ) , 'foo' ) ;
535
- return Promise . resolve ( ) ;
536
- } , null , r => {
537
- result = r ;
538
- } ) . run ( ) . then ( passed => {
539
- t . is ( passed , true ) ;
540
- t . is ( result . result . planCount , 1 ) ;
541
- t . is ( result . result . assertCount , 1 ) ;
542
- t . end ( ) ;
543
- } ) ;
530
+ t . is ( passed , false ) ;
531
+ t . is ( result . reason . name , 'Error' ) ;
532
+ t . match ( result . reason . message , / T e s t f i n i s h e d , b u t a n a s s e r t i o n i s s t i l l p e n d i n g / ) ;
533
+ t . end ( ) ;
544
534
} ) ;
545
535
546
- test ( 'waits for t.throws to reject after the promise returned from the test resolves ' , t => {
536
+ test ( 'fails if callback test ends while there are pending assertions ' , t => {
547
537
let result ;
548
- ava ( a => {
549
- a . plan ( 1 ) ;
550
- a . throws ( delay . reject ( 10 , new Error ( 'foo' ) ) , 'foo' ) ;
551
- return Promise . resolve ( ) ;
538
+ const passed = ava . cb ( a => {
539
+ a . throws ( Promise . reject ( new Error ( ) ) ) ;
540
+ a . end ( ) ;
552
541
} , null , r => {
553
542
result = r ;
554
- } ) . run ( ) . then ( passed => {
555
- t . is ( passed , true ) ;
556
- t . is ( result . result . planCount , 1 ) ;
557
- t . is ( result . result . assertCount , 1 ) ;
558
- t . end ( ) ;
559
- } ) ;
560
- } ) ;
543
+ } ) . run ( ) ;
561
544
562
- test ( 'multiple resolving and rejecting promises passed to t.throws/t.notThrows' , t => {
563
- let result ;
564
- ava ( a => {
565
- a . plan ( 6 ) ;
566
- for ( let i = 0 ; i < 3 ; i ++ ) {
567
- a . throws ( delay . reject ( 10 , new Error ( 'foo' ) ) , 'foo' ) ;
568
- a . notThrows ( delay ( 10 ) , 'foo' ) ;
569
- }
570
- } , null , r => {
571
- result = r ;
572
- } ) . run ( ) . then ( passed => {
573
- t . is ( passed , true ) ;
574
- t . is ( result . result . planCount , 6 ) ;
575
- t . is ( result . result . assertCount , 6 ) ;
576
- t . end ( ) ;
577
- } ) ;
545
+ t . is ( passed , false ) ;
546
+ t . is ( result . reason . name , 'Error' ) ;
547
+ t . match ( result . reason . message , / T e s t f i n i s h e d , b u t a n a s s e r t i o n i s s t i l l p e n d i n g / ) ;
548
+ t . end ( ) ;
578
549
} ) ;
579
550
580
- test ( 'number of assertions matches t.plan when the test exits, but before all pending assertions resolve another is added ' , t => {
551
+ test ( 'fails if async test ends while there are pending assertions' , t => {
581
552
let result ;
582
553
ava ( a => {
583
- a . plan ( 2 ) ;
584
- a . throws ( delay . reject ( 10 , new Error ( 'foo' ) ) , 'foo' ) ;
585
- a . notThrows ( delay ( 10 ) , 'foo' ) ;
586
- setTimeout ( ( ) => {
587
- a . pass ( ) ;
588
- } , 5 ) ;
554
+ a . throws ( Promise . reject ( new Error ( ) ) ) ;
555
+ return Promise . resolve ( ) ;
589
556
} , null , r => {
590
557
result = r ;
591
558
} ) . run ( ) . then ( passed => {
592
559
t . is ( passed , false ) ;
593
- t . match ( result . reason . message , / A s s e r t i o n p a s s e d , b u t t e s t h a s a l r e a d y f i n i s h e d / ) ;
594
560
t . is ( result . reason . name , 'Error' ) ;
561
+ t . match ( result . reason . message , / T e s t f i n i s h e d , b u t a n a s s e r t i o n i s s t i l l p e n d i n g / ) ;
595
562
t . end ( ) ;
596
563
} ) ;
597
564
} ) ;
598
565
599
- test ( 'number of assertions matches t.plan when the test exits, but before all pending assertions resolve, a failing assertion is added' , t => {
600
- let result ;
566
+ // This behavior is incorrect, but feedback cannot be provided to the user due to
567
+ // https://github.com/avajs/ava/issues/1330
568
+ test ( 'no crash when adding assertions after the test has ended' , t => {
569
+ t . plan ( 3 ) ;
570
+
601
571
ava ( a => {
602
- a . plan ( 2 ) ;
603
- a . throws ( delay . reject ( 10 , new Error ( 'foo' ) ) , 'foo' ) ;
604
- a . notThrows ( delay ( 10 ) , 'foo' ) ;
605
- setTimeout ( ( ) => {
606
- a . fail ( ) ;
607
- } , 5 ) ;
608
- } , null , r => {
609
- result = r ;
610
- } ) . run ( ) . then ( passed => {
611
- t . is ( passed , false ) ;
612
- t . match ( result . reason . message , / A s s e r t i o n f a i l e d , b u t t e s t h a s a l r e a d y f i n i s h e d / ) ;
613
- t . is ( result . reason . name , 'Error' ) ;
614
- t . end ( ) ;
615
- } ) ;
616
- } ) ;
572
+ a . pass ( ) ;
573
+ setImmediate ( ( ) => {
574
+ t . doesNotThrow ( ( ) => a . pass ( ) ) ;
575
+ } ) ;
576
+ } ) . run ( ) ;
617
577
618
- test ( 'number of assertions doesn\'t match plan when the test exits, but before all promises resolve another is added' , t => {
619
- let result ;
620
- const passed = ava ( a => {
621
- a . plan ( 3 ) ;
622
- a . throws ( delay . reject ( 10 , new Error ( 'foo' ) ) , 'foo' ) ;
623
- a . notThrows ( delay ( 10 ) , 'foo' ) ;
624
- setTimeout ( ( ) => {
625
- a . throws ( Promise . reject ( new Error ( 'foo' ) ) , 'foo' ) ;
626
- } , 5 ) ;
627
- } , null , r => {
628
- result = r ;
578
+ ava ( a => {
579
+ a . pass ( ) ;
580
+ setImmediate ( ( ) => {
581
+ t . doesNotThrow ( ( ) => a . fail ( ) ) ;
582
+ } ) ;
629
583
} ) . run ( ) ;
630
584
631
- t . is ( passed , false ) ;
632
- t . is ( result . reason . assertion , 'plan' ) ;
633
- t . is ( result . reason . operator , '===' ) ;
634
- t . end ( ) ;
585
+ ava ( a => {
586
+ a . pass ( ) ;
587
+ setImmediate ( ( ) => {
588
+ t . doesNotThrow ( ( ) => a . notThrows ( Promise . resolve ( ) ) ) ;
589
+ } ) ;
590
+ } ) . run ( ) ;
635
591
} ) ;
636
592
637
593
test ( 'contextRef' , t => {
@@ -725,8 +681,8 @@ test('failing tests must not return a fulfilled promise', t => {
725
681
test ( 'failing tests pass when returning a rejected promise' , t => {
726
682
ava . failing ( a => {
727
683
a . plan ( 1 ) ;
728
- a . notThrows ( delay ( 10 ) , 'foo' ) ;
729
- return Promise . reject ( ) ;
684
+ return a . notThrows ( delay ( 10 ) , 'foo' )
685
+ . then ( ( ) => Promise . reject ( ) ) ;
730
686
} ) . run ( ) . then ( passed => {
731
687
t . is ( passed , true ) ;
732
688
t . end ( ) ;
@@ -735,7 +691,7 @@ test('failing tests pass when returning a rejected promise', t => {
735
691
736
692
test ( 'failing tests pass with `t.throws(nonThrowingPromise)`' , t => {
737
693
ava . failing ( a => {
738
- a . throws ( Promise . resolve ( 10 ) ) ;
694
+ return a . throws ( Promise . resolve ( 10 ) ) ;
739
695
} ) . run ( ) . then ( passed => {
740
696
t . is ( passed , true ) ;
741
697
t . end ( ) ;
@@ -745,7 +701,7 @@ test('failing tests pass with `t.throws(nonThrowingPromise)`', t => {
745
701
test ( 'failing tests fail with `t.notThrows(throws)`' , t => {
746
702
let result ;
747
703
ava . failing ( a => {
748
- a . notThrows ( Promise . resolve ( 'foo' ) ) ;
704
+ return a . notThrows ( Promise . resolve ( 'foo' ) ) ;
749
705
} , null , r => {
750
706
result = r ;
751
707
} ) . run ( ) . then ( passed => {
0 commit comments