From a3aec85fda43458a0bc918e2621fa2e95afcded9 Mon Sep 17 00:00:00 2001 From: Moshe Atlow Date: Wed, 8 Feb 2023 21:14:48 +0200 Subject: [PATCH 1/4] test_runner: reset count on watch mode --- lib/internal/test_runner/runner.js | 14 +++++++++++++- test/fixtures/test-runner/dependent.js | 1 + test/parallel/test-runner-watch-mode.mjs | 10 ++++++---- 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/lib/internal/test_runner/runner.js b/lib/internal/test_runner/runner.js index 2e0c62505f5004..5108fc3283f63b 100644 --- a/lib/internal/test_runner/runner.js +++ b/lib/internal/test_runner/runner.js @@ -4,10 +4,12 @@ const { ArrayPrototypeFilter, ArrayPrototypeForEach, ArrayPrototypeIncludes, + ArrayPrototypeIndexOf, ArrayPrototypePush, ArrayPrototypeSlice, ArrayPrototypeSome, ArrayPrototypeSort, + ArrayPrototypeSplice, FunctionPrototypeCall, Number, ObjectAssign, @@ -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) { diff --git a/test/fixtures/test-runner/dependent.js b/test/fixtures/test-runner/dependent.js index c382b0f989e47b..ab61d6b7c4b25a 100644 --- a/test/fixtures/test-runner/dependent.js +++ b/test/fixtures/test-runner/dependent.js @@ -1,3 +1,4 @@ require('./dependency.js'); import('./dependency.mjs'); import('data:text/javascript,'); +console.log('ok 1 - test has ran'); diff --git a/test/parallel/test-runner-watch-mode.mjs b/test/parallel/test-runner-watch-mode.mjs index 6803ac4e349138..e6b19cbda4781a 100644 --- a/test/parallel/test-runner-watch-mode.mjs +++ b/test/parallel/test-runner-watch-mode.mjs @@ -13,20 +13,22 @@ async function testWatch({ files, fileToUpdate }) { let stdout = ''; child.stdout.on('data', (data) => { 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')); + const interval = setInterval(() => writeFileSync(fileToUpdate, readFileSync(fileToUpdate, 'utf8')), 50); await ran2.promise; + clearInterval(interval); 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 }); }); From 18587281b678e8834bae2763458d88e23d92a304 Mon Sep 17 00:00:00 2001 From: Moshe Atlow Date: Sat, 25 Feb 2023 23:02:23 +0200 Subject: [PATCH 2/4] add debug logs --- test/parallel/test-runner-watch-mode.mjs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/test/parallel/test-runner-watch-mode.mjs b/test/parallel/test-runner-watch-mode.mjs index e6b19cbda4781a..ff6266ea05153a 100644 --- a/test/parallel/test-runner-watch-mode.mjs +++ b/test/parallel/test-runner-watch-mode.mjs @@ -11,7 +11,10 @@ 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]`); child.stdout.on('data', (data) => { + console.log(data.toString().split(/\r?\n/).map((line) => `${child.pid} [stdout] ${line}`).join('\n')); stdout += data.toString(); const matches = stdout.match(/test has ran/g); if (matches?.length >= 1) ran1.resolve(); @@ -19,9 +22,11 @@ async function testWatch({ files, fileToUpdate }) { }); await ran1.promise; + console.log(`${child.pid} [restarting]`); const interval = setInterval(() => writeFileSync(fileToUpdate, readFileSync(fileToUpdate, 'utf8')), 50); await ran2.promise; clearInterval(interval); + console.log(`${child.pid} [done]`); child.kill(); } From f2d72e98afcdfed842ec19b82997aea4092ceaeb Mon Sep 17 00:00:00 2001 From: Moshe Atlow Date: Sun, 26 Feb 2023 00:22:06 +0200 Subject: [PATCH 3/4] attempt fixing --- test/fixtures/test-runner/dependent.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/fixtures/test-runner/dependent.js b/test/fixtures/test-runner/dependent.js index ab61d6b7c4b25a..6eb2ef2fda2ad8 100644 --- a/test/fixtures/test-runner/dependent.js +++ b/test/fixtures/test-runner/dependent.js @@ -1,4 +1,5 @@ +const test = require('node:test'); require('./dependency.js'); import('./dependency.mjs'); import('data:text/javascript,'); -console.log('ok 1 - test has ran'); +test('test has ran'); From fb486d21023a53d75ddf37baf6c0fdc1335bfdea Mon Sep 17 00:00:00 2001 From: Moshe Atlow Date: Sun, 26 Feb 2023 09:58:03 +0200 Subject: [PATCH 4/4] remove logs --- test/parallel/test-runner-watch-mode.mjs | 4 ---- 1 file changed, 4 deletions(-) diff --git a/test/parallel/test-runner-watch-mode.mjs b/test/parallel/test-runner-watch-mode.mjs index ff6266ea05153a..1fbc7a7c584e92 100644 --- a/test/parallel/test-runner-watch-mode.mjs +++ b/test/parallel/test-runner-watch-mode.mjs @@ -12,9 +12,7 @@ async function testWatch({ files, fileToUpdate }) { const child = spawn(process.execPath, ['--watch', '--test', '--no-warnings', ...files], { encoding: 'utf8' }); let stdout = ''; - console.log(`${child.pid} [running test]`); child.stdout.on('data', (data) => { - console.log(data.toString().split(/\r?\n/).map((line) => `${child.pid} [stdout] ${line}`).join('\n')); stdout += data.toString(); const matches = stdout.match(/test has ran/g); if (matches?.length >= 1) ran1.resolve(); @@ -22,11 +20,9 @@ async function testWatch({ files, fileToUpdate }) { }); await ran1.promise; - console.log(`${child.pid} [restarting]`); const interval = setInterval(() => writeFileSync(fileToUpdate, readFileSync(fileToUpdate, 'utf8')), 50); await ran2.promise; clearInterval(interval); - console.log(`${child.pid} [done]`); child.kill(); }