@@ -450,10 +450,12 @@ test('throws and notThrows work with promises', t => {
450450 let result ;
451451 ava ( a => {
452452 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+ ] ) ;
457459 } , null , r => {
458460 result = r ;
459461 } ) . run ( ) . then ( passed => {
@@ -495,143 +497,97 @@ test('cb test that throws sync', t => {
495497 t . end ( ) ;
496498} ) ;
497499
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 => {
499501 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 ) ;
504512 } , null , r => {
505513 result = r ;
506514 } ) . run ( ) . then ( passed => {
507515 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 ) ;
510518 t . end ( ) ;
511519 } ) ;
512520} ) ;
513521
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 => {
515523 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 ( ) ) ) ;
520526 } , null , r => {
521527 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 ( ) ;
529529
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 ( ) ;
544534} ) ;
545535
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 => {
547537 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 ( ) ;
552541 } , null , r => {
553542 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 ( ) ;
561544
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 ( ) ;
578549} ) ;
579550
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 => {
581552 let result ;
582553 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 ( ) ;
589556 } , null , r => {
590557 result = r ;
591558 } ) . run ( ) . then ( passed => {
592559 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 / ) ;
594560 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 / ) ;
595562 t . end ( ) ;
596563 } ) ;
597564} ) ;
598565
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+
601571 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 ( ) ;
617577
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+ } ) ;
629583 } ) . run ( ) ;
630584
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 ( ) ;
635591} ) ;
636592
637593test ( 'contextRef' , t => {
@@ -725,8 +681,8 @@ test('failing tests must not return a fulfilled promise', t => {
725681test ( 'failing tests pass when returning a rejected promise' , t => {
726682 ava . failing ( a => {
727683 a . plan ( 1 ) ;
728- a . notThrows ( delay ( 10 ) , 'foo' ) ;
729- return Promise . reject ( ) ;
684+ return a . notThrows ( delay ( 10 ) , 'foo' )
685+ . then ( ( ) => Promise . reject ( ) ) ;
730686 } ) . run ( ) . then ( passed => {
731687 t . is ( passed , true ) ;
732688 t . end ( ) ;
@@ -735,7 +691,7 @@ test('failing tests pass when returning a rejected promise', t => {
735691
736692test ( 'failing tests pass with `t.throws(nonThrowingPromise)`' , t => {
737693 ava . failing ( a => {
738- a . throws ( Promise . resolve ( 10 ) ) ;
694+ return a . throws ( Promise . resolve ( 10 ) ) ;
739695 } ) . run ( ) . then ( passed => {
740696 t . is ( passed , true ) ;
741697 t . end ( ) ;
@@ -745,7 +701,7 @@ test('failing tests pass with `t.throws(nonThrowingPromise)`', t => {
745701test ( 'failing tests fail with `t.notThrows(throws)`' , t => {
746702 let result ;
747703 ava . failing ( a => {
748- a . notThrows ( Promise . resolve ( 'foo' ) ) ;
704+ return a . notThrows ( Promise . resolve ( 'foo' ) ) ;
749705 } , null , r => {
750706 result = r ;
751707 } ) . run ( ) . then ( passed => {
0 commit comments