Skip to content

Commit 3a1f912

Browse files
committed
test_runner: stop watch mode when abortSignal aborted
1 parent 8aa02e8 commit 3a1f912

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

lib/internal/test_runner/runner.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,7 @@ function runTestFile(path, root, inspectPort, filesWatcher, testNamePatterns) {
409409
return subtest.start();
410410
}
411411

412-
function watchFiles(testFiles, root, inspectPort, testNamePatterns) {
412+
function watchFiles(testFiles, root, inspectPort, signal, testNamePatterns) {
413413
const filesWatcher = new FilesWatcher({ throttle: 500, mode: 'filter' });
414414
filesWatcher.on('changed', ({ owners }) => {
415415
filesWatcher.unfilterFilesOwnedBy(owners);
@@ -432,6 +432,10 @@ function watchFiles(testFiles, root, inspectPort, testNamePatterns) {
432432
triggerUncaughtException(error, true /* fromPromise */);
433433
}));
434434
});
435+
signal?.addEventListener('abort', () => {
436+
filesWatcher.clear();
437+
root.postRun();
438+
}, { __proto__: null, once: true });
435439
return filesWatcher;
436440
}
437441

@@ -474,7 +478,7 @@ function run(options) {
474478
let postRun = () => root.postRun();
475479
let filesWatcher;
476480
if (watch) {
477-
filesWatcher = watchFiles(testFiles, root, inspectPort, testNamePatterns);
481+
filesWatcher = watchFiles(testFiles, root, inspectPort, signal, testNamePatterns);
478482
postRun = undefined;
479483
}
480484
const runFiles = () => {

test/parallel/test-runner-run.mjs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,4 +117,19 @@ describe('require(\'node:test\').run', { concurrency: true }, () => {
117117
assert.strictEqual(result[2], 'ok 1 - this should be skipped # SKIP test name does not match pattern\n');
118118
assert.strictEqual(result[5], 'ok 2 - this should be executed\n');
119119
});
120+
121+
it('should stop watch mode when abortSignal aborts', async () => {
122+
const controller = new AbortController();
123+
const result = await run({ files: [join(testFixtures, 'test/random.cjs')], watch: true, signal: controller.signal })
124+
.compose(async function* (source) {
125+
for await (const chunk of source) {
126+
if (chunk.type === 'test:pass') {
127+
controller.abort();
128+
yield chunk.data.name;
129+
}
130+
}
131+
})
132+
.toArray();
133+
assert.deepStrictEqual(result, ['this should pass']);
134+
});
120135
});

0 commit comments

Comments
 (0)