@@ -254,4 +254,103 @@ describe('generate-and-save', () => {
254254 // makes sure it doesn't write a new file
255255 expect ( writeSpy ) . toHaveBeenCalled ( ) ;
256256 } ) ;
257+
258+ describe ( 'Syntax errors when loading pointers' , ( ) => {
259+ const originalConsole = { ...console } ;
260+ const originalNodeEnv = process . env . NODE_ENV ;
261+
262+ let consoleErrorMock ;
263+
264+ beforeEach ( ( ) => {
265+ // Mock common console functions to avoid noise in the terminal
266+ global . console . log = jest . fn ( ) ;
267+ global . console . warn = jest . fn ( ) ;
268+ global . console . error = jest . fn ( ) ;
269+
270+ // By default, the NODE_ENV is set to 'test', and this is used to silent console errors.
271+ // For these tests below, we want to see what's being logged out to console errors.
272+ process . env . NODE_ENV = 'not_test_so_error' ;
273+
274+ consoleErrorMock = jest . mocked ( global . console . error ) ;
275+ } ) ;
276+
277+ afterEach ( ( ) => {
278+ global . console = originalConsole ;
279+ process . env . NODE_ENV = originalNodeEnv ;
280+ } ) ;
281+
282+ test ( 'Schema syntax error - should print native GraphQLError for' , async ( ) => {
283+ try {
284+ await generate (
285+ {
286+ verbose : true ,
287+ schema : './tests/test-files/schema-dir/error-schema.graphql' ,
288+ generates : {
289+ 'src/test.ts' : {
290+ plugins : [ 'typescript' ] ,
291+ } ,
292+ } ,
293+ } ,
294+ false
295+ ) ;
296+ } catch {
297+ expect ( consoleErrorMock . mock . calls [ 0 ] [ 0 ] ) . toBeSimilarStringTo (
298+ '[FAILED] Failed to load schema from ./tests/test-files/schema-dir/error-schema.graphql:'
299+ ) ;
300+ expect ( consoleErrorMock . mock . calls [ 0 ] [ 0 ] ) . toBeSimilarStringTo (
301+ '[FAILED] Syntax Error: Expected Name, found "!".'
302+ ) ;
303+ // We can only use partial file path to the error file, because the error contains absolute path on the host machine
304+ expect ( consoleErrorMock . mock . calls [ 0 ] [ 0 ] ) . toBeSimilarStringTo (
305+ '/tests/test-files/schema-dir/error-schema.graphql:2:15'
306+ ) ;
307+ expect ( consoleErrorMock . mock . calls [ 0 ] [ 0 ] ) . toBeSimilarStringTo ( `
308+ [FAILED] 1 | type Query {
309+ [FAILED] 2 | foo: String!!
310+ [FAILED] | ^
311+ [FAILED] 3 | }
312+ [FAILED]
313+ [FAILED] GraphQL Code Generator supports:
314+ [FAILED]
315+ [FAILED] - ES Modules and CommonJS exports (export as default or named export "schema")
316+ [FAILED] - Introspection JSON File
317+ [FAILED] - URL of GraphQL endpoint
318+ [FAILED] - Multiple files with type definitions (glob expression)
319+ [FAILED] - String in config file
320+ [FAILED]
321+ [FAILED] Try to use one of above options and run codegen again.
322+ ` ) ;
323+ }
324+ } ) ;
325+
326+ test ( 'Document syntax error - should print native GraphQLError' , async ( ) => {
327+ try {
328+ await generate (
329+ {
330+ verbose : true ,
331+ schema : './tests/test-files/schema-dir/schema.ts' ,
332+ documents : './tests/test-files/error-document.graphql' ,
333+ generates : {
334+ 'src/test.ts' : {
335+ plugins : [ 'typescript' ] ,
336+ } ,
337+ } ,
338+ } ,
339+ false
340+ ) ;
341+ } catch {
342+ expect ( consoleErrorMock . mock . calls [ 0 ] [ 0 ] ) . toBeSimilarStringTo (
343+ 'Failed to load documents from ./tests/test-files/error-document.graphql:'
344+ ) ;
345+ expect ( consoleErrorMock . mock . calls [ 0 ] [ 0 ] ) . toBeSimilarStringTo ( 'Syntax Error: Expected "{", found <EOF>.' ) ;
346+ // We can only use partial file path to the error file, because the error contains absolute path on the host machine
347+ expect ( consoleErrorMock . mock . calls [ 0 ] [ 0 ] ) . toBeSimilarStringTo ( '/tests/test-files/error-document.graphql:2:1' ) ;
348+ expect ( consoleErrorMock . mock . calls [ 0 ] [ 0 ] ) . toBeSimilarStringTo ( `
349+ [FAILED] 1 | query
350+ [FAILED] 2 |
351+ [FAILED] | ^
352+ ` ) ;
353+ }
354+ } ) ;
355+ } ) ;
257356} ) ;
0 commit comments