@@ -2,6 +2,7 @@ import { expect } from 'chai';
2
2
import { describe , it } from 'mocha' ;
3
3
4
4
import { expectJSON } from '../../__testUtils__/expectJSON' ;
5
+ import { resolveOnNextTick } from '../../__testUtils__/resolveOnNextTick' ;
5
6
6
7
import { inspect } from '../../jsutils/inspect' ;
7
8
import { invariant } from '../../jsutils/invariant' ;
@@ -626,57 +627,52 @@ describe('Execute: Handles basic execution tasks', () => {
626
627
} ) ;
627
628
628
629
it ( 'handles sync errors combined with rejections' , async ( ) => {
629
- const catchUnhandledRejections = ( reason : any ) => {
630
- throw new Error ( 'Unhandled rejection: ' + reason . message ) ;
631
- } ;
632
-
633
- process . on ( 'unhandledRejection' , catchUnhandledRejections ) ;
630
+ let isAsyncResolverCalled = false ;
634
631
635
- try {
636
- const schema = new GraphQLSchema ( {
637
- query : new GraphQLObjectType ( {
638
- name : 'Type' ,
639
- fields : {
640
- syncNullError : {
641
- type : new GraphQLNonNull ( GraphQLString ) ,
642
- resolve : ( ) => null ,
643
- } ,
644
- asyncNullError : {
645
- type : new GraphQLNonNull ( GraphQLString ) ,
646
- resolve : ( ) => Promise . resolve ( null ) ,
632
+ const schema = new GraphQLSchema ( {
633
+ query : new GraphQLObjectType ( {
634
+ name : 'Query' ,
635
+ fields : {
636
+ syncNullError : {
637
+ type : new GraphQLNonNull ( GraphQLString ) ,
638
+ resolve : ( ) => null ,
639
+ } ,
640
+ asyncNullError : {
641
+ type : new GraphQLNonNull ( GraphQLString ) ,
642
+ async resolve ( ) {
643
+ await resolveOnNextTick ( ) ;
644
+ await resolveOnNextTick ( ) ;
645
+ await resolveOnNextTick ( ) ;
646
+ isAsyncResolverCalled = true ;
647
+ return Promise . resolve ( null ) ;
647
648
} ,
648
649
} ,
649
- } ) ,
650
- } ) ;
650
+ } ,
651
+ } ) ,
652
+ } ) ;
651
653
652
- // Order is important here, as the promise has to be created before the synchronous error is thrown
653
- const document = parse ( `
654
- {
655
- asyncNullError
656
- syncNullError
657
- }
658
- ` ) ;
659
-
660
- const rootValue = { } ;
661
-
662
- const result = await execute ( { schema, document, rootValue } ) ;
663
- expectJSON ( result ) . toDeepEqual ( {
664
- data : null ,
665
- errors : [
666
- {
667
- message :
668
- 'Cannot return null for non-nullable field Type.asyncNullError.' ,
669
- locations : [ { line : 3 , column : 11 } ] ,
670
- path : [ 'asyncNullError' ] ,
671
- } ,
672
- ] ,
673
- } ) ;
654
+ // Order is important here, as the promise has to be created before the synchronous error is thrown
655
+ const document = parse ( `
656
+ {
657
+ asyncNullError
658
+ syncNullError
659
+ }
660
+ ` ) ;
674
661
675
- // Allow time for the asyncReject field to be rejected
676
- await new Promise ( ( resolve ) => process . nextTick ( resolve ) ) ;
677
- } finally {
678
- process . off ( 'unhandledRejection' , catchUnhandledRejections ) ;
679
- }
662
+ const result = await execute ( { schema, document } ) ;
663
+
664
+ expect ( isAsyncResolverCalled ) . to . equal ( true ) ;
665
+ expectJSON ( result ) . toDeepEqual ( {
666
+ data : null ,
667
+ errors : [
668
+ {
669
+ message :
670
+ 'Cannot return null for non-nullable field Query.syncNullError.' ,
671
+ locations : [ { line : 4 , column : 9 } ] ,
672
+ path : [ 'syncNullError' ] ,
673
+ } ,
674
+ ] ,
675
+ } ) ;
680
676
} ) ;
681
677
682
678
it ( 'Full response path is included for non-nullable fields' , ( ) => {
0 commit comments