From 8e4e4df91a43fe81881e126e9e29de076490c77c Mon Sep 17 00:00:00 2001 From: Pietro Marchini Date: Wed, 16 Apr 2025 17:38:50 +0200 Subject: [PATCH] test_runner: recalculate run duration on watch restart PR-URL: https://github.com/nodejs/node/pull/57786 Reviewed-By: Benjamin Gruenbaum Reviewed-By: Marco Ippolito --- lib/internal/test_runner/runner.js | 5 ++++- lib/internal/test_runner/test.js | 5 +++++ test/parallel/test-runner-run-watch.mjs | 16 ++++++++++++++++ 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/lib/internal/test_runner/runner.js b/lib/internal/test_runner/runner.js index 98ffefb9cc..f89b98e1e9 100644 --- a/lib/internal/test_runner/runner.js +++ b/lib/internal/test_runner/runner.js @@ -503,7 +503,10 @@ function watchFiles(testFiles, opts) { return; // Avoid rerunning files when file deleted } } - + // Reset the root start time to recalculate the duration + // of the run + opts.root.clearExecutionTime(); + // Restart test files if (opts.isolation === 'none') { PromisePrototypeThen(restartTestFile(kIsolatedProcessName), undefined, (error) => { triggerUncaughtException(error, true /* fromPromise */); diff --git a/lib/internal/test_runner/test.js b/lib/internal/test_runner/test.js index a508a77fbf..c839058645 100644 --- a/lib/internal/test_runner/test.js +++ b/lib/internal/test_runner/test.js @@ -1341,6 +1341,11 @@ class Test extends AsyncResource { this.parent.reportStarted(); this.reporter.start(this.nesting, this.loc, this.name); } + + clearExecutionTime() { + this.startTime = hrtime(); + this.endTime = null; + } } class TestHook extends Test { diff --git a/test/parallel/test-runner-run-watch.mjs b/test/parallel/test-runner-run-watch.mjs index 83e8524f44..739cc29db6 100644 --- a/test/parallel/test-runner-run-watch.mjs +++ b/test/parallel/test-runner-run-watch.mjs @@ -189,6 +189,8 @@ async function testWatch( action === 'rename2' && await testRename(); action === 'delete' && await testDelete(); action === 'create' && await testCreate(); + + return runs; } describe('test runner watch mode', () => { @@ -241,6 +243,20 @@ describe('test runner watch mode', () => { await testWatch({ action: 'create', fileToCreate: 'new-test-file.test.js' }); }); + // This test is flaky by its nature as it relies on the timing of 2 different runs + // considering the number of digits in the duration_ms is 9 + // the chances of having the same duration_ms are very low + // but not impossible + // In case of costant failures, consider increasing the number of tests + it('should recalculate the run duration on a watch restart', async () => { + const testRuns = await testWatch({ file: 'test.js', fileToUpdate: 'test.js' }); + const durations = testRuns.map((run) => { + const runDuration = run.match(/# duration_ms\s([\d.]+)/); + return runDuration; + }); + assert.notDeepStrictEqual(durations[0][1], durations[1][1]); + }); + describe('test runner watch mode with different cwd', () => { it( 'should execute run using a different cwd for the runner than the process cwd',