Skip to content
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion lib/internal/test_runner/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ const {
ArrayPrototypeFilter,
ArrayPrototypeForEach,
ArrayPrototypeIncludes,
ArrayPrototypeIndexOf,
ArrayPrototypePush,
ArrayPrototypeSlice,
ArrayPrototypeSome,
ArrayPrototypeSort,
ArrayPrototypeSplice,
FunctionPrototypeCall,
Number,
ObjectAssign,
Expand Down Expand Up @@ -325,7 +327,17 @@ function runTestFile(path, root, inspectPort, filesWatcher) {
throw err;
}
});
return subtest.start();
const promise = subtest.start();
if (filesWatcher) {
return PromisePrototypeThen(promise, () => {
const index = ArrayPrototypeIndexOf(root.subtests, subtest);
if (index !== -1) {
ArrayPrototypeSplice(root.subtests, index, 1);
root.waitingOn--;
}
});
}
return promise;
}

function watchFiles(testFiles, root, inspectPort) {
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/test-runner/dependent.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const test = require('node:test');
require('./dependency.js');
import('./dependency.mjs');
import('data:text/javascript,');
test('test has ran');
15 changes: 11 additions & 4 deletions test/parallel/test-runner-watch-mode.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,29 @@ async function testWatch({ files, fileToUpdate }) {
const ran2 = util.createDeferredPromise();
const child = spawn(process.execPath, ['--watch', '--test', '--no-warnings', ...files], { encoding: 'utf8' });
let stdout = '';

console.log(`${child.pid} [running test]`);
Comment thread
MoLow marked this conversation as resolved.
Outdated
child.stdout.on('data', (data) => {
console.log(data.toString().split(/\r?\n/).map((line) => `${child.pid} [stdout] ${line}`).join('\n'));
Comment thread
MoLow marked this conversation as resolved.
Outdated
stdout += data.toString();
if (/ok 2/.test(stdout)) ran1.resolve();
if (/ok 3/.test(stdout)) ran2.resolve();
const matches = stdout.match(/test has ran/g);
if (matches?.length >= 1) ran1.resolve();
if (matches?.length >= 2) ran2.resolve();
});

await ran1.promise;
writeFileSync(fileToUpdate, readFileSync(fileToUpdate, 'utf8'));
console.log(`${child.pid} [restarting]`);
Comment thread
MoLow marked this conversation as resolved.
Outdated
const interval = setInterval(() => writeFileSync(fileToUpdate, readFileSync(fileToUpdate, 'utf8')), 50);
await ran2.promise;
clearInterval(interval);
console.log(`${child.pid} [done]`);
Comment thread
MoLow marked this conversation as resolved.
Outdated
child.kill();
}

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

Expand Down