From 6f3abba3d4edac309e0f87ab20948e672dede4ff Mon Sep 17 00:00:00 2001 From: Pietro Marchini Date: Mon, 7 Apr 2025 22:45:29 +0200 Subject: [PATCH 1/4] test_runner: recalculate run duration on watch restart --- 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 733a745a494162..24d28969479d08 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.clearStartTime(); + // Start the test file 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..576b6df7e596f4 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); } + + clearStartTime() { + 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..a04a1e6533295c 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', () => { @@ -240,6 +242,20 @@ describe('test runner watch mode', () => { async () => { 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( From 6a51802d0a3ea364c3929fbb395c711ed483ccc7 Mon Sep 17 00:00:00 2001 From: Pietro Marchini Date: Mon, 14 Apr 2025 12:16:44 +0200 Subject: [PATCH 2/4] test_runner: rename method to clearExecutionTime for clarity --- lib/internal/test_runner/runner.js | 2 +- lib/internal/test_runner/test.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/internal/test_runner/runner.js b/lib/internal/test_runner/runner.js index 24d28969479d08..eba66f98aae8f4 100644 --- a/lib/internal/test_runner/runner.js +++ b/lib/internal/test_runner/runner.js @@ -504,7 +504,7 @@ function watchFiles(testFiles, opts) { } // Reset the root start time to recalculate the duration // of the run - opts.root.clearStartTime(); + opts.root.clearExecutionTime(); // Start the test file if (opts.isolation === 'none') { PromisePrototypeThen(restartTestFile(kIsolatedProcessName), undefined, (error) => { diff --git a/lib/internal/test_runner/test.js b/lib/internal/test_runner/test.js index 576b6df7e596f4..fae26494ef9125 100644 --- a/lib/internal/test_runner/test.js +++ b/lib/internal/test_runner/test.js @@ -1336,7 +1336,7 @@ class Test extends AsyncResource { this.reporter.start(this.nesting, this.loc, this.name); } - clearStartTime() { + clearExecutionTime() { this.startTime = hrtime(); this.endTime = null; } From 153c163138edf2a50cb2b881de6092e0af2fafba Mon Sep 17 00:00:00 2001 From: Pietro Marchini Date: Mon, 14 Apr 2025 12:19:13 +0200 Subject: [PATCH 3/4] fixup! test_runner: rename method to clearExecutionTime for clarity --- lib/internal/test_runner/runner.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/internal/test_runner/runner.js b/lib/internal/test_runner/runner.js index eba66f98aae8f4..7943917a08840e 100644 --- a/lib/internal/test_runner/runner.js +++ b/lib/internal/test_runner/runner.js @@ -505,7 +505,7 @@ function watchFiles(testFiles, opts) { // Reset the root start time to recalculate the duration // of the run opts.root.clearExecutionTime(); - // Start the test file + // Restart test files if (opts.isolation === 'none') { PromisePrototypeThen(restartTestFile(kIsolatedProcessName), undefined, (error) => { triggerUncaughtException(error, true /* fromPromise */); From 121e36088b32c4d3fa4d2a2be0d3dea5d28a48f1 Mon Sep 17 00:00:00 2001 From: Pietro Marchini Date: Mon, 14 Apr 2025 17:03:56 +0200 Subject: [PATCH 4/4] test: lint tests --- test/parallel/test-runner-run-watch.mjs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/parallel/test-runner-run-watch.mjs b/test/parallel/test-runner-run-watch.mjs index a04a1e6533295c..739cc29db6365e 100644 --- a/test/parallel/test-runner-run-watch.mjs +++ b/test/parallel/test-runner-run-watch.mjs @@ -242,9 +242,9 @@ describe('test runner watch mode', () => { async () => { 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 + // 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 @@ -252,9 +252,9 @@ describe('test runner watch mode', () => { 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 + return runDuration; }); - assert.notDeepStrictEqual(durations[0][1] , durations[1][1]); + assert.notDeepStrictEqual(durations[0][1], durations[1][1]); }); describe('test runner watch mode with different cwd', () => {