test_runner: recalculate run duration on watch restart

PR-URL: https://github.com/nodejs/node/pull/57786
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Marco Ippolito <marcoippolito54@gmail.com>
This commit is contained in:
Pietro Marchini
2025-04-16 17:38:50 +02:00
committed by GitHub
parent af85f3f169
commit 8e4e4df91a
3 changed files with 25 additions and 1 deletions

View File

@@ -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 */);

View File

@@ -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 {

View File

@@ -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',