| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1473,6 +1473,10 @@ This event is only emitted if `--test` flag is passed. | |||
| 1473 | 1473 | Emitted when a running test writes to `stdout`. | |
| 1474 | 1474 | This event is only emitted if `--test` flag is passed. | |
| 1475 | 1475 | ||
| 1476 | + ### Event: `'test:watch:drained'` | ||
| 1477 | + | ||
| 1478 | + Emitted when no more tests are queued for execution in watch mode. | ||
| 1479 | + | ||
| 1476 | 1480 | ## Class: `TestContext` | |
| 1477 | 1481 | ||
| 1478 | 1482 | <!-- YAML | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -341,25 +341,25 @@ class FileTest extends Test { | |||
| 341 | 341 | } | |
| 342 | 342 | } | |
| 343 | 343 | ||
| 344 | - const runningProcesses = new SafeMap(); | ||
| 345 | - const runningSubtests = new SafeMap(); | ||
| 346 | - | ||
| 347 | 344 | function runTestFile(path, root, inspectPort, filesWatcher, testNamePatterns) { | |
| 345 | + const watchMode = filesWatcher != null; | ||
| 348 | 346 | const subtest = root.createSubtest(FileTest, path, async (t) => { | |
| 349 | 347 | const args = getRunArgs({ path, inspectPort, testNamePatterns }); | |
| 350 | 348 | const stdio = ['pipe', 'pipe', 'pipe']; | |
| 351 | 349 | const env = { ...process.env, NODE_TEST_CONTEXT: 'child-v8' }; | |
| 352 | - if (filesWatcher) { | ||
| 350 | + if (watchMode) { | ||
| 353 | 351 | stdio.push('ipc'); | |
| 354 | 352 | env.WATCH_REPORT_DEPENDENCIES = '1'; | |
| 355 | 353 | } | |
| 356 | 354 | ||
| 357 | 355 | const child = spawn(process.execPath, args, { signal: t.signal, encoding: 'utf8', env, stdio }); | |
| 358 | - runningProcesses.set(path, child); | ||
| 356 | + if (watchMode) { | ||
| 357 | + filesWatcher.runningProcesses.set(path, child); | ||
| 358 | + filesWatcher.watcher.watchChildProcessModules(child, path); | ||
| 359 | + } | ||
| 359 | 360 | ||
| 360 | 361 | let err; | |
| 361 | 362 | ||
| 362 | - filesWatcher?.watchChildProcessModules(child, path); | ||
| 363 | 363 | ||
| 364 | 364 | child.on('error', (error) => { | |
| 365 | 365 | err = error; | |
@@ -391,8 +391,14 @@ function runTestFile(path, root, inspectPort, filesWatcher, testNamePatterns) { | |||
| 391 | 391 | finished(child.stdout, { signal: t.signal }), | |
| 392 | 392 | ]); | |
| 393 | 393 | ||
| 394 | - runningProcesses.delete(path); | ||
| 395 | - runningSubtests.delete(path); | ||
| 394 | + if (watchMode) { | ||
| 395 | + filesWatcher.runningProcesses.delete(path); | ||
| 396 | + filesWatcher.runningSubtests.delete(path); | ||
| 397 | + if (filesWatcher.runningSubtests.size === 0) { | ||
| 398 | + root.reporter[kEmitMessage]('test:watch:drained'); | ||
| 399 | + } | ||
| 400 | + } | ||
| 401 | + | ||
| 396 | 402 | if (code !== 0 || signal !== null) { | |
| 397 | 403 | if (!err) { | |
| 398 | 404 | const failureType = subtest.failedSubtests ? kSubtestsFailed : kTestCodeFailure; | |
@@ -413,9 +419,13 @@ function runTestFile(path, root, inspectPort, filesWatcher, testNamePatterns) { | |||
| 413 | 419 | } | |
| 414 | 420 | ||
| 415 | 421 | function watchFiles(testFiles, root, inspectPort, signal, testNamePatterns) { | |
| 416 | - const filesWatcher = new FilesWatcher({ throttle: 500, mode: 'filter', signal }); | ||
| 417 | - filesWatcher.on('changed', ({ owners }) => { | ||
| 418 | - filesWatcher.unfilterFilesOwnedBy(owners); | ||
| 422 | + const runningProcesses = new SafeMap(); | ||
| 423 | + const runningSubtests = new SafeMap(); | ||
| 424 | + const watcher = new FilesWatcher({ throttle: 500, mode: 'filter', signal }); | ||
| 425 | + const filesWatcher = { __proto__: null, watcher, runningProcesses, runningSubtests }; | ||
| 426 | + | ||
| 427 | + watcher.on('changed', ({ owners }) => { | ||
| 428 | + watcher.unfilterFilesOwnedBy(owners); | ||
| 419 | 429 | PromisePrototypeThen(SafePromiseAllReturnVoid(testFiles, async (file) => { | |
| 420 | 430 | if (!owners.has(file)) { | |
| 421 | 431 | return; | |
@@ -436,6 +446,7 @@ function watchFiles(testFiles, root, inspectPort, signal, testNamePatterns) { | |||
| 436 | 446 | })); | |
| 437 | 447 | }); | |
| 438 | 448 | signal?.addEventListener('abort', () => root.postRun(), { __proto__: null, once: true }); | |
| 449 | + | ||
| 439 | 450 | return filesWatcher; | |
| 440 | 451 | } | |
| 441 | 452 | ||
@@ -485,7 +496,7 @@ function run(options) { | |||
| 485 | 496 | root.harness.bootstrapComplete = true; | |
| 486 | 497 | return SafePromiseAllSettledReturnVoid(testFiles, (path) => { | |
| 487 | 498 | const subtest = runTestFile(path, root, inspectPort, filesWatcher, testNamePatterns); | |
| 488 | - runningSubtests.set(path, subtest); | ||
| 499 | + filesWatcher?.runningSubtests.set(path, subtest); | ||
| 489 | 500 | return subtest; | |
| 490 | 501 | }); | |
| 491 | 502 | }; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -132,4 +132,14 @@ describe('require(\'node:test\').run', { concurrency: true }, () => { | |||
| 132 | 132 | .toArray(); | |
| 133 | 133 | assert.deepStrictEqual(result, ['this should pass']); | |
| 134 | 134 | }); | |
| 135 | + | ||
| 136 | + it('should emit "test:watch:drained" event on watch mode', async () => { | ||
| 137 | + const controller = new AbortController(); | ||
| 138 | + await run({ files: [join(testFixtures, 'test/random.cjs')], watch: true, signal: controller.signal }) | ||
| 139 | + .on('data', function({ type }) { | ||
| 140 | + if (type === 'test:watch:drained') { | ||
| 141 | + controller.abort(); | ||
| 142 | + } | ||
| 143 | + }); | ||
| 144 | + }); | ||
| 135 | 145 | }); | |
| Back | FazBrowse Home | New Git URL |
0 commit comments