|
| 1 | +import { afterAll, afterEach, beforeAll, describe, expect, it } from 'vitest'; |
| 2 | +import { createServer, UserConfig, ViteDevServer } from 'vite'; |
| 3 | +import { promises as fs } from 'node:fs'; |
| 4 | +import codegen from '../../src/index'; |
| 5 | + |
| 6 | +const TEST_PATH = './test/match-on-glob-schema' as const; |
| 7 | +const DIR_PATH_1 = `${TEST_PATH}/dir-1` as const; |
| 8 | +const DIR_PATH_2 = `${TEST_PATH}/dir-2` as const; |
| 9 | +const SCHEMA_PATH_1 = `${DIR_PATH_1}/schema-1.graphql` as const; |
| 10 | +const SCHEMA_PATH_2 = `${DIR_PATH_1}/schema-2.graphql` as const; |
| 11 | +const SCHEMA_PATH_3 = `${DIR_PATH_2}/schema-1.graphql` as const; |
| 12 | +const SCHEMA_PATH_4 = `${DIR_PATH_2}/schema-2.graphql` as const; |
| 13 | +const DOCUMENT_PATH = `${TEST_PATH}/graphql` as const; |
| 14 | +const OUTPUT_PATH = `${TEST_PATH}/generated` as const; |
| 15 | +const OUTPUT_FILE_NAME = 'graphql.ts' as const; |
| 16 | +const OUTPUT_FILE = `${OUTPUT_PATH}/${OUTPUT_FILE_NAME}` as const; |
| 17 | + |
| 18 | +const viteConfig = { |
| 19 | + plugins: [ |
| 20 | + codegen({ |
| 21 | + runOnStart: false, |
| 22 | + matchOnDocuments: false, |
| 23 | + matchOnSchemas: true, |
| 24 | + configFilePathOverride: `${TEST_PATH}/codegen.yml`, |
| 25 | + }), |
| 26 | + ], |
| 27 | +} satisfies UserConfig; |
| 28 | + |
| 29 | +describe('match-on-glob-schema', () => { |
| 30 | + let viteServer: ViteDevServer | null = null; |
| 31 | + |
| 32 | + const isFileGenerated = async (): Promise<boolean> => { |
| 33 | + try { |
| 34 | + await fs.access(OUTPUT_FILE); |
| 35 | + return true; |
| 36 | + } catch (error) { |
| 37 | + // ignore |
| 38 | + } |
| 39 | + |
| 40 | + return new Promise((resolve, reject) => { |
| 41 | + if (!viteServer) reject('Vite server not started'); |
| 42 | + |
| 43 | + viteServer?.watcher.on('add', (path: string) => { |
| 44 | + if (path.includes(OUTPUT_FILE_NAME)) resolve(true); |
| 45 | + }); |
| 46 | + |
| 47 | + setTimeout(() => reject('Generated file not found'), 5000); |
| 48 | + }); |
| 49 | + }; |
| 50 | + |
| 51 | + beforeAll(async () => { |
| 52 | + // Files in dir1 |
| 53 | + await fs.mkdir(DIR_PATH_1, { recursive: true }); |
| 54 | + await fs.writeFile(SCHEMA_PATH_1, 'type Query { foo: String }'); |
| 55 | + await fs.writeFile(SCHEMA_PATH_2, 'type Query { bar: String }'); |
| 56 | + |
| 57 | + // Files in dir2 |
| 58 | + await fs.mkdir(DIR_PATH_2, { recursive: true }); |
| 59 | + await fs.writeFile(SCHEMA_PATH_3, 'type Query { baz: String }'); |
| 60 | + await fs.writeFile(SCHEMA_PATH_4, 'type Query { qux: String }'); |
| 61 | + |
| 62 | + await fs.mkdir(DOCUMENT_PATH, { recursive: true }); |
| 63 | + await fs.writeFile(`${DOCUMENT_PATH}/Foo.graphql`, 'query Foo { foo }'); |
| 64 | + viteServer = await createServer(viteConfig).then((s) => s.listen()); |
| 65 | + }); |
| 66 | + |
| 67 | + afterAll(async () => { |
| 68 | + await viteServer?.close(); |
| 69 | + viteServer = null; |
| 70 | + await fs.rm(DIR_PATH_1, { recursive: true }); |
| 71 | + await fs.rm(DIR_PATH_2, { recursive: true }); |
| 72 | + |
| 73 | + await fs.rm(DOCUMENT_PATH, { recursive: true }); |
| 74 | + }); |
| 75 | + |
| 76 | + afterEach(async () => { |
| 77 | + await fs.rm(OUTPUT_PATH, { recursive: true }); |
| 78 | + }); |
| 79 | + |
| 80 | + it('generates on schema change', async () => { |
| 81 | + // Files in dir1 |
| 82 | + await fs.writeFile(SCHEMA_PATH_1, 'type Query { foo: Int }'); |
| 83 | + await fs.writeFile(SCHEMA_PATH_2, 'type Query { bar: Int }'); |
| 84 | + |
| 85 | + // Files in dir2 |
| 86 | + await fs.writeFile(SCHEMA_PATH_3, 'type Query { baz: Int }'); |
| 87 | + await fs.writeFile(SCHEMA_PATH_4, 'type Query { qux: Int }'); |
| 88 | + |
| 89 | + await isFileGenerated(); |
| 90 | + |
| 91 | + const file = await fs.readFile(OUTPUT_FILE, 'utf-8'); |
| 92 | + |
| 93 | + expect(file).toMatchSnapshot(); |
| 94 | + }); |
| 95 | +}); |
0 commit comments