Skip to content

Commit 29f4fd3

Browse files
authored
fix failed tests from MessageProcessor.spec.ts on MacOS (#3727)
1 parent 7404e8e commit 29f4fd3

File tree

1 file changed

+27
-28
lines changed

1 file changed

+27
-28
lines changed

packages/graphql-language-service-server/src/__tests__/MessageProcessor.spec.ts

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
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';
15
import mockfs from 'mock-fs';
2-
import { join } from 'node:path';
36
import { MockFile, MockProject } from './__utils__/MockProject';
4-
// import { readFileSync } from 'node:fs';
57
import { FileChangeType } from 'vscode-languageserver';
68
import { serializeRange } from './__utils__/utils';
7-
import { readFile } from 'node:fs/promises';
8-
import { existsSync } from 'node:fs';
99
import { URI } from 'vscode-uri';
1010
import {
1111
GraphQLSchema,
@@ -64,8 +64,14 @@ const fooInlineTypePosition = {
6464
end: { line: 5, character: 24 },
6565
};
6666

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+
);
6975

7076
// TODO:
7177
// - reorganize into multiple files
@@ -76,10 +82,18 @@ const genSchemaPath =
7682
// - fix TODO comments where bugs were found that couldn't be resolved quickly (2-4hr time box)
7783

7884
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+
7992
afterEach(() => {
8093
mockfs.restore();
8194
fetchMock.restore();
8295
});
96+
8397
it('fails to initialize with empty config file', async () => {
8498
const project = new MockProject({
8599
files: [...defaultFiles, ['graphql.config.json', '']],
@@ -97,6 +111,7 @@ describe('MessageProcessor with no config', () => {
97111
expect(project.lsp._isGraphQLConfigMissing).toEqual(true);
98112
project.lsp.handleShutdownRequest();
99113
});
114+
100115
it('fails to initialize with no config file present', async () => {
101116
const project = new MockProject({
102117
files: [...defaultFiles],
@@ -113,6 +128,7 @@ describe('MessageProcessor with no config', () => {
113128
expect(project.lsp._isGraphQLConfigMissing).toEqual(true);
114129
project.lsp.handleShutdownRequest();
115130
});
131+
116132
it('initializes when presented with a valid config later', async () => {
117133
const project = new MockProject({
118134
files: [...defaultFiles],
@@ -144,13 +160,7 @@ describe('MessageProcessor with config', () => {
144160
mockfs.restore();
145161
fetchMock.restore();
146162
});
147-
// beforeAll(async () => {
148-
// app = await import('../../../graphiql/test/e2e-server');
149-
// });
150-
// afterAll(() => {
151-
// app.server.close();
152-
// app.wsServer.close();
153-
// });
163+
154164
it('caches files and schema with .graphql file config, and the schema updates with watched file changes', async () => {
155165
const project = new MockProject({
156166
files: [
@@ -250,14 +260,7 @@ describe('MessageProcessor with config', () => {
250260
// now Foo has a bad field, the fragment should be invalid
251261
'type Query { foo: Foo, test: Test }\n\n type Test { test: String }\n\n\n\n\n\ntype Foo { bad: Int }',
252262
);
253-
// await project.lsp.handleWatchedFilesChangedNotification({
254-
// changes: [
255-
// {
256-
// type: FileChangeType.Changed,
257-
// uri: project.uri('schema.graphql'),
258-
// },
259-
// ],
260-
// });
263+
261264
await project.lsp.handleDidChangeNotification({
262265
contentChanges: [
263266
{
@@ -290,7 +293,7 @@ describe('MessageProcessor with config', () => {
290293
expect(result.diagnostics[0].message).toEqual(
291294
'Cannot query field "bar" on type "Foo". Did you mean "bad"?',
292295
);
293-
const generatedFile = existsSync(join(genSchemaPath));
296+
const generatedFile = existsSync(genSchemaPath);
294297
// this generated file should not exist because the schema is local!
295298
expect(generatedFile).toEqual(false);
296299
// simulating codegen
@@ -376,7 +379,6 @@ describe('MessageProcessor with config', () => {
376379
],
377380
],
378381
});
379-
380382
const initParams = await project.init('query.graphql');
381383
expect(project.lsp._logger.error).not.toHaveBeenCalled();
382384

@@ -392,7 +394,7 @@ describe('MessageProcessor with config', () => {
392394
expect(await project.lsp._graphQLCache.getSchema('default')).toBeDefined();
393395

394396
// schema file is present and contains schema
395-
const file = await readFile(join(genSchemaPath), { encoding: 'utf-8' });
397+
const file = await readFile(genSchemaPath, 'utf8');
396398
expect(file.split('\n').length).toBeGreaterThan(10);
397399

398400
// hover works
@@ -426,7 +428,6 @@ describe('MessageProcessor with config', () => {
426428
textDocument: { uri: project.uri('fragments.graphql') },
427429
position: { character: 15, line: 0 },
428430
});
429-
430431
expect(typeDefinitions[0].uri).toEqual(URI.parse(genSchemaPath).toString());
431432

432433
expect(serializeRange(typeDefinitions[0].range)).toEqual({
@@ -560,9 +561,7 @@ describe('MessageProcessor with config', () => {
560561
(await project.lsp._graphQLCache.getSchema('a')).getType('example100'),
561562
).toBeTruthy();
562563
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');
566565
expect(file).toContain('example100');
567566
// add a new typescript file with empty query to the b project
568567
// and expect autocomplete to only show options for project b

0 commit comments

Comments
 (0)