Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 446f611

Browse filesBrowse files
committed
test: fix flaky test-runner-watch-mode
PR-URL: #48144 Reviewed-By: Jacob Smith <jacob@frende.me> Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
1 parent 7692d2e commit 446f611
Copy full SHA for 446f611

File tree

Expand file treeCollapse file tree

4 files changed

+35
-24
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

4 files changed

+35
-24
lines changed
Open diff view settings
Collapse file

‎test/fixtures/test-runner/dependency.js‎

Copy file name to clipboardExpand all lines: test/fixtures/test-runner/dependency.js
-1Lines changed: 0 additions & 1 deletion
This file was deleted.
Collapse file

‎test/fixtures/test-runner/dependency.mjs‎

Copy file name to clipboardExpand all lines: test/fixtures/test-runner/dependency.mjs
-1Lines changed: 0 additions & 1 deletion
This file was deleted.
Collapse file

‎test/fixtures/test-runner/dependent.js‎

Copy file name to clipboardExpand all lines: test/fixtures/test-runner/dependent.js
-5Lines changed: 0 additions & 5 deletions
This file was deleted.
Collapse file
+35-17Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,37 @@
11
// Flags: --expose-internals
22
import '../common/index.mjs';
3+
import path from 'node:path';
34
import { describe, it } from 'node:test';
45
import { spawn } from 'node:child_process';
5-
import { writeFileSync, readFileSync } from 'node:fs';
6+
import { writeFileSync } from 'node:fs';
67
import util from 'internal/util';
7-
import * as fixtures from '../common/fixtures.mjs';
8+
import tmpdir from '../common/tmpdir.js';
89

9-
async function testWatch({ files, fileToUpdate }) {
10+
tmpdir.refresh();
11+
12+
// This test updates these files repeatedly,
13+
// Reading them from disk is unreliable due to race conditions.
14+
const fixtureContent = {
15+
'dependency.js': 'module.exports = {};',
16+
'dependency.mjs': 'export const a = 1;',
17+
'dependent.js': `
18+
const test = require('node:test');
19+
require('./dependency.js');
20+
import('./dependency.mjs');
21+
import('data:text/javascript,');
22+
test('test has ran');`,
23+
};
24+
const fixturePaths = Object.keys(fixtureContent)
25+
.reduce((acc, file) => ({ ...acc, [file]: path.join(tmpdir.path, file) }), {});
26+
Object.entries(fixtureContent)
27+
.forEach(([file, content]) => writeFileSync(fixturePaths[file], content));
28+
29+
async function testWatch({ fileToUpdate }) {
1030
const ran1 = util.createDeferredPromise();
1131
const ran2 = util.createDeferredPromise();
12-
const child = spawn(process.execPath, ['--watch', '--test', '--no-warnings', ...files], { encoding: 'utf8' });
32+
const child = spawn(process.execPath,
33+
['--watch', '--test', '--no-warnings', fixturePaths['dependent.js']],
34+
{ encoding: 'utf8', stdio: 'pipe' });
1335
let stdout = '';
1436

1537
child.stdout.on('data', (data) => {
@@ -20,31 +42,27 @@ async function testWatch({ files, fileToUpdate }) {
2042
});
2143

2244
await ran1.promise;
23-
const content = readFileSync(fileToUpdate, 'utf8');
24-
const interval = setInterval(() => writeFileSync(fileToUpdate, content), 10);
45+
const content = fixtureContent[fileToUpdate];
46+
const path = fixturePaths[fileToUpdate];
47+
const interval = setInterval(() => {
48+
console.log(`Updating ${path}`);
49+
writeFileSync(path, content);
50+
}, 50);
2551
await ran2.promise;
2652
clearInterval(interval);
2753
child.kill();
2854
}
2955

3056
describe('test runner watch mode', () => {
3157
it('should run tests repeatedly', async () => {
32-
const file1 = fixtures.path('test-runner/index.test.js');
33-
const file2 = fixtures.path('test-runner/dependent.js');
34-
await testWatch({ files: [file1, file2], fileToUpdate: file2 });
58+
await testWatch({ fileToUpdate: 'dependent.js' });
3559
});
3660

3761
it('should run tests with dependency repeatedly', async () => {
38-
const file1 = fixtures.path('test-runner/index.test.js');
39-
const dependent = fixtures.path('test-runner/dependent.js');
40-
const dependency = fixtures.path('test-runner/dependency.js');
41-
await testWatch({ files: [file1, dependent], fileToUpdate: dependency });
62+
await testWatch({ fileToUpdate: 'dependency.js' });
4263
});
4364

4465
it('should run tests with ESM dependency', async () => {
45-
const file1 = fixtures.path('test-runner/index.test.js');
46-
const dependent = fixtures.path('test-runner/dependent.js');
47-
const dependency = fixtures.path('test-runner/dependency.mjs');
48-
await testWatch({ files: [file1, dependent], fileToUpdate: dependency });
66+
await testWatch({ fileToUpdate: 'dependency.mjs' });
4967
});
5068
});

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.