-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathwatcher.run.spec.ts
More file actions
217 lines (193 loc) · 5.84 KB
/
watcher.run.spec.ts
File metadata and controls
217 lines (193 loc) · 5.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import * as path from 'path';
import { mkdtempSync, mkdirSync, writeFileSync } from 'fs';
import { createWatcher } from '../src/utils/watcher.js';
import { CodegenContext } from '../src/config.js';
/**
* waitForNextEvent
* @description This function waits for a short amount of time to let async things run
* e.g. watcher subscription setup, watcher to react to change/create events, etc.
*/
const waitForNextEvent = async () => {
return await new Promise(resolve => setTimeout(resolve, 500));
};
type TestFilePaths = { absolute: string; relative: string };
const setupTestFiles = (): { testDir: string; schemaFile: TestFilePaths; documentFile: TestFilePaths } => {
const tempDir = path.join(__dirname, '..', 'temp');
mkdirSync(tempDir, { recursive: true });
const testDir = mkdtempSync(path.join(tempDir, 'watcher-run-spec-'));
const schemaFileAbsolute = path.join(testDir, 'schema.graphql');
const schemaFile = {
absolute: schemaFileAbsolute,
relative: path.relative(process.cwd(), schemaFileAbsolute),
};
const documentFileAbsolute = path.join(testDir, 'document.graphql');
const documentFile = {
absolute: documentFileAbsolute,
relative: path.relative(process.cwd(), documentFileAbsolute),
};
return {
testDir,
schemaFile,
documentFile,
};
};
const setupMockWatcher = async (codegenContext: ConstructorParameters<typeof CodegenContext>[0]) => {
const onNextMock = vi.fn().mockResolvedValue([]);
const { stopWatching } = createWatcher(new CodegenContext(codegenContext), onNextMock);
// After creating watcher, wait for a tick for subscription to be completely set up
await waitForNextEvent();
return { stopWatching, onNextMock };
};
describe('Watch runs', () => {
test('calls onNext correctly on initial runs and subsequent runs', async () => {
const { testDir, schemaFile, documentFile } = setupTestFiles();
writeFileSync(
schemaFile.absolute,
/* GraphQL */ `
type Query {
me: User
}
type User {
id: ID!
name: String!
}
`
);
writeFileSync(
documentFile.absolute,
/* GraphQL */ `
query {
me {
id
}
}
`
);
const { stopWatching, onNextMock } = await setupMockWatcher({
filepath: path.join(testDir, 'codegen.ts'),
config: {
schema: schemaFile.relative,
documents: documentFile.relative,
generates: {
[path.join(testDir, 'types.ts')]: {
plugins: ['typescript'],
},
},
},
});
// 1. Initial setup: onNext in initial run should be called because no errors
expect(onNextMock).toHaveBeenCalledTimes(1);
// 2. Subsequent run 1: correct document file, so `onNext` is called again because no errors
writeFileSync(
documentFile.absolute,
/* GraphQL */ `
query {
me {
id
name
}
}
`
);
await waitForNextEvent();
expect(onNextMock).toHaveBeenCalledTimes(2);
// 3. Subsequent run 2: incorrect document file, so `onNext` is NOT called
writeFileSync(
documentFile.absolute,
/* GraphQL */ `
query {
me {
id
name
zzzz # should throw error
}
}
`
);
await waitForNextEvent();
expect(onNextMock).toHaveBeenCalledTimes(2);
await stopWatching();
});
test('only re-runs generates processes based on watched path', async () => {
const { testDir, schemaFile, documentFile } = setupTestFiles();
writeFileSync(
schemaFile.absolute,
/* GraphQL */ `
type Query {
me: User
}
type User {
id: ID!
name: String!
}
`
);
writeFileSync(
documentFile.absolute,
/* GraphQL */ `
query {
me {
id
}
}
`
);
const generatesKey1 = path.join(testDir, 'types-1.ts');
const generatesKey2 = path.join(testDir, 'types-2.ts');
const { stopWatching, onNextMock } = await setupMockWatcher({
filepath: path.join(testDir, 'codegen.ts'),
config: {
schema: schemaFile.relative,
generates: {
[generatesKey1]: {
plugins: ['typescript'],
},
[generatesKey2]: {
documents: documentFile.relative, // When this file is changed, only this block will be re-generated
plugins: ['typescript'],
},
},
},
});
// 1. Initial setup: onNext in initial run should be called successfully with 2 files,
// because there are no errors
expect(onNextMock.mock.calls[0][0].length).toBe(2);
expect(onNextMock.mock.calls[0][0][0].filename).toBe(generatesKey1);
expect(onNextMock.mock.calls[0][0][1].filename).toBe(generatesKey2);
// 2. Subsequent run 1: update document file for generatesKey2,
// so only the second generates block gets triggered
writeFileSync(
documentFile.absolute,
/* GraphQL */ `
query {
me {
id
name
}
}
`
);
await waitForNextEvent();
expect(onNextMock.mock.calls[1][0].length).toBe(1);
expect(onNextMock.mock.calls[1][0][0].filename).toBe(generatesKey2);
// 2. Subsequent run 2: update schema file, so both generates block are triggered
writeFileSync(
schemaFile.absolute,
/* GraphQL */ `
type Query {
me: User
}
type User {
id: ID!
name: String!
}
scalar DateTime
`
);
await waitForNextEvent();
expect(onNextMock.mock.calls[2][0].length).toBe(2);
expect(onNextMock.mock.calls[2][0][0].filename).toBe(generatesKey1);
expect(onNextMock.mock.calls[2][0][1].filename).toBe(generatesKey2);
await stopWatching();
});
});