Mirror: TypeScript LSP plugin that finds GraphQL documents in your code and provides diagnostics, auto-complete and hover-information.
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Add e2e test for renaming an operation name (#43)

* Add e2e test for rename operation

* Update test/e2e/rename.test.ts

Co-authored-by: Jovi De Croock <decroockjovi@gmail.com>

* Poll based expects instead of TSServer response

---------

Co-authored-by: Jovi De Croock <decroockjovi@gmail.com>

authored by

Daniel
Jovi De Croock
and committed by
GitHub
c7924015 89a43ac3

+125
+9
test/e2e/fixture-project/fixtures/rename.ts
··· 1 + import { gql } from '@urql/core'; 2 + 3 + const PostsQuery = gql` 4 + query Posts { 5 + posts { 6 + title 7 + } 8 + } 9 + `;
+83
test/e2e/rename.test.ts
··· 1 + import { expect, afterAll, beforeAll, it, describe } from 'vitest'; 2 + import { TSServer } from './server'; 3 + import path from 'node:path'; 4 + import fs from 'node:fs'; 5 + import url from 'node:url'; 6 + import ts from 'typescript/lib/tsserverlibrary'; 7 + import { waitForExpect } from './util'; 8 + 9 + const __dirname = path.dirname(url.fileURLToPath(import.meta.url)); 10 + 11 + const projectPath = path.resolve(__dirname, 'fixture-project'); 12 + describe('Operation name', () => { 13 + const outFile = path.join(projectPath, 'rename.ts'); 14 + const genFile = path.join(projectPath, 'rename.generated.ts'); 15 + 16 + let server: TSServer; 17 + beforeAll(async () => { 18 + server = new TSServer(projectPath, { debugLog: false }); 19 + }); 20 + 21 + afterAll(() => { 22 + try { 23 + fs.unlinkSync(outFile); 24 + fs.unlinkSync(genFile); 25 + } catch {} 26 + }); 27 + 28 + it('gets renamed correctly', async () => { 29 + server.sendCommand('open', { 30 + file: outFile, 31 + fileContent: '// empty', 32 + scriptKindName: 'TS', 33 + } satisfies ts.server.protocol.OpenRequestArgs); 34 + 35 + server.sendCommand('updateOpen', { 36 + openFiles: [ 37 + { 38 + file: outFile, 39 + fileContent: fs.readFileSync( 40 + path.join(projectPath, 'fixtures/rename.ts'), 41 + 'utf-8' 42 + ), 43 + }, 44 + ], 45 + } satisfies ts.server.protocol.UpdateOpenRequestArgs); 46 + 47 + server.sendCommand('saveto', { 48 + file: outFile, 49 + tmpfile: outFile, 50 + } satisfies ts.server.protocol.SavetoRequestArgs); 51 + 52 + await waitForExpect(() => { 53 + expect(fs.readFileSync(outFile, 'utf-8')).toContain( 54 + `as typeof import('./rename.generated').PostsDocument` 55 + ); 56 + }); 57 + 58 + server.sendCommand('updateOpen', { 59 + openFiles: [ 60 + { 61 + file: outFile, 62 + fileContent: fs 63 + .readFileSync(outFile, 'utf-8') 64 + .replace('query Posts', 'query PostList'), 65 + }, 66 + ], 67 + } satisfies ts.server.protocol.UpdateOpenRequestArgs); 68 + 69 + server.sendCommand('saveto', { 70 + file: outFile, 71 + tmpfile: outFile, 72 + } satisfies ts.server.protocol.SavetoRequestArgs); 73 + 74 + await waitForExpect(() => { 75 + expect(fs.readFileSync(outFile, 'utf-8')).toContain( 76 + `as typeof import('./rename.generated').PostListDocument` 77 + ); 78 + expect(fs.readFileSync(genFile, 'utf-8')).toContain( 79 + 'export const PostListDocument =' 80 + ); 81 + }); 82 + }, 12500); 83 + });
+33
test/e2e/util.ts
··· 1 + import { vi } from 'vitest'; 2 + 3 + type WaitForExpectOptions = { 4 + timeout?: number; 5 + interval?: number; 6 + }; 7 + 8 + export const waitForExpect = async ( 9 + expectFn: () => void, 10 + { interval = 200, timeout = 10000 }: WaitForExpectOptions = {} 11 + ) => { 12 + // @sinonjs/fake-timers injects `clock` property into setTimeout 13 + const usesFakeTimers = 'clock' in setTimeout; 14 + 15 + if (usesFakeTimers) vi.useRealTimers(); 16 + 17 + const start = Date.now(); 18 + 19 + while (true) { 20 + try { 21 + expectFn(); 22 + break; 23 + } catch {} 24 + 25 + if (Date.now() - start > timeout) { 26 + throw new Error('Timeout'); 27 + } 28 + 29 + await new Promise(resolve => setTimeout(resolve, interval)); 30 + } 31 + 32 + if (usesFakeTimers) vi.useFakeTimers(); 33 + };