1
1
import context from '@aws-lambda-powertools/testing-utils/context' ;
2
- import {
3
- onMutationEventFactory ,
4
- onQueryEventFactory ,
5
- } from 'tests/helpers/factories.js' ;
2
+ import { onGraphqlEventFactory } from 'tests/helpers/factories.js' ;
6
3
import { beforeEach , describe , expect , it , vi } from 'vitest' ;
7
4
import { AppSyncGraphQLResolver } from '../../../src/appsync-graphql/AppSyncGraphQLResolver.js' ;
8
5
import { ResolverNotFoundException } from '../../../src/appsync-graphql/errors.js' ;
@@ -17,7 +14,10 @@ describe('Class: AppSyncGraphQLResolver', () => {
17
14
const app = new AppSyncGraphQLResolver ( { logger : console } ) ;
18
15
19
16
// Act
20
- const result = await app . resolve ( [ onQueryEventFactory ( ) ] , context ) ;
17
+ const result = await app . resolve (
18
+ [ onGraphqlEventFactory ( 'getPost' , 'Query' ) ] ,
19
+ context
20
+ ) ;
21
21
22
22
// Assess
23
23
expect ( console . warn ) . toHaveBeenCalledWith (
@@ -40,26 +40,26 @@ describe('Class: AppSyncGraphQLResolver', () => {
40
40
expect ( result ) . toBeUndefined ( ) ;
41
41
} ) ;
42
42
43
- it ( 'throw error if there are no onQuery handlers' , async ( ) => {
43
+ it ( 'throws error if there are no onQuery handlers' , async ( ) => {
44
44
// Prepare
45
45
const app = new AppSyncGraphQLResolver ( { logger : console } ) ;
46
46
47
47
// Act && Assess
48
48
await expect (
49
- app . resolve ( onQueryEventFactory ( 'getPost' ) , context )
49
+ app . resolve ( onGraphqlEventFactory ( 'getPost' , 'Query ') , context )
50
50
) . rejects . toThrow (
51
51
new ResolverNotFoundException ( 'No resolver found for Query-getPost' )
52
52
) ;
53
53
expect ( console . error ) . toHaveBeenCalled ( ) ;
54
54
} ) ;
55
55
56
- it ( 'throw error if there are no onMutation handlers' , async ( ) => {
56
+ it ( 'throws error if there are no onMutation handlers' , async ( ) => {
57
57
// Prepare
58
58
const app = new AppSyncGraphQLResolver ( { logger : console } ) ;
59
59
60
60
// Act && Assess
61
61
await expect (
62
- app . resolve ( onMutationEventFactory ( 'addPost' ) , context )
62
+ app . resolve ( onGraphqlEventFactory ( 'addPost' , 'Mutation ') , context )
63
63
) . rejects . toThrow (
64
64
new ResolverNotFoundException ( 'No resolver found for Mutation-addPost' )
65
65
) ;
@@ -84,7 +84,7 @@ describe('Class: AppSyncGraphQLResolver', () => {
84
84
85
85
// Act
86
86
const result = await app . resolve (
87
- onQueryEventFactory ( 'getPost' , { id : '123' } ) ,
87
+ onGraphqlEventFactory ( 'getPost' , 'Query ', { id : '123' } ) ,
88
88
context
89
89
) ;
90
90
@@ -115,7 +115,7 @@ describe('Class: AppSyncGraphQLResolver', () => {
115
115
116
116
// Act
117
117
const result = await app . resolve (
118
- onMutationEventFactory ( 'addPost' , {
118
+ onGraphqlEventFactory ( 'addPost' , 'Mutation ', {
119
119
title : 'Post Title' ,
120
120
content : 'Post Content' ,
121
121
} ) ,
@@ -138,6 +138,74 @@ describe('Class: AppSyncGraphQLResolver', () => {
138
138
} ) ;
139
139
} ) ;
140
140
141
+ it ( 'logs only warnings and errors using global console object if no logger supplied' , async ( ) => {
142
+ // Prepare
143
+ const app = new AppSyncGraphQLResolver ( ) ;
144
+ app . resolver < { title : string ; content : string } > (
145
+ async ( { title, content } ) => {
146
+ return {
147
+ id : '123' ,
148
+ title,
149
+ content,
150
+ } ;
151
+ } ,
152
+ {
153
+ fieldName : 'addPost' ,
154
+ typeName : 'Mutation' ,
155
+ }
156
+ ) ;
157
+
158
+ // Act
159
+ const result = await app . resolve (
160
+ onGraphqlEventFactory ( 'addPost' , 'Mutation' , {
161
+ title : 'Post Title' ,
162
+ content : 'Post Content' ,
163
+ } ) ,
164
+ context
165
+ ) ;
166
+
167
+ // Assess
168
+ expect ( console . debug ) . not . toHaveBeenCalledWith ( ) ;
169
+ expect ( console . debug ) . not . toHaveBeenCalledWith ( ) ;
170
+ expect ( result ) . toEqual ( {
171
+ id : '123' ,
172
+ title : 'Post Title' ,
173
+ content : 'Post Content' ,
174
+ } ) ;
175
+ } ) ;
176
+
177
+ it ( 'emits debug message when AWS_LAMBDA_LOG_LEVEL is set to DEBUG' , async ( ) => {
178
+ // Prepare
179
+ vi . stubEnv ( 'AWS_LAMBDA_LOG_LEVEL' , 'DEBUG' ) ;
180
+ const app = new AppSyncGraphQLResolver ( ) ;
181
+
182
+ app . resolver < { title : string ; content : string } > (
183
+ async ( { title, content } ) => {
184
+ return {
185
+ id : '123' ,
186
+ title,
187
+ content,
188
+ } ;
189
+ } ,
190
+ {
191
+ fieldName : 'addPost' ,
192
+ typeName : 'Mutation' ,
193
+ }
194
+ ) ;
195
+
196
+ // Act
197
+ await app . resolve (
198
+ onGraphqlEventFactory ( 'addPost' , 'Mutation' , {
199
+ title : 'Post Title' ,
200
+ content : 'Post Content' ,
201
+ } ) ,
202
+ context
203
+ ) ;
204
+
205
+ // Assess
206
+ expect ( console . debug ) . toHaveBeenCalled ( ) ;
207
+ } ) ;
208
+
141
209
it . each ( [
142
210
{
143
211
type : 'base error' ,
@@ -171,7 +239,7 @@ describe('Class: AppSyncGraphQLResolver', () => {
171
239
172
240
// Act
173
241
const result = await app . resolve (
174
- onMutationEventFactory ( 'addPost' , {
242
+ onGraphqlEventFactory ( 'addPost' , 'Mutation ', {
175
243
title : 'Post Title' ,
176
244
content : 'Post Content' ,
177
245
} ) ,
0 commit comments