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