Skip to content

Commit

Permalink
test: fix flaky test-runner-watch-mode
Browse files Browse the repository at this point in the history
PR-URL: #48144
Reviewed-By: Jacob Smith <jacob@frende.me>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
  • Loading branch information
MoLow authored and danielleadams committed Jul 6, 2023
1 parent 1b248f3 commit 9ffe061
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 24 deletions.
1 change: 0 additions & 1 deletion test/fixtures/test-runner/dependency.js

This file was deleted.

1 change: 0 additions & 1 deletion test/fixtures/test-runner/dependency.mjs

This file was deleted.

5 changes: 0 additions & 5 deletions test/fixtures/test-runner/dependent.js

This file was deleted.

52 changes: 35 additions & 17 deletions test/parallel/test-runner-watch-mode.mjs
Original file line number Diff line number Diff line change
@@ -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) => {
Expand All @@ -20,31 +42,27 @@ 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();
}

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' });
});
});

0 comments on commit 9ffe061

Please sign in to comment.