From d634476e17dd2393c76bec05342b4ab76c12d458 Mon Sep 17 00:00:00 2001 From: Moshe Atlow Date: Thu, 25 May 2023 21:20:09 +0300 Subject: [PATCH] test: fix flaky test-runner-watch-mode PR-URL: https://github.com/nodejs/node/pull/48144 Reviewed-By: Jacob Smith Reviewed-By: Rich Trott Reviewed-By: Benjamin Gruenbaum --- test/fixtures/test-runner/dependency.js | 1 - test/fixtures/test-runner/dependency.mjs | 1 - test/fixtures/test-runner/dependent.js | 5 --- test/parallel/test-runner-watch-mode.mjs | 52 ++++++++++++++++-------- 4 files changed, 35 insertions(+), 24 deletions(-) delete mode 100644 test/fixtures/test-runner/dependency.js delete mode 100644 test/fixtures/test-runner/dependency.mjs delete mode 100644 test/fixtures/test-runner/dependent.js diff --git a/test/fixtures/test-runner/dependency.js b/test/fixtures/test-runner/dependency.js deleted file mode 100644 index f053ebf7976e37..00000000000000 --- a/test/fixtures/test-runner/dependency.js +++ /dev/null @@ -1 +0,0 @@ -module.exports = {}; diff --git a/test/fixtures/test-runner/dependency.mjs b/test/fixtures/test-runner/dependency.mjs deleted file mode 100644 index cc798ff50da947..00000000000000 --- a/test/fixtures/test-runner/dependency.mjs +++ /dev/null @@ -1 +0,0 @@ -export const a = 1; diff --git a/test/fixtures/test-runner/dependent.js b/test/fixtures/test-runner/dependent.js deleted file mode 100644 index 6eb2ef2fda2ad8..00000000000000 --- a/test/fixtures/test-runner/dependent.js +++ /dev/null @@ -1,5 +0,0 @@ -const test = require('node:test'); -require('./dependency.js'); -import('./dependency.mjs'); -import('data:text/javascript,'); -test('test has ran'); diff --git a/test/parallel/test-runner-watch-mode.mjs b/test/parallel/test-runner-watch-mode.mjs index e7f17da8e5e142..9f06335cd92785 100644 --- a/test/parallel/test-runner-watch-mode.mjs +++ b/test/parallel/test-runner-watch-mode.mjs @@ -1,15 +1,37 @@ // Flags: --expose-internals import '../common/index.mjs'; +import path from 'node:path'; import { describe, it } from 'node:test'; import { spawn } from 'node:child_process'; -import { writeFileSync, readFileSync } from 'node:fs'; +import { writeFileSync } from 'node:fs'; import util from 'internal/util'; -import * as fixtures from '../common/fixtures.mjs'; +import tmpdir from '../common/tmpdir.js'; -async function testWatch({ files, fileToUpdate }) { +tmpdir.refresh(); + +// This test updates these files repeatedly, +// Reading them from disk is unreliable due to race conditions. +const fixtureContent = { + 'dependency.js': 'module.exports = {};', + 'dependency.mjs': 'export const a = 1;', + 'dependent.js': ` +const test = require('node:test'); +require('./dependency.js'); +import('./dependency.mjs'); +import('data:text/javascript,'); +test('test has ran');`, +}; +const fixturePaths = Object.keys(fixtureContent) + .reduce((acc, file) => ({ ...acc, [file]: path.join(tmpdir.path, file) }), {}); +Object.entries(fixtureContent) + .forEach(([file, content]) => writeFileSync(fixturePaths[file], content)); + +async function testWatch({ fileToUpdate }) { const ran1 = util.createDeferredPromise(); const ran2 = util.createDeferredPromise(); - const child = spawn(process.execPath, ['--watch', '--test', '--no-warnings', ...files], { encoding: 'utf8' }); + const child = spawn(process.execPath, + ['--watch', '--test', '--no-warnings', fixturePaths['dependent.js']], + { encoding: 'utf8', stdio: 'pipe' }); let stdout = ''; child.stdout.on('data', (data) => { @@ -20,8 +42,12 @@ async function testWatch({ files, fileToUpdate }) { }); await ran1.promise; - const content = readFileSync(fileToUpdate, 'utf8'); - const interval = setInterval(() => writeFileSync(fileToUpdate, content), 10); + const content = fixtureContent[fileToUpdate]; + const path = fixturePaths[fileToUpdate]; + const interval = setInterval(() => { + console.log(`Updating ${path}`); + writeFileSync(path, content); + }, 50); await ran2.promise; clearInterval(interval); child.kill(); @@ -29,22 +55,14 @@ async function testWatch({ files, fileToUpdate }) { 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/dependent.js'); - await testWatch({ files: [file1, file2], fileToUpdate: file2 }); + await testWatch({ fileToUpdate: 'dependent.js' }); }); it('should run tests with dependency repeatedly', async () => { - const file1 = fixtures.path('test-runner/index.test.js'); - const dependent = fixtures.path('test-runner/dependent.js'); - const dependency = fixtures.path('test-runner/dependency.js'); - await testWatch({ files: [file1, dependent], fileToUpdate: dependency }); + await testWatch({ fileToUpdate: 'dependency.js' }); }); it('should run tests with ESM dependency', async () => { - const file1 = fixtures.path('test-runner/index.test.js'); - const dependent = fixtures.path('test-runner/dependent.js'); - const dependency = fixtures.path('test-runner/dependency.mjs'); - await testWatch({ files: [file1, dependent], fileToUpdate: dependency }); + await testWatch({ fileToUpdate: 'dependency.mjs' }); }); });