|
1 | | -import { access, writeFile } from 'node:fs/promises'; |
| 1 | +import { access, rm, writeFile } from 'node:fs/promises'; |
| 2 | +import { platform } from 'node:process'; |
2 | 3 | import dedent from 'dedent'; |
3 | | -import { describe, expect, test } from 'vitest'; |
4 | | -import { runCMK } from './runner.js'; |
| 4 | +import { afterEach, describe, expect, test, vi } from 'vitest'; |
| 5 | +import type { Watcher } from './runner.js'; |
| 6 | +import { runCMK, runCMKInWatchMode } from './runner.js'; |
5 | 7 | import { formatDiagnostics } from './test/diagnostic.js'; |
6 | 8 | import { fakeParsedArgs } from './test/faker.js'; |
7 | 9 | import { createIFF } from './test/fixture.js'; |
8 | 10 | import { createLoggerSpy } from './test/logger.js'; |
9 | 11 |
|
| 12 | +async function sleep(ms: number): Promise<void> { |
| 13 | + // eslint-disable-next-line no-promise-executor-return |
| 14 | + return new Promise((resolve) => setTimeout(resolve, ms)); |
| 15 | +} |
| 16 | + |
| 17 | +async function waitForWatcherReady(): Promise<void> { |
| 18 | + if (platform === 'darwin') { |
| 19 | + // Workaround for https://github.com/paulmillr/chokidar/issues/1443 |
| 20 | + await sleep(100); |
| 21 | + } |
| 22 | +} |
| 23 | +async function waitForWatcherEmit(): Promise<void> { |
| 24 | + await sleep(300); |
| 25 | +} |
| 26 | + |
10 | 27 | describe('runCMK', () => { |
11 | 28 | test('emits .d.ts files', async () => { |
12 | 29 | const iff = await createIFF({ |
@@ -95,4 +112,205 @@ describe('runCMK', () => { |
95 | 112 | }); |
96 | 113 | }); |
97 | 114 |
|
98 | | -describe.todo('runCMKInWatchMode', () => {}); |
| 115 | +describe('runCMKInWatchMode', () => { |
| 116 | + let watcher: Watcher | null = null; |
| 117 | + afterEach(async () => { |
| 118 | + if (watcher) { |
| 119 | + await watcher.close(); |
| 120 | + // eslint-disable-next-line require-atomic-updates |
| 121 | + watcher = null; |
| 122 | + } |
| 123 | + }); |
| 124 | + test('emits .d.ts files', async () => { |
| 125 | + const iff = await createIFF({ |
| 126 | + 'tsconfig.json': dedent` |
| 127 | + { |
| 128 | + "cmkOptions": { "dtsOutDir": "generated" } |
| 129 | + } |
| 130 | + `, |
| 131 | + 'src/a.module.css': '.a_1 { color: red; }', |
| 132 | + 'src/b.module.css': '.b_1 { color: blue; }', |
| 133 | + }); |
| 134 | + watcher = await runCMKInWatchMode(fakeParsedArgs({ project: iff.rootDir }), createLoggerSpy()); |
| 135 | + expect(await iff.readFile('generated/src/a.module.css.d.ts')).toMatchInlineSnapshot(` |
| 136 | + "// @ts-nocheck |
| 137 | + declare const styles = { |
| 138 | + a_1: '' as readonly string, |
| 139 | + }; |
| 140 | + export default styles; |
| 141 | + " |
| 142 | + `); |
| 143 | + expect(await iff.readFile('generated/src/b.module.css.d.ts')).toMatchInlineSnapshot(` |
| 144 | + "// @ts-nocheck |
| 145 | + declare const styles = { |
| 146 | + b_1: '' as readonly string, |
| 147 | + }; |
| 148 | + export default styles; |
| 149 | + " |
| 150 | + `); |
| 151 | + }); |
| 152 | + test('reports diagnostics if errors are found', async () => { |
| 153 | + const iff = await createIFF({ |
| 154 | + 'tsconfig.json': '{}', |
| 155 | + 'src/a.module.css': '.a_1 {', |
| 156 | + 'src/b.module.css': '.b_1 { color: red; }', |
| 157 | + }); |
| 158 | + const loggerSpy = createLoggerSpy(); |
| 159 | + watcher = await runCMKInWatchMode(fakeParsedArgs({ project: iff.rootDir }), loggerSpy); |
| 160 | + expect(loggerSpy.logDiagnostics).toHaveBeenCalledTimes(1); |
| 161 | + expect(formatDiagnostics(loggerSpy.logDiagnostics.mock.calls[0]![0], iff.rootDir)).toMatchInlineSnapshot(` |
| 162 | + [ |
| 163 | + { |
| 164 | + "category": "error", |
| 165 | + "fileName": "<rootDir>/src/a.module.css", |
| 166 | + "length": 1, |
| 167 | + "start": { |
| 168 | + "column": 1, |
| 169 | + "line": 1, |
| 170 | + }, |
| 171 | + "text": "Unclosed block", |
| 172 | + }, |
| 173 | + ] |
| 174 | + `); |
| 175 | + }); |
| 176 | + test('emits .d.ts files even if there are diagnostics', async () => { |
| 177 | + const iff = await createIFF({ |
| 178 | + 'tsconfig.json': '{}', |
| 179 | + 'src/a.module.css': '.a_1 {', |
| 180 | + 'src/b.module.css': '.b_1 { color: red; }', |
| 181 | + }); |
| 182 | + const loggerSpy = createLoggerSpy(); |
| 183 | + watcher = await runCMKInWatchMode(fakeParsedArgs({ project: iff.rootDir }), loggerSpy); |
| 184 | + expect(loggerSpy.logDiagnostics).toHaveBeenCalledTimes(1); |
| 185 | + await expect(access(iff.join('generated/src/a.module.css.d.ts'))).resolves.not.toThrow(); |
| 186 | + await expect(access(iff.join('generated/src/b.module.css.d.ts'))).resolves.not.toThrow(); |
| 187 | + }); |
| 188 | + test('removes output directory before emitting files when `clean` is true', async () => { |
| 189 | + const iff = await createIFF({ |
| 190 | + 'tsconfig.json': '{}', |
| 191 | + 'src/a.module.css': '.a_1 { color: red; }', |
| 192 | + 'generated/src/old.module.css.d.ts': '', |
| 193 | + }); |
| 194 | + watcher = await runCMKInWatchMode(fakeParsedArgs({ project: iff.rootDir, clean: true }), createLoggerSpy()); |
| 195 | + await expect(access(iff.join('generated/src/a.module.css.d.ts'))).resolves.not.toThrow(); |
| 196 | + await expect(access(iff.join('generated/src/old.module.css.d.ts'))).rejects.toThrow(); |
| 197 | + }); |
| 198 | + test('reports system error occurs during watching', async () => { |
| 199 | + const iff = await createIFF({ |
| 200 | + 'tsconfig.json': '{}', |
| 201 | + 'src/a.module.css': '.a_1 { color: red; }', |
| 202 | + }); |
| 203 | + const loggerSpy = createLoggerSpy(); |
| 204 | + watcher = await runCMKInWatchMode(fakeParsedArgs({ project: iff.rootDir }), loggerSpy); |
| 205 | + await waitForWatcherReady(); |
| 206 | + |
| 207 | + // Error when adding a file |
| 208 | + vi.spyOn(watcher.project, 'addFile').mockImplementationOnce(() => { |
| 209 | + throw new Error('test error'); |
| 210 | + }); |
| 211 | + await writeFile(iff.join('src/b.module.css'), '.b_1 { color: red; }'); |
| 212 | + await vi.waitFor(() => { |
| 213 | + expect(loggerSpy.logError).toHaveBeenCalledTimes(1); |
| 214 | + }); |
| 215 | + |
| 216 | + // Error when changing a file |
| 217 | + vi.spyOn(watcher.project, 'updateFile').mockImplementationOnce(() => { |
| 218 | + throw new Error('test error'); |
| 219 | + }); |
| 220 | + await writeFile(iff.join('src/a.module.css'), '.a_1 { color: blue; }'); |
| 221 | + await vi.waitFor(() => { |
| 222 | + expect(loggerSpy.logError).toHaveBeenCalledTimes(2); |
| 223 | + }); |
| 224 | + |
| 225 | + await waitForWatcherEmit(); |
| 226 | + |
| 227 | + // Error when emitting files |
| 228 | + vi.spyOn(watcher.project, 'emitDtsFiles').mockImplementationOnce(() => { |
| 229 | + throw new Error('test error'); |
| 230 | + }); |
| 231 | + await writeFile(iff.join('src/a.module.css'), '.a_1 { color: yellow; }'); |
| 232 | + await waitForWatcherEmit(); |
| 233 | + await vi.waitFor(() => { |
| 234 | + expect(loggerSpy.logError).toHaveBeenCalledTimes(3); |
| 235 | + }); |
| 236 | + }); |
| 237 | + test('reports diagnostics and emits files on changes', async () => { |
| 238 | + const iff = await createIFF({ |
| 239 | + 'tsconfig.json': '{}', |
| 240 | + 'src/a.module.css': '.a_1 { color: red; }', |
| 241 | + }); |
| 242 | + const loggerSpy = createLoggerSpy(); |
| 243 | + watcher = await runCMKInWatchMode(fakeParsedArgs({ project: iff.rootDir }), loggerSpy); |
| 244 | + await waitForWatcherReady(); |
| 245 | + |
| 246 | + // Add a file |
| 247 | + await writeFile(iff.join('src/b.module.css'), '.b_1 {'); |
| 248 | + await waitForWatcherEmit(); |
| 249 | + expect(loggerSpy.logDiagnostics).toHaveBeenCalledTimes(1); |
| 250 | + expect(formatDiagnostics(loggerSpy.logDiagnostics.mock.calls[0]![0], iff.rootDir)).toMatchInlineSnapshot(` |
| 251 | + [ |
| 252 | + { |
| 253 | + "category": "error", |
| 254 | + "fileName": "<rootDir>/src/b.module.css", |
| 255 | + "length": 1, |
| 256 | + "start": { |
| 257 | + "column": 1, |
| 258 | + "line": 1, |
| 259 | + }, |
| 260 | + "text": "Unclosed block", |
| 261 | + }, |
| 262 | + ] |
| 263 | + `); |
| 264 | + expect(await iff.readFile('generated/src/b.module.css.d.ts')).contain('b_1'); |
| 265 | + |
| 266 | + // Change a file |
| 267 | + loggerSpy.logDiagnostics.mockClear(); |
| 268 | + await writeFile(iff.join('src/b.module.css'), '.b_2 {'); |
| 269 | + await waitForWatcherEmit(); |
| 270 | + expect(loggerSpy.logDiagnostics).toHaveBeenCalledTimes(1); |
| 271 | + expect(formatDiagnostics(loggerSpy.logDiagnostics.mock.calls[0]![0], iff.rootDir)).toMatchInlineSnapshot(` |
| 272 | + [ |
| 273 | + { |
| 274 | + "category": "error", |
| 275 | + "fileName": "<rootDir>/src/b.module.css", |
| 276 | + "length": 1, |
| 277 | + "start": { |
| 278 | + "column": 1, |
| 279 | + "line": 1, |
| 280 | + }, |
| 281 | + "text": "Unclosed block", |
| 282 | + }, |
| 283 | + ] |
| 284 | + `); |
| 285 | + expect(await iff.readFile('generated/src/b.module.css.d.ts')).contain('b_2'); |
| 286 | + |
| 287 | + // Remove a file |
| 288 | + loggerSpy.logDiagnostics.mockClear(); |
| 289 | + await rm(iff.join('src/b.module.css')); |
| 290 | + await waitForWatcherEmit(); |
| 291 | + expect(loggerSpy.logDiagnostics).toHaveBeenCalledTimes(0); |
| 292 | + }); |
| 293 | + test('batches rapid file changes', async () => { |
| 294 | + const iff = await createIFF({ |
| 295 | + 'tsconfig.json': '{}', |
| 296 | + 'src': {}, |
| 297 | + }); |
| 298 | + const loggerSpy = createLoggerSpy(); |
| 299 | + watcher = await runCMKInWatchMode(fakeParsedArgs({ project: iff.rootDir }), loggerSpy); |
| 300 | + await waitForWatcherReady(); |
| 301 | + loggerSpy.logDiagnostics.mockClear(); |
| 302 | + |
| 303 | + // Make rapid changes |
| 304 | + const promises = [ |
| 305 | + writeFile(iff.join('src/a.module.css'), '.a_1 {'), |
| 306 | + writeFile(iff.join('src/b.module.css'), '.b_1 {'), |
| 307 | + writeFile(iff.join('src/c.module.css'), '.c_1 {'), |
| 308 | + ]; |
| 309 | + expect(loggerSpy.logDiagnostics).toHaveBeenCalledTimes(0); |
| 310 | + await Promise.all(promises); |
| 311 | + await waitForWatcherEmit(); |
| 312 | + expect(loggerSpy.logDiagnostics).toHaveBeenCalledTimes(1); |
| 313 | + // Diagnostics for three files are reported at once. |
| 314 | + expect(formatDiagnostics(loggerSpy.logDiagnostics.mock.calls[0]![0], iff.rootDir)).length(3); |
| 315 | + }); |
| 316 | +}); |
0 commit comments