Skip to content

Commit 68dce3d

Browse files
committed
test: fix test-run-watch flakiness
Signed-off-by: Antoine du Hamel <duhamelantoine1995@gmail.com>
1 parent 7203d9b commit 68dce3d

3 files changed

Lines changed: 35 additions & 34 deletions

File tree

test/common/watch.js

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
const common = require('./index.js');
33
const tmpdir = require('./tmpdir.js');
44
const fixtures = require('./fixtures.js');
5-
const { writeFileSync, readdirSync, readFileSync, renameSync, unlinkSync } = require('node:fs');
5+
const { writeFileSync, renameSync, unlinkSync, cpSync } = require('node:fs');
66
const { spawn } = require('node:child_process');
77
const { once } = require('node:events');
88
const assert = require('node:assert');
@@ -37,14 +37,7 @@ const fixtureContent = {};
3737

3838
function refreshForTestRunnerWatch() {
3939
tmpdir.refresh();
40-
const files = readdirSync(fixtures.path('test-runner-watch'));
41-
for (const file of files) {
42-
const src = fixtures.path('test-runner-watch', file);
43-
const dest = tmpdir.resolve(file);
44-
fixturePaths[file] = dest;
45-
fixtureContent[file] = readFileSync(src, 'utf8');
46-
writeFileSync(dest, fixtureContent[file]);
47-
}
40+
cpSync(fixtures.path('test-runner-watch'), tmpdir.path, { recursive: true });
4841
}
4942

5043
async function performFileOperation(operation, useRunApi, timeout = 1000) {

test/test-runner/test-run-watch-cwd-isolation-none-argv.mjs

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
// parent process argv when spawning the watch child.
33
import * as common from '../common/index.mjs';
44
import assert from 'node:assert';
5-
import { writeFileSync } from 'node:fs';
5+
import { setTimeout } from 'node:timers/promises';
6+
import { writeFile } from 'node:fs/promises';
67
import { join } from 'node:path';
78
import { run } from 'node:test';
89
import tmpdir from '../common/tmpdir.js';
@@ -11,31 +12,36 @@ import { skipIfNoWatch } from '../common/watch.js';
1112
skipIfNoWatch();
1213
tmpdir.refresh();
1314

14-
writeFileSync(join(tmpdir.path, 'test.js'), `
15+
await writeFile(join(tmpdir.path, 'test.js'), `
1516
const test = require('node:test');
1617
1718
test('test ran from cwd', () => {});
1819
`);
1920

21+
// Add some delay to ensure the OS sends the FS events before watch mode is started.
22+
await setTimeout(common.platformTimeout(100));
23+
2024
const passed = [];
2125
const controller = new AbortController();
2226
const stream = run({
2327
cwd: tmpdir.path,
2428
watch: true,
2529
signal: controller.signal,
2630
isolation: 'none',
27-
}).on('data', function({ type }) {
28-
if (type === 'test:watch:drained') {
29-
stream.removeAllListeners('test:fail');
30-
stream.removeAllListeners('test:pass');
31-
controller.abort();
32-
}
31+
}).on('data', ({ type }) => {
32+
if (type !== 'test:watch:drained') return;
33+
34+
stream.removeAllListeners('test:fail');
35+
stream.removeAllListeners('test:pass');
36+
controller.abort();
3337
});
3438

35-
stream.on('test:fail', common.mustNotCall());
36-
stream.on('test:pass', common.mustCall((data) => passed.push(data.name), 1));
37-
// eslint-disable-next-line no-unused-vars
38-
for await (const _ of stream);
39+
stream.on('test:watch:restarted', common.mustNotCall('test:watch:restarted'));
40+
stream.on('test:fail', common.mustNotCall('test:fail'));
41+
stream.on('test:pass', common.mustCall((data) => passed.push(data.name)));
42+
43+
// eslint-disable-next-line no-empty-pattern
44+
for await (const {} of stream);
3945

4046
// Validate the expected test ran by name:
4147
assert.deepStrictEqual(passed, ['test ran from cwd']);
Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,29 @@
11
// Test run({ watch: true, cwd, isolation: 'none' }) runs with different cwd while in watch mode and isolation none
22
import * as common from '../common/index.mjs';
3+
import * as fixtures from '../common/fixtures.mjs';
4+
import { setTimeout } from 'node:timers/promises';
35
import { run } from 'node:test';
4-
import tmpdir from '../common/tmpdir.js';
56
import { refreshForTestRunnerWatch, skipIfNoWatch } from '../common/watch.js';
67

78
skipIfNoWatch();
8-
refreshForTestRunnerWatch();
99

1010
const controller = new AbortController();
1111
const stream = run({
12-
cwd: tmpdir.path,
12+
cwd: fixtures.path('test-runner-watch'),
1313
watch: true,
1414
signal: controller.signal,
1515
isolation: 'none',
16-
}).on('data', function({ type }) {
17-
if (type === 'test:watch:drained') {
18-
stream.removeAllListeners('test:fail');
19-
stream.removeAllListeners('test:pass');
20-
controller.abort();
21-
}
16+
}).on('data', ({ type }) => {
17+
if (type !== 'test:watch:drained') return;
18+
19+
stream.removeAllListeners('test:fail');
20+
stream.removeAllListeners('test:pass');
21+
controller.abort();
2222
});
2323

24-
stream.on('test:fail', common.mustNotCall());
25-
stream.on('test:pass', common.mustCall(1));
26-
// eslint-disable-next-line no-unused-vars
27-
for await (const _ of stream);
24+
stream.on('test:watch:restarted', common.mustNotCall('test:watch:restarted'));
25+
stream.on('test:fail', common.mustNotCall('test:fail'));
26+
stream.on('test:pass', common.mustCall());
27+
28+
// eslint-disable-next-line no-empty-pattern
29+
for await (const {} of stream);

0 commit comments

Comments
 (0)