@@ -13,10 +13,25 @@ const SIMPLE_TEST_SCHEMA = `type MyType { f: String } type Query { f: String }`;
1313const inputFile = join ( __dirname , '../temp/input-graphql.tsx' ) ;
1414const outputFile = join ( __dirname , '../temp/output-graphql.tsx' ) ;
1515
16+ const writeSpy = vi . spyOn ( fs , 'writeFile' ) ;
17+ const readSpy = vi . spyOn ( fs , 'readFile' ) ;
18+ const outputErrorSpy = vi . spyOn ( process . stderr , 'write' ) ;
19+
1620describe ( 'generate-and-save' , ( ) => {
21+ beforeEach ( ( ) => {
22+ // We must call .spyOn and .mockReset these individually instead of vi.resetAllMocks
23+ // because there's a `vi.spyOn(process, 'cwd').mockImplementation(() => __dirname);` in vitest.setup.ts
24+ //
25+ // If we called vi.resetAllMocks, the cwd spy is reset too!
26+ // That would cause all `schema` and `documents`paths to be from the root of the workspace
27+ writeSpy . mockReset ( ) ;
28+ readSpy . mockReset ( ) ;
29+ outputErrorSpy . mockReset ( ) ;
30+ } ) ;
31+
1732 test ( 'allow to specify overwrite for specific output (should write file)' , async ( ) => {
1833 const filename = 'overwrite.ts' ;
19- const writeSpy = vi . spyOn ( fs , 'writeFile' ) . mockImplementation ( ( ) => Promise . resolve ( ) ) ;
34+ writeSpy . mockImplementation ( ( ) => Promise . resolve ( ) ) ;
2035
2136 const output = await generate (
2237 {
@@ -42,10 +57,8 @@ describe('generate-and-save', () => {
4257
4358 test ( 'allow to specify overwrite for specific output (should not write file)' , async ( ) => {
4459 const filename = 'overwrite.ts' ;
45- const writeSpy = vi . spyOn ( fs , 'writeFile' ) . mockImplementation ( ( ) => Promise . resolve ( ) ) ;
46- // forces file to exist
47- const fileReadSpy = vi . spyOn ( fs , 'readFile' ) ;
48- fileReadSpy . mockImplementation ( async ( ) => '' ) ;
60+ writeSpy . mockImplementation ( ( ) => Promise . resolve ( ) ) ;
61+ readSpy . mockImplementation ( async ( ) => '' ) ; // forces file to exist
4962
5063 const output = await generate (
5164 {
@@ -66,14 +79,14 @@ describe('generate-and-save', () => {
6679
6780 expect ( output . length ) . toBe ( 1 ) ;
6881 // makes sure it checks if file is there
69- expect ( fileReadSpy ) . toHaveBeenCalledWith ( filename ) ;
82+ expect ( readSpy ) . toHaveBeenCalledWith ( filename ) ;
7083 // makes sure it doesn't write a new file
7184 expect ( writeSpy ) . not . toHaveBeenCalled ( ) ;
7285 } ) ;
7386
7487 test ( 'should use global overwrite option and write a file' , async ( ) => {
7588 const filename = 'overwrite.ts' ;
76- const writeSpy = vi . spyOn ( fs , 'writeFile' ) . mockImplementation ( ( ) => Promise . resolve ( ) ) ;
89+ writeSpy . mockImplementation ( ( ) => Promise . resolve ( ) ) ;
7790
7891 const output = await generate (
7992 {
@@ -98,10 +111,8 @@ describe('generate-and-save', () => {
98111
99112 test ( 'should use global overwrite option and not write a file' , async ( ) => {
100113 const filename = 'overwrite.ts' ;
101- const writeSpy = vi . spyOn ( fs , 'writeFile' ) . mockImplementation ( ( ) => Promise . resolve ( ) ) ;
102- // forces file to exist
103- const fileReadSpy = vi . spyOn ( fs , 'readFile' ) ;
104- fileReadSpy . mockImplementation ( async ( ) => '' ) ;
114+ writeSpy . mockImplementation ( ( ) => Promise . resolve ( ) ) ;
115+ readSpy . mockImplementation ( async ( ) => '' ) ; // forces file to exist
105116
106117 const output = await generate (
107118 {
@@ -121,15 +132,15 @@ describe('generate-and-save', () => {
121132
122133 expect ( output . length ) . toBe ( 1 ) ;
123134 // makes sure it checks if file is there
124- expect ( fileReadSpy ) . toHaveBeenCalledWith ( filename ) ;
135+ expect ( readSpy ) . toHaveBeenCalledWith ( filename ) ;
125136 // makes sure it doesn't write a new file
126137 expect ( writeSpy ) . not . toHaveBeenCalled ( ) ;
127138 } ) ;
128139
129140 test ( 'should overwrite a file by default' , async ( ) => {
130141 const filename = 'overwrite.ts' ;
131- const writeSpy = vi . spyOn ( fs , 'writeFile' ) . mockImplementation ( ( ) => Promise . resolve ( ) ) ;
132- const readSpy = vi . spyOn ( fs , 'readFile' ) . mockImplementation ( ( ) => Promise . resolve ( '' ) ) ;
142+ writeSpy . mockImplementation ( ( ) => Promise . resolve ( ) ) ;
143+ readSpy . mockImplementation ( ( ) => Promise . resolve ( '' ) ) ;
133144 readSpy . mockImplementation ( async _f => '' ) ;
134145
135146 const output = await generate (
@@ -189,11 +200,11 @@ describe('generate-and-save', () => {
189200 } ) ;
190201 test ( 'should extract a document from the gql tag (imported from apollo-server)' , async ( ) => {
191202 const filename = 'overwrite.ts' ;
192- const writeSpy = vi . spyOn ( fs , 'writeFile' ) . mockImplementation ( ( ) => Promise . resolve ( ) ) ;
203+ writeSpy . mockImplementation ( ( ) => Promise . resolve ( ) ) ;
193204
194205 const output = await generate (
195206 {
196- schema : ` ./tests/test-files/schema-dir/gatsby-and-custom-parsers/apollo-server.ts` ,
207+ schema : ' ./tests/test-files/schema-dir/gatsby-and-custom-parsers/apollo-server.ts' ,
197208 generates : {
198209 [ filename ] : {
199210 plugins : [ 'typescript' ] ,
@@ -210,7 +221,7 @@ describe('generate-and-save', () => {
210221 } ) ;
211222 test ( 'should allow to alter the content with the beforeOneFileWrite hook' , async ( ) => {
212223 const filename = 'modify.ts' ;
213- const writeSpy = vi . spyOn ( fs , 'writeFile' ) . mockImplementation ( ( ) => Promise . resolve ( ) ) ;
224+ writeSpy . mockImplementation ( ( ) => Promise . resolve ( ) ) ;
214225
215226 const output = await generate (
216227 {
@@ -249,7 +260,7 @@ describe('generate-and-save', () => {
249260
250261 test ( 'Schema syntax error - should print native GraphQLError' , async ( ) => {
251262 expect . assertions ( 1 ) ;
252- const outputErrorMock = vi . spyOn ( process . stderr , 'write' ) . mockImplementation ( ( ) => true ) ;
263+ outputErrorSpy . mockImplementation ( ( ) => true ) ;
253264 try {
254265 await generate (
255266 {
@@ -265,7 +276,7 @@ describe('generate-and-save', () => {
265276 ) ;
266277 } catch {
267278 const cwd = process . cwd ( ) ; // cwd is different for every machine, remember to replace local path with this after updating snapshot
268- expect ( outputErrorMock . mock . calls [ 0 ] [ 0 ] ) . toMatchInlineSnapshot ( `
279+ expect ( outputErrorSpy . mock . calls [ 0 ] [ 0 ] ) . toMatchInlineSnapshot ( `
269280 "[FAILED] Failed to load schema from ./tests/test-files/schema-dir/error-schema.graphql:
270281 [FAILED] Syntax Error: Expected Name, found "!".
271282
@@ -292,7 +303,7 @@ describe('generate-and-save', () => {
292303
293304 test ( 'Document syntax error - should print native GraphQLError' , async ( ) => {
294305 expect . assertions ( 1 ) ;
295- const outputErrorMock = vi . spyOn ( process . stderr , 'write' ) . mockImplementation ( ( ) => true ) ;
306+ outputErrorSpy . mockImplementation ( ( ) => true ) ;
296307 try {
297308 await generate (
298309 {
@@ -309,7 +320,7 @@ describe('generate-and-save', () => {
309320 ) ;
310321 } catch {
311322 const cwd = process . cwd ( ) ; // cwd is different for every machine, remember to replace local path with this after updating snapshot
312- expect ( outputErrorMock . mock . calls [ 0 ] [ 0 ] ) . toMatchInlineSnapshot ( `
323+ expect ( outputErrorSpy . mock . calls [ 0 ] [ 0 ] ) . toMatchInlineSnapshot ( `
313324 "[FAILED] Failed to load documents from ./tests/test-files/error-document.graphql:
314325 [FAILED] Syntax Error: Expected "{", found <EOF>.
315326
@@ -324,7 +335,7 @@ describe('generate-and-save', () => {
324335
325336 test ( 'No documents found - should throw error by default' , async ( ) => {
326337 expect . assertions ( 1 ) ;
327- const outputErrorMock = vi . spyOn ( process . stderr , 'write' ) . mockImplementation ( ( ) => true ) ;
338+ outputErrorSpy . mockImplementation ( ( ) => true ) ;
328339 try {
329340 await generate (
330341 {
@@ -340,7 +351,7 @@ describe('generate-and-save', () => {
340351 false
341352 ) ;
342353 } catch {
343- expect ( outputErrorMock . mock . calls [ 0 ] [ 0 ] ) . toMatchInlineSnapshot ( `
354+ expect ( outputErrorSpy . mock . calls [ 0 ] [ 0 ] ) . toMatchInlineSnapshot ( `
344355 "
345356 [FAILED] Unable to find any GraphQL type definitions for the following pointers:
346357 [FAILED] - ./tests/test-files/document-file-does-not-exist.graphql
@@ -350,7 +361,7 @@ describe('generate-and-save', () => {
350361 } ) ;
351362
352363 test ( 'No documents found - should not fail if ignoreNoDocuments=true' , async ( ) => {
353- const outputErrorMock = vi . spyOn ( process . stderr , 'write' ) . mockImplementation ( ( ) => true ) ;
364+ outputErrorSpy . mockImplementation ( ( ) => true ) ;
354365 await generate (
355366 {
356367 verbose : true ,
@@ -365,11 +376,11 @@ describe('generate-and-save', () => {
365376 } ,
366377 false
367378 ) ;
368- expect ( outputErrorMock ) . not . toHaveBeenCalled ( ) ;
379+ expect ( outputErrorSpy ) . not . toHaveBeenCalled ( ) ;
369380 } ) ;
370381
371382 test ( 'No documents found - GraphQL Config - should throw error by default' , async ( ) => {
372- const outputErrorMock = vi . spyOn ( process . stderr , 'write' ) . mockImplementation ( ( ) => true ) ;
383+ outputErrorSpy . mockImplementation ( ( ) => true ) ;
373384 expect . assertions ( 1 ) ;
374385 try {
375386 const config = await createContext ( {
@@ -385,7 +396,7 @@ describe('generate-and-save', () => {
385396
386397 await generate ( config , false ) ;
387398 } catch {
388- expect ( outputErrorMock . mock . calls [ 0 ] [ 0 ] ) . toMatchInlineSnapshot ( `
399+ expect ( outputErrorSpy . mock . calls [ 0 ] [ 0 ] ) . toMatchInlineSnapshot ( `
389400 "
390401 [FAILED] Unable to find any GraphQL type definitions for the following pointers:
391402 [FAILED] - ../test-documents/empty.graphql
@@ -395,7 +406,7 @@ describe('generate-and-save', () => {
395406 } ) ;
396407
397408 test ( 'No documents found - GraphQL Config - should not fail if ignoreNoDocuments=true' , async ( ) => {
398- const outputErrorMock = vi . spyOn ( process . stderr , 'write' ) . mockImplementation ( ( ) => true ) ;
409+ outputErrorSpy . mockImplementation ( ( ) => true ) ;
399410 vi . spyOn ( fs , 'writeFile' ) . mockImplementation ( ( ) => Promise . resolve ( ) ) ;
400411 const config = await createContext ( {
401412 config : './tests/test-files/graphql.config.no-doc-ignored.js' ,
@@ -410,7 +421,7 @@ describe('generate-and-save', () => {
410421
411422 await generate ( config , false ) ;
412423
413- expect ( outputErrorMock ) . not . toHaveBeenCalled ( ) ;
424+ expect ( outputErrorSpy ) . not . toHaveBeenCalled ( ) ;
414425 } ) ;
415426 } ) ;
416427
0 commit comments