|
| 1 | +/** |
| 2 | + * E2E test for file watcher with external clientDir |
| 3 | + * |
| 4 | + * This test verifies that when clientDir is set to a path outside the project root |
| 5 | + * (e.g., ../../apps/ecommerce/app/graphql), the file watcher: |
| 6 | + * - Correctly watches the external directory |
| 7 | + * - Regenerates client types when .graphql files change in that directory |
| 8 | + */ |
| 9 | +import type { Nitro } from 'nitro/types' |
| 10 | +import { existsSync, readFileSync, rmSync, writeFileSync } from 'node:fs' |
| 11 | +import { build, createNitro, prepare } from 'nitro/builder' |
| 12 | +import { join, resolve } from 'pathe' |
| 13 | +import { afterAll, beforeAll, describe, expect, it } from 'vitest' |
| 14 | +import graphql from '../../src' |
| 15 | + |
| 16 | +const fixturesDir = resolve(__dirname, '../fixtures') |
| 17 | +const projectDir = resolve(fixturesDir, 'nitro-relative-parent/packages/graphql') |
| 18 | +const externalClientDir = resolve(fixturesDir, 'nitro-relative-parent/apps/ecommerce/app/graphql') |
| 19 | +const clientQueryPath = join(externalClientDir, 'queries.graphql') |
| 20 | + |
| 21 | +// Note: nitro-graphql uses .graphql as the output directory for types (nitro.graphql.buildDir) |
| 22 | +const graphqlBuildDir = join(projectDir, '.graphql') |
| 23 | +const clientTypesPath = join(graphqlBuildDir, 'nitro-graphql-client.d.ts') |
| 24 | +const serverTypesPath = join(graphqlBuildDir, 'nitro-graphql-server.d.ts') |
| 25 | + |
| 26 | +// Initial client query |
| 27 | +const initialQuery = `query GetUser($id: ID!) { |
| 28 | + user(id: $id) { |
| 29 | + id |
| 30 | + name |
| 31 | + } |
| 32 | +} |
| 33 | +
|
| 34 | +query GetHello { |
| 35 | + hello |
| 36 | +} |
| 37 | +` |
| 38 | + |
| 39 | +// Updated client query with new GetUserDetails query |
| 40 | +const updatedQuery = `query GetUser($id: ID!) { |
| 41 | + user(id: $id) { |
| 42 | + id |
| 43 | + name |
| 44 | + } |
| 45 | +} |
| 46 | +
|
| 47 | +query GetHello { |
| 48 | + hello |
| 49 | +} |
| 50 | +
|
| 51 | +query GetUserDetails($id: ID!) { |
| 52 | + user(id: $id) { |
| 53 | + id |
| 54 | + name |
| 55 | + } |
| 56 | + hello |
| 57 | +} |
| 58 | +` |
| 59 | + |
| 60 | +// Clean up generated files |
| 61 | +function cleanupGeneratedFiles() { |
| 62 | + const dirsToClean = [ |
| 63 | + join(projectDir, '.nitro'), |
| 64 | + join(projectDir, '.output'), |
| 65 | + join(projectDir, '.graphql'), |
| 66 | + join(projectDir, 'node_modules/.nitro'), |
| 67 | + resolve(fixturesDir, 'nitro-relative-parent/apps/ecommerce/app/graphql/types'), |
| 68 | + resolve(fixturesDir, 'nitro-relative-parent/apps/ecommerce/app/graphql/default'), |
| 69 | + ] |
| 70 | + |
| 71 | + for (const dir of dirsToClean) { |
| 72 | + if (existsSync(dir)) { |
| 73 | + rmSync(dir, { recursive: true, force: true }) |
| 74 | + } |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +// Reset fixture files to initial state |
| 79 | +function resetFixtureFiles() { |
| 80 | + writeFileSync(clientQueryPath, initialQuery, 'utf-8') |
| 81 | +} |
| 82 | + |
| 83 | +// Helper to wait for file to be updated |
| 84 | +async function waitForFileChange( |
| 85 | + filePath: string, |
| 86 | + expectedContent: string, |
| 87 | + timeout = 5000, |
| 88 | +): Promise<boolean> { |
| 89 | + const startTime = Date.now() |
| 90 | + |
| 91 | + while (Date.now() - startTime < timeout) { |
| 92 | + if (existsSync(filePath)) { |
| 93 | + const content = readFileSync(filePath, 'utf-8') |
| 94 | + if (content.includes(expectedContent)) { |
| 95 | + return true |
| 96 | + } |
| 97 | + } |
| 98 | + await new Promise(r => setTimeout(r, 100)) |
| 99 | + } |
| 100 | + |
| 101 | + return false |
| 102 | +} |
| 103 | + |
| 104 | +describe('external clientDir File Watcher E2E', () => { |
| 105 | + let nitro: Nitro |
| 106 | + let initialServerTypesContent: string |
| 107 | + |
| 108 | + beforeAll(async () => { |
| 109 | + cleanupGeneratedFiles() |
| 110 | + resetFixtureFiles() |
| 111 | + |
| 112 | + // Create Nitro with dev mode and external clientDir |
| 113 | + nitro = await createNitro({ |
| 114 | + rootDir: projectDir, |
| 115 | + dev: true, // This enables file watching |
| 116 | + modules: [ |
| 117 | + graphql({ |
| 118 | + framework: 'graphql-yoga', |
| 119 | + clientDir: '../../apps/ecommerce/app/graphql', |
| 120 | + }), |
| 121 | + ], |
| 122 | + }) |
| 123 | + |
| 124 | + await prepare(nitro) |
| 125 | + await build(nitro) |
| 126 | + |
| 127 | + // Wait for initial type generation |
| 128 | + await new Promise(r => setTimeout(r, 500)) |
| 129 | + |
| 130 | + // Store initial server types content for comparison |
| 131 | + if (existsSync(serverTypesPath)) { |
| 132 | + initialServerTypesContent = readFileSync(serverTypesPath, 'utf-8') |
| 133 | + } |
| 134 | + }, 60000) |
| 135 | + |
| 136 | + afterAll(async () => { |
| 137 | + await nitro?.close() |
| 138 | + cleanupGeneratedFiles() |
| 139 | + resetFixtureFiles() |
| 140 | + }) |
| 141 | + |
| 142 | + it('should have generated initial types from external clientDir', () => { |
| 143 | + expect(existsSync(clientTypesPath)).toBe(true) |
| 144 | + expect(existsSync(serverTypesPath)).toBe(true) |
| 145 | + |
| 146 | + const clientContent = readFileSync(clientTypesPath, 'utf-8') |
| 147 | + expect(clientContent).toContain('GetUser') |
| 148 | + expect(clientContent).toContain('GetHello') |
| 149 | + // Should NOT contain GetUserDetails yet |
| 150 | + expect(clientContent).not.toContain('GetUserDetails') |
| 151 | + }) |
| 152 | + |
| 153 | + it('should regenerate client types when external clientDir .graphql file changes', async () => { |
| 154 | + // Verify initial state - no GetUserDetails |
| 155 | + const initialContent = readFileSync(clientTypesPath, 'utf-8') |
| 156 | + expect(initialContent).not.toContain('GetUserDetails') |
| 157 | + |
| 158 | + // Modify the query file in the external clientDir |
| 159 | + writeFileSync(clientQueryPath, updatedQuery, 'utf-8') |
| 160 | + |
| 161 | + // Wait for file watcher to detect change and regenerate types |
| 162 | + // File watcher has 150ms debounce + processing time |
| 163 | + const found = await waitForFileChange(clientTypesPath, 'GetUserDetails', 5000) |
| 164 | + |
| 165 | + expect(found).toBe(true) |
| 166 | + |
| 167 | + // Verify the new query type is present |
| 168 | + const updatedContent = readFileSync(clientTypesPath, 'utf-8') |
| 169 | + expect(updatedContent).toContain('GetUserDetails') |
| 170 | + expect(updatedContent).toContain('GetUser') |
| 171 | + expect(updatedContent).toContain('GetHello') |
| 172 | + }, 10000) |
| 173 | + |
| 174 | + it('should NOT trigger server type regeneration for client-only changes', () => { |
| 175 | + // Server types should remain unchanged when only client files change |
| 176 | + const currentServerContent = readFileSync(serverTypesPath, 'utf-8') |
| 177 | + |
| 178 | + // The content should be the same (server types shouldn't have been regenerated) |
| 179 | + // Note: We're comparing content, not timestamps, because content is what matters |
| 180 | + expect(currentServerContent).toContain('Query') |
| 181 | + expect(currentServerContent).toContain('User') |
| 182 | + |
| 183 | + // Verify the structure is the same as initial |
| 184 | + expect(currentServerContent).toBe(initialServerTypesContent) |
| 185 | + }) |
| 186 | +}) |
0 commit comments