Skip to content

Commit 8207828

Browse files
pmarchiniRafaelGSS
authored andcommitted
test_runner: recalculate run duration on watch restart
PR-URL: #57786 Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Marco Ippolito <marcoippolito54@gmail.com>
1 parent ab99ee6 commit 8207828

3 files changed

Lines changed: 25 additions & 1 deletion

File tree

‎lib/internal/test_runner/runner.js‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,10 @@ function watchFiles(testFiles, opts) {
503503
return;// Avoid rerunning files when file deleted
504504
}
505505
}
506-
506+
// Reset the root start time to recalculate the duration
507+
// of the run
508+
opts.root.clearExecutionTime();
509+
// Restart test files
507510
if(opts.isolation==='none'){
508511
PromisePrototypeThen(restartTestFile(kIsolatedProcessName),undefined,(error)=>{
509512
triggerUncaughtException(error,true/* fromPromise */);

‎lib/internal/test_runner/test.js‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1341,6 +1341,11 @@ class Test extends AsyncResource {
13411341
this.parent.reportStarted();
13421342
this.reporter.start(this.nesting,this.loc,this.name);
13431343
}
1344+
1345+
clearExecutionTime(){
1346+
this.startTime=hrtime();
1347+
this.endTime=null;
1348+
}
13441349
}
13451350

13461351
classTestHookextendsTest{

‎test/parallel/test-runner-run-watch.mjs‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,8 @@ async function testWatch(
189189
action==='rename2'&&awaittestRename();
190190
action==='delete'&&awaittestDelete();
191191
action==='create'&&awaittestCreate();
192+
193+
returnruns;
192194
}
193195

194196
describe('test runner watch mode',()=>{
@@ -241,6 +243,20 @@ describe('test runner watch mode', () => {
241243
awaittestWatch({action: 'create',fileToCreate: 'new-test-file.test.js'});
242244
});
243245

246+
// This test is flaky by its nature as it relies on the timing of 2 different runs
247+
// considering the number of digits in the duration_ms is 9
248+
// the chances of having the same duration_ms are very low
249+
// but not impossible
250+
// In case of costant failures, consider increasing the number of tests
251+
it('should recalculate the run duration on a watch restart',async()=>{
252+
consttestRuns=awaittestWatch({file: 'test.js',fileToUpdate: 'test.js'});
253+
constdurations=testRuns.map((run)=>{
254+
construnDuration=run.match(/#duration_ms\s([\d.]+)/);
255+
returnrunDuration;
256+
});
257+
assert.notDeepStrictEqual(durations[0][1],durations[1][1]);
258+
});
259+
244260
describe('test runner watch mode with different cwd',()=>{
245261
it(
246262
'should execute run using a different cwd for the runner than the process cwd',

0 commit comments

Comments
 (0)