test: fix flaky test-runner-watch-mode

PR-URL: https://github.com/nodejs/node/pull/48144
Reviewed-By: Jacob Smith <jacob@frende.me>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
This commit is contained in:
Moshe Atlow authored and Michaël Zasso committed 2023-05-30 12:24:30 +02:00
1 parent 6cf2adc36e
commit 8c49d74002
4 files changed
+35 -24

No files matched your search

-1
View File
@@ -1 +0,0 @@
module.exports = {};
-1
View File
@@ -1 +0,0 @@
export const a = 1;
-5
View File
@@ -1,5 +0,0 @@
const test = require('node:test');
require('./dependency.js');
import('./dependency.mjs');
import('data:text/javascript,');
test('test has ran');
+35 -17
View File
@@ -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' });
});
});