diff --git a/lib/internal/test_runner/runner.js b/lib/internal/test_runner/runner.js index 733a745a494162..7943917a08840e 100644 --- a/lib/internal/test_runner/runner.js +++ b/lib/internal/test_runner/runner.js @@ -502,7 +502,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 bdeeddd0892c1b..fae26494ef9125 100644 --- a/lib/internal/test_runner/test.js +++ b/lib/internal/test_runner/test.js @@ -1335,6 +1335,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 83e8524f44a338..739cc29db6365e 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',