| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1538,6 +1538,10 @@ This event is only emitted if `--test` flag is passed. | |||
| 1538 | 1538 | Emitted when a running test writes to `stdout`. | |
| 1539 | 1539 | This event is only emitted if `--test` flag is passed. | |
| 1540 | 1540 | ||
| 1541 | + ### Event: `'test:watch:drained'` | ||
| 1542 | + | ||
| 1543 | + Emitted when no more tests are queued for execution in watch mode. | ||
| 1544 | + | ||
| 1541 | 1545 | ## Class: `TestContext` | |
| 1542 | 1546 | ||
| 1543 | 1547 | <!-- YAML | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -335,15 +335,13 @@ class FileTest extends Test { | |||
| 335 | 335 | } | |
| 336 | 336 | } | |
| 337 | 337 | ||
| 338 | - const runningProcesses = new SafeMap(); | ||
| 339 | - const runningSubtests = new SafeMap(); | ||
| 340 | - | ||
| 341 | 338 | function runTestFile(path, root, inspectPort, filesWatcher, testNamePatterns) { | |
| 339 | + const watchMode = filesWatcher != null; | ||
| 342 | 340 | const subtest = root.createSubtest(FileTest, path, async (t) => { | |
| 343 | 341 | const args = getRunArgs({ path, inspectPort, testNamePatterns }); | |
| 344 | 342 | const stdio = ['pipe', 'pipe', 'pipe']; | |
| 345 | 343 | const env = { ...process.env, NODE_TEST_CONTEXT: 'child-v8' }; | |
| 346 | - if (filesWatcher) { | ||
| 344 | + if (watchMode) { | ||
| 347 | 345 | stdio.push('ipc'); | |
| 348 | 346 | env.WATCH_REPORT_DEPENDENCIES = '1'; | |
| 349 | 347 | } | |
@@ -352,11 +350,13 @@ function runTestFile(path, root, inspectPort, filesWatcher, testNamePatterns) { | |||
| 352 | 350 | } | |
| 353 | 351 | ||
| 354 | 352 | const child = spawn(process.execPath, args, { signal: t.signal, encoding: 'utf8', env, stdio }); | |
| 355 | - runningProcesses.set(path, child); | ||
| 353 | + if (watchMode) { | ||
| 354 | + filesWatcher.runningProcesses.set(path, child); | ||
| 355 | + filesWatcher.watcher.watchChildProcessModules(child, path); | ||
| 356 | + } | ||
| 356 | 357 | ||
| 357 | 358 | let err; | |
| 358 | 359 | ||
| 359 | - filesWatcher?.watchChildProcessModules(child, path); | ||
| 360 | 360 | ||
| 361 | 361 | child.on('error', (error) => { | |
| 362 | 362 | err = error; | |
@@ -388,8 +388,14 @@ function runTestFile(path, root, inspectPort, filesWatcher, testNamePatterns) { | |||
| 388 | 388 | finished(child.stdout, { signal: t.signal }), | |
| 389 | 389 | ]); | |
| 390 | 390 | ||
| 391 | - runningProcesses.delete(path); | ||
| 392 | - runningSubtests.delete(path); | ||
| 391 | + if (watchMode) { | ||
| 392 | + filesWatcher.runningProcesses.delete(path); | ||
| 393 | + filesWatcher.runningSubtests.delete(path); | ||
| 394 | + if (filesWatcher.runningSubtests.size === 0) { | ||
| 395 | + root.reporter[kEmitMessage]('test:watch:drained'); | ||
| 396 | + } | ||
| 397 | + } | ||
| 398 | + | ||
| 393 | 399 | if (code !== 0 || signal !== null) { | |
| 394 | 400 | if (!err) { | |
| 395 | 401 | const failureType = subtest.failedSubtests ? kSubtestsFailed : kTestCodeFailure; | |
@@ -410,9 +416,13 @@ function runTestFile(path, root, inspectPort, filesWatcher, testNamePatterns) { | |||
| 410 | 416 | } | |
| 411 | 417 | ||
| 412 | 418 | function watchFiles(testFiles, root, inspectPort, signal, testNamePatterns) { | |
| 413 | - const filesWatcher = new FilesWatcher({ throttle: 500, mode: 'filter', signal }); | ||
| 414 | - filesWatcher.on('changed', ({ owners }) => { | ||
| 415 | - filesWatcher.unfilterFilesOwnedBy(owners); | ||
| 419 | + const runningProcesses = new SafeMap(); | ||
| 420 | + const runningSubtests = new SafeMap(); | ||
| 421 | + const watcher = new FilesWatcher({ throttle: 500, mode: 'filter', signal }); | ||
| 422 | + const filesWatcher = { __proto__: null, watcher, runningProcesses, runningSubtests }; | ||
| 423 | + | ||
| 424 | + watcher.on('changed', ({ owners }) => { | ||
| 425 | + watcher.unfilterFilesOwnedBy(owners); | ||
| 416 | 426 | PromisePrototypeThen(SafePromiseAllReturnVoid(testFiles, async (file) => { | |
| 417 | 427 | if (!owners.has(file)) { | |
| 418 | 428 | return; | |
@@ -433,6 +443,7 @@ function watchFiles(testFiles, root, inspectPort, signal, testNamePatterns) { | |||
| 433 | 443 | })); | |
| 434 | 444 | }); | |
| 435 | 445 | signal?.addEventListener('abort', () => root.postRun(), { __proto__: null, once: true }); | |
| 446 | + | ||
| 436 | 447 | return filesWatcher; | |
| 437 | 448 | } | |
| 438 | 449 | ||
@@ -482,7 +493,7 @@ function run(options) { | |||
| 482 | 493 | root.harness.bootstrapComplete = true; | |
| 483 | 494 | return SafePromiseAllSettledReturnVoid(testFiles, (path) => { | |
| 484 | 495 | const subtest = runTestFile(path, root, inspectPort, filesWatcher, testNamePatterns); | |
| 485 | - runningSubtests.set(path, subtest); | ||
| 496 | + filesWatcher?.runningSubtests.set(path, subtest); | ||
| 486 | 497 | return subtest; | |
| 487 | 498 | }); | |
| 488 | 499 | }; | |
| 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