Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: fix flaky test-runner-watch-mode #48144

Merged
merged 2 commits into from
May 25, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
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' });
});
});