Skip to content

Commit 17fbfb2

Browse files
MoLowdanielleadams
authored andcommitted
test_runner: reset count on watch mode
PR-URL: #46577 Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 7f9e60a commit 17fbfb2

File tree

3 files changed

+22
-5
lines changed

3 files changed

+22
-5
lines changed

lib/internal/test_runner/runner.js

+13-1
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ const {
44
ArrayPrototypeFilter,
55
ArrayPrototypeForEach,
66
ArrayPrototypeIncludes,
7+
ArrayPrototypeIndexOf,
78
ArrayPrototypePush,
89
ArrayPrototypeSlice,
910
ArrayPrototypeSome,
1011
ArrayPrototypeSort,
12+
ArrayPrototypeSplice,
1113
FunctionPrototypeCall,
1214
Number,
1315
ObjectAssign,
@@ -324,7 +326,17 @@ function runTestFile(path, root, inspectPort, filesWatcher) {
324326
throw err;
325327
}
326328
});
327-
return subtest.start();
329+
const promise = subtest.start();
330+
if (filesWatcher) {
331+
return PromisePrototypeThen(promise, () => {
332+
const index = ArrayPrototypeIndexOf(root.subtests, subtest);
333+
if (index !== -1) {
334+
ArrayPrototypeSplice(root.subtests, index, 1);
335+
root.waitingOn--;
336+
}
337+
});
338+
}
339+
return promise;
328340
}
329341

330342
function watchFiles(testFiles, root, inspectPort) {
+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
const test = require('node:test');
12
require('./dependency.js');
23
import('./dependency.mjs');
34
import('data:text/javascript,');
5+
test('test has ran');

test/parallel/test-runner-watch-mode.mjs

+7-4
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,25 @@ async function testWatch({ files, fileToUpdate }) {
1111
const ran2 = util.createDeferredPromise();
1212
const child = spawn(process.execPath, ['--watch', '--test', '--no-warnings', ...files], { encoding: 'utf8' });
1313
let stdout = '';
14+
1415
child.stdout.on('data', (data) => {
1516
stdout += data.toString();
16-
if (/ok 2/.test(stdout)) ran1.resolve();
17-
if (/ok 3/.test(stdout)) ran2.resolve();
17+
const matches = stdout.match(/test has ran/g);
18+
if (matches?.length >= 1) ran1.resolve();
19+
if (matches?.length >= 2) ran2.resolve();
1820
});
1921

2022
await ran1.promise;
21-
writeFileSync(fileToUpdate, readFileSync(fileToUpdate, 'utf8'));
23+
const interval = setInterval(() => writeFileSync(fileToUpdate, readFileSync(fileToUpdate, 'utf8')), 50);
2224
await ran2.promise;
25+
clearInterval(interval);
2326
child.kill();
2427
}
2528

2629
describe('test runner watch mode', () => {
2730
it('should run tests repeatedly', async () => {
2831
const file1 = fixtures.path('test-runner/index.test.js');
29-
const file2 = fixtures.path('test-runner/subdir/subdir_test.js');
32+
const file2 = fixtures.path('test-runner/dependent.js');
3033
await testWatch({ files: [file1, file2], fileToUpdate: file2 });
3134
});
3235

0 commit comments

Comments
 (0)