-
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathrunner.test.ts
More file actions
308 lines (293 loc) · 11.7 KB
/
runner.test.ts
File metadata and controls
308 lines (293 loc) · 11.7 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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
import { access, rm, writeFile } from 'node:fs/promises';
import { platform } from 'node:process';
import dedent from 'dedent';
import { afterEach, describe, expect, test, vi } from 'vitest';
import type { Watcher } from './runner.js';
import { runCMK, runCMKInWatchMode } from './runner.js';
import { formatDiagnostics } from './test/diagnostic.js';
import { fakeParsedArgs } from './test/faker.js';
import { createIFF } from './test/fixture.js';
import { createLoggerSpy } from './test/logger.js';
async function sleep(ms: number): Promise<void> {
// eslint-disable-next-line no-promise-executor-return
return new Promise((resolve) => setTimeout(resolve, ms));
}
describe('runCMK', () => {
test('emits .d.ts files', async () => {
const iff = await createIFF({
'tsconfig.json': dedent`
{
"cmkOptions": { "dtsOutDir": "generated" }
}
`,
'src/a.module.css': '.a_1 { color: red; }',
'src/b.module.css': '.b_1 { color: blue; }',
});
await runCMK(fakeParsedArgs({ project: iff.rootDir }), createLoggerSpy());
expect(await iff.readFile('generated/src/a.module.css.d.ts')).toMatchInlineSnapshot(`
"// @ts-nocheck
declare const styles = {
a_1: '' as readonly string,
};
export default styles;
"
`);
expect(await iff.readFile('generated/src/b.module.css.d.ts')).toMatchInlineSnapshot(`
"// @ts-nocheck
declare const styles = {
b_1: '' as readonly string,
};
export default styles;
"
`);
});
test('reports diagnostics if errors are found', async () => {
const iff = await createIFF({
'tsconfig.json': '{}',
'src/a.module.css': '.a_1 {',
'src/b.module.css': '.b_1 { color: red; }',
});
const loggerSpy = createLoggerSpy();
await runCMK(fakeParsedArgs({ project: iff.rootDir }), loggerSpy);
expect(loggerSpy.logDiagnostics).toHaveBeenCalledTimes(1);
expect(formatDiagnostics(loggerSpy.logDiagnostics.mock.calls[0]![0], iff.rootDir)).toMatchInlineSnapshot(`
[
{
"category": "error",
"fileName": "<rootDir>/src/a.module.css",
"length": 1,
"start": {
"column": 1,
"line": 1,
},
"text": "Unclosed block",
},
]
`);
});
test('returns false if errors are found', async () => {
const iff = await createIFF({
'tsconfig.json': '{}',
'src/a.module.css': '.a_1 {',
});
const result1 = await runCMK(fakeParsedArgs({ project: iff.rootDir }), createLoggerSpy());
expect(result1).toBe(false);
await writeFile(iff.join('src/a.module.css'), '.a_1 { color: red; }');
const result2 = await runCMK(fakeParsedArgs({ project: iff.rootDir }), createLoggerSpy());
expect(result2).toBe(true);
});
test('emits .d.ts files even if there are diagnostics', async () => {
const iff = await createIFF({
'tsconfig.json': '{}',
'src/a.module.css': '.a_1 {',
'src/b.module.css': '.b_1 { color: red; }',
});
const loggerSpy = createLoggerSpy();
await runCMK(fakeParsedArgs({ project: iff.rootDir }), loggerSpy);
expect(loggerSpy.logDiagnostics).toHaveBeenCalledTimes(1);
await expect(access(iff.join('generated/src/a.module.css.d.ts'))).resolves.not.toThrow();
await expect(access(iff.join('generated/src/b.module.css.d.ts'))).resolves.not.toThrow();
});
test('removes output directory before emitting files when `clean` is true', async () => {
const iff = await createIFF({
'tsconfig.json': '{}',
'src/a.module.css': '.a_1 { color: red; }',
'generated/src/old.module.css.d.ts': '',
});
await runCMK(fakeParsedArgs({ project: iff.rootDir, clean: true }), createLoggerSpy());
await expect(access(iff.join('generated/src/a.module.css.d.ts'))).resolves.not.toThrow();
await expect(access(iff.join('generated/src/old.module.css.d.ts'))).rejects.toThrow();
});
});
describe('runCMKInWatchMode', () => {
let watcher: Watcher | null = null;
afterEach(async () => {
if (watcher) {
await watcher.close();
// eslint-disable-next-line require-atomic-updates
watcher = null;
}
});
test('emits .d.ts files', async () => {
const iff = await createIFF({
'tsconfig.json': dedent`
{
"cmkOptions": { "dtsOutDir": "generated" }
}
`,
'src/a.module.css': '.a_1 { color: red; }',
'src/b.module.css': '.b_1 { color: blue; }',
});
watcher = await runCMKInWatchMode(fakeParsedArgs({ project: iff.rootDir }), createLoggerSpy());
expect(await iff.readFile('generated/src/a.module.css.d.ts')).toMatchInlineSnapshot(`
"// @ts-nocheck
declare const styles = {
a_1: '' as readonly string,
};
export default styles;
"
`);
expect(await iff.readFile('generated/src/b.module.css.d.ts')).toMatchInlineSnapshot(`
"// @ts-nocheck
declare const styles = {
b_1: '' as readonly string,
};
export default styles;
"
`);
});
test('reports diagnostics if errors are found', async () => {
const iff = await createIFF({
'tsconfig.json': '{}',
'src/a.module.css': '.a_1 {',
'src/b.module.css': '.b_1 { color: red; }',
});
const loggerSpy = createLoggerSpy();
watcher = await runCMKInWatchMode(fakeParsedArgs({ project: iff.rootDir }), loggerSpy);
expect(loggerSpy.logDiagnostics).toHaveBeenCalledTimes(1);
expect(formatDiagnostics(loggerSpy.logDiagnostics.mock.calls[0]![0], iff.rootDir)).toMatchInlineSnapshot(`
[
{
"category": "error",
"fileName": "<rootDir>/src/a.module.css",
"length": 1,
"start": {
"column": 1,
"line": 1,
},
"text": "Unclosed block",
},
]
`);
});
test('emits .d.ts files even if there are diagnostics', async () => {
const iff = await createIFF({
'tsconfig.json': '{}',
'src/a.module.css': '.a_1 {',
'src/b.module.css': '.b_1 { color: red; }',
});
const loggerSpy = createLoggerSpy();
watcher = await runCMKInWatchMode(fakeParsedArgs({ project: iff.rootDir }), loggerSpy);
expect(loggerSpy.logDiagnostics).toHaveBeenCalledTimes(1);
await expect(access(iff.join('generated/src/a.module.css.d.ts'))).resolves.not.toThrow();
await expect(access(iff.join('generated/src/b.module.css.d.ts'))).resolves.not.toThrow();
});
test('removes output directory before emitting files when `clean` is true', async () => {
const iff = await createIFF({
'tsconfig.json': '{}',
'src/a.module.css': '.a_1 { color: red; }',
'generated/src/old.module.css.d.ts': '',
});
watcher = await runCMKInWatchMode(fakeParsedArgs({ project: iff.rootDir, clean: true }), createLoggerSpy());
await expect(access(iff.join('generated/src/a.module.css.d.ts'))).resolves.not.toThrow();
await expect(access(iff.join('generated/src/old.module.css.d.ts'))).rejects.toThrow();
});
test('reports system error occurs during watching', async () => {
const iff = await createIFF({
'tsconfig.json': '{}',
'src/a.module.css': '.a_1 { color: red; }',
});
// Workaround for https://github.com/nodejs/node/issues/54450
if (platform === 'darwin') await sleep(100);
const loggerSpy = createLoggerSpy();
watcher = await runCMKInWatchMode(fakeParsedArgs({ project: iff.rootDir }), loggerSpy);
// Workaround for https://github.com/nodejs/node/issues/52601
if (platform === 'darwin') await sleep(100);
// Error when adding a file
vi.spyOn(watcher.project, 'addFile').mockImplementationOnce(() => {
throw new Error('test error');
});
await writeFile(iff.join('src/b.module.css'), '.b_1 { color: red; }');
await vi.waitFor(() => {
expect(loggerSpy.logError).toHaveBeenCalledTimes(1);
});
// Error when changing a file
vi.spyOn(watcher.project, 'updateFile').mockImplementationOnce(() => {
throw new Error('test error');
});
await writeFile(iff.join('src/a.module.css'), '.a_1 { color: blue; }');
await vi.waitFor(() => {
expect(loggerSpy.logError).toHaveBeenCalledTimes(2);
});
// Workaround for https://github.com/paulmillr/chokidar/issues/1399
await sleep(100);
// Error when emitting files
vi.spyOn(watcher.project, 'emitDtsFiles').mockImplementationOnce(() => {
throw new Error('test error');
});
await writeFile(iff.join('src/a.module.css'), '.a_1 { color: yellow; }');
await vi.waitFor(() => {
// Wait for watcher to emit and report diagnostics
expect(loggerSpy.logError).toHaveBeenCalledTimes(3);
});
});
test('reports diagnostics and emits files on changes', async () => {
const iff = await createIFF({
'tsconfig.json': '{}',
'src/a.module.css': '.a_1 { color: red; }',
});
const loggerSpy = createLoggerSpy();
watcher = await runCMKInWatchMode(fakeParsedArgs({ project: iff.rootDir }), loggerSpy);
// Workaround for https://github.com/nodejs/node/issues/52601
if (platform === 'darwin') await sleep(100);
loggerSpy.logDiagnostics.mockClear();
// Add a file
await writeFile(iff.join('src/b.module.css'), '.b_1 {');
await vi.waitFor(async () => {
// Wait for watcher to emit and report diagnostics
expect(loggerSpy.logDiagnostics).toHaveBeenCalledTimes(1);
expect(await iff.readFile('generated/src/b.module.css.d.ts')).contain('b_1');
});
// Change a file
await writeFile(iff.join('src/b.module.css'), '.b_2 {');
await vi.waitFor(async () => {
// Wait for watcher to emit and report diagnostics
expect(loggerSpy.logDiagnostics).toHaveBeenCalledTimes(2);
expect(await iff.readFile('generated/src/b.module.css.d.ts')).contain('b_2');
});
// Remove a file
await rm(iff.join('src/a.module.css'));
await vi.waitFor(async () => {
// Wait for watcher to emit and report diagnostics
expect(loggerSpy.logDiagnostics).toHaveBeenCalledTimes(3);
await expect(access(iff.join('generated/src/a.module.css.d.ts'))).resolves.not.toThrow();
});
});
test('batches rapid file changes', async () => {
const iff = await createIFF({
'tsconfig.json': '{}',
'src': {},
});
const loggerSpy = createLoggerSpy();
watcher = await runCMKInWatchMode(fakeParsedArgs({ project: iff.rootDir }), loggerSpy);
// Workaround for https://github.com/nodejs/node/issues/52601
if (platform === 'darwin') await sleep(100);
loggerSpy.logDiagnostics.mockClear();
// Make rapid changes
await Promise.all([
writeFile(iff.join('src/a.module.css'), '.a_1 {'),
writeFile(iff.join('src/b.module.css'), '.b_1 {'),
writeFile(iff.join('src/c.module.css'), '.c_1 {'),
]);
await vi.waitFor(() => {
// Wait for watcher to emit and report diagnostics
expect(loggerSpy.logDiagnostics).toHaveBeenCalledTimes(1);
// Diagnostics for three files are reported at once.
expect(formatDiagnostics(loggerSpy.logDiagnostics.mock.calls[0]![0], iff.rootDir)).length(3);
});
});
test('does not clear screen when preserveWatchOutput is true', async () => {
const iff = await createIFF({
'tsconfig.json': '{}',
'src/a.module.css': '.a_1 { color: red; }',
});
const loggerSpy1 = createLoggerSpy();
watcher = await runCMKInWatchMode(fakeParsedArgs({ project: iff.rootDir, preserveWatchOutput: false }), loggerSpy1);
expect(loggerSpy1.clearScreen).toHaveBeenCalledTimes(1);
await watcher.close();
const loggerSpy2 = createLoggerSpy();
// eslint-disable-next-line require-atomic-updates
watcher = await runCMKInWatchMode(fakeParsedArgs({ project: iff.rootDir, preserveWatchOutput: true }), loggerSpy2);
expect(loggerSpy2.clearScreen).toHaveBeenCalledTimes(0);
});
});