1
+ import { readFile , rm } from 'node:fs/promises' ;
2
+ import { existsSync } from 'node:fs' ;
3
+ import { tmpdir } from 'node:os' ;
4
+ import path from 'node:path' ;
1
5
import mockfs from 'mock-fs' ;
2
- import { join } from 'node:path' ;
3
6
import { MockFile , MockProject } from './__utils__/MockProject' ;
4
- // import { readFileSync } from 'node:fs';
5
7
import { FileChangeType } from 'vscode-languageserver' ;
6
8
import { serializeRange } from './__utils__/utils' ;
7
- import { readFile } from 'node:fs/promises' ;
8
- import { existsSync } from 'node:fs' ;
9
9
import { URI } from 'vscode-uri' ;
10
10
import {
11
11
GraphQLSchema ,
@@ -64,8 +64,14 @@ const fooInlineTypePosition = {
64
64
end : { line : 5 , character : 24 } ,
65
65
} ;
66
66
67
- const genSchemaPath =
68
- '/tmp/graphql-language-service/test/projects/default/generated-schema.graphql' ;
67
+ const genSchemaPath = path . join (
68
+ tmpdir ( ) ,
69
+ 'graphql-language-service' ,
70
+ 'test' ,
71
+ 'projects' ,
72
+ 'default' ,
73
+ 'generated-schema.graphql' ,
74
+ ) ;
69
75
70
76
// TODO:
71
77
// - reorganize into multiple files
@@ -76,10 +82,18 @@ const genSchemaPath =
76
82
// - fix TODO comments where bugs were found that couldn't be resolved quickly (2-4hr time box)
77
83
78
84
describe ( 'MessageProcessor with no config' , ( ) => {
85
+ beforeAll ( async ( ) => {
86
+ await rm ( path . join ( tmpdir ( ) , 'graphql-language-service' ) , {
87
+ recursive : true ,
88
+ force : true ,
89
+ } ) ;
90
+ } ) ;
91
+
79
92
afterEach ( ( ) => {
80
93
mockfs . restore ( ) ;
81
94
fetchMock . restore ( ) ;
82
95
} ) ;
96
+
83
97
it ( 'fails to initialize with empty config file' , async ( ) => {
84
98
const project = new MockProject ( {
85
99
files : [ ...defaultFiles , [ 'graphql.config.json' , '' ] ] ,
@@ -97,6 +111,7 @@ describe('MessageProcessor with no config', () => {
97
111
expect ( project . lsp . _isGraphQLConfigMissing ) . toEqual ( true ) ;
98
112
project . lsp . handleShutdownRequest ( ) ;
99
113
} ) ;
114
+
100
115
it ( 'fails to initialize with no config file present' , async ( ) => {
101
116
const project = new MockProject ( {
102
117
files : [ ...defaultFiles ] ,
@@ -113,6 +128,7 @@ describe('MessageProcessor with no config', () => {
113
128
expect ( project . lsp . _isGraphQLConfigMissing ) . toEqual ( true ) ;
114
129
project . lsp . handleShutdownRequest ( ) ;
115
130
} ) ;
131
+
116
132
it ( 'initializes when presented with a valid config later' , async ( ) => {
117
133
const project = new MockProject ( {
118
134
files : [ ...defaultFiles ] ,
@@ -144,13 +160,7 @@ describe('MessageProcessor with config', () => {
144
160
mockfs . restore ( ) ;
145
161
fetchMock . restore ( ) ;
146
162
} ) ;
147
- // beforeAll(async () => {
148
- // app = await import('../../../graphiql/test/e2e-server');
149
- // });
150
- // afterAll(() => {
151
- // app.server.close();
152
- // app.wsServer.close();
153
- // });
163
+
154
164
it ( 'caches files and schema with .graphql file config, and the schema updates with watched file changes' , async ( ) => {
155
165
const project = new MockProject ( {
156
166
files : [
@@ -250,14 +260,7 @@ describe('MessageProcessor with config', () => {
250
260
// now Foo has a bad field, the fragment should be invalid
251
261
'type Query { foo: Foo, test: Test }\n\n type Test { test: String }\n\n\n\n\n\ntype Foo { bad: Int }' ,
252
262
) ;
253
- // await project.lsp.handleWatchedFilesChangedNotification({
254
- // changes: [
255
- // {
256
- // type: FileChangeType.Changed,
257
- // uri: project.uri('schema.graphql'),
258
- // },
259
- // ],
260
- // });
263
+
261
264
await project . lsp . handleDidChangeNotification ( {
262
265
contentChanges : [
263
266
{
@@ -290,7 +293,7 @@ describe('MessageProcessor with config', () => {
290
293
expect ( result . diagnostics [ 0 ] . message ) . toEqual (
291
294
'Cannot query field "bar" on type "Foo". Did you mean "bad"?' ,
292
295
) ;
293
- const generatedFile = existsSync ( join ( genSchemaPath ) ) ;
296
+ const generatedFile = existsSync ( genSchemaPath ) ;
294
297
// this generated file should not exist because the schema is local!
295
298
expect ( generatedFile ) . toEqual ( false ) ;
296
299
// simulating codegen
@@ -376,7 +379,6 @@ describe('MessageProcessor with config', () => {
376
379
] ,
377
380
] ,
378
381
} ) ;
379
-
380
382
const initParams = await project . init ( 'query.graphql' ) ;
381
383
expect ( project . lsp . _logger . error ) . not . toHaveBeenCalled ( ) ;
382
384
@@ -392,7 +394,7 @@ describe('MessageProcessor with config', () => {
392
394
expect ( await project . lsp . _graphQLCache . getSchema ( 'default' ) ) . toBeDefined ( ) ;
393
395
394
396
// schema file is present and contains schema
395
- const file = await readFile ( join ( genSchemaPath ) , { encoding : 'utf-8' } ) ;
397
+ const file = await readFile ( genSchemaPath , 'utf8' ) ;
396
398
expect ( file . split ( '\n' ) . length ) . toBeGreaterThan ( 10 ) ;
397
399
398
400
// hover works
@@ -426,7 +428,6 @@ describe('MessageProcessor with config', () => {
426
428
textDocument : { uri : project . uri ( 'fragments.graphql' ) } ,
427
429
position : { character : 15 , line : 0 } ,
428
430
} ) ;
429
-
430
431
expect ( typeDefinitions [ 0 ] . uri ) . toEqual ( URI . parse ( genSchemaPath ) . toString ( ) ) ;
431
432
432
433
expect ( serializeRange ( typeDefinitions [ 0 ] . range ) ) . toEqual ( {
@@ -560,9 +561,7 @@ describe('MessageProcessor with config', () => {
560
561
( await project . lsp . _graphQLCache . getSchema ( 'a' ) ) . getType ( 'example100' ) ,
561
562
) . toBeTruthy ( ) ;
562
563
await sleep ( 1000 ) ;
563
- const file = await readFile ( join ( genSchemaPath . replace ( 'default' , 'a' ) ) , {
564
- encoding : 'utf-8' ,
565
- } ) ;
564
+ const file = await readFile ( genSchemaPath . replace ( 'default' , 'a' ) , 'utf8' ) ;
566
565
expect ( file ) . toContain ( 'example100' ) ;
567
566
// add a new typescript file with empty query to the b project
568
567
// and expect autocomplete to only show options for project b
0 commit comments