/
test-watch-mode.mjs
235 lines (203 loc) Β· 9.46 KB
/
test-watch-mode.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
import * as common from '../common/index.mjs';
import * as fixtures from '../common/fixtures.mjs';
import tmpdir from '../common/tmpdir.js';
import assert from 'node:assert';
import path from 'node:path';
import { execPath } from 'node:process';
import { describe, it } from 'node:test';
import { spawn } from 'node:child_process';
import { writeFileSync, readFileSync } from 'node:fs';
import { inspect } from 'node:util';
import { once } from 'node:events';
import { setTimeout } from 'node:timers/promises';
if (common.isIBMi)
common.skip('IBMi does not support `fs.watch()`');
async function spawnWithRestarts({
args,
file,
restarts,
startedPredicate,
restartMethod,
}) {
args ??= [file];
const printedArgs = inspect(args.slice(args.indexOf(file)).join(' '));
startedPredicate ??= (data) => Boolean(data.match(new RegExp(`(Failed|Completed) running ${printedArgs.replace(/\\/g, '\\\\')}`, 'g'))?.length);
restartMethod ??= () => writeFileSync(file, readFileSync(file));
let stderr = '';
let stdout = '';
let restartCount = 0;
let completedStart = false;
let finished = false;
const child = spawn(execPath, ['--watch', '--no-warnings', ...args], { encoding: 'utf8' });
child.stderr.on('data', (data) => {
stderr += data;
});
child.stdout.on('data', async (data) => {
if (finished) return;
stdout += data;
const restartMessages = stdout.match(new RegExp(`Restarting ${printedArgs.replace(/\\/g, '\\\\')}`, 'g'))?.length ?? 0;
completedStart = completedStart || startedPredicate(data.toString());
if (restartMessages >= restarts && completedStart) {
finished = true;
child.kill();
return;
}
if (restartCount <= restartMessages && completedStart) {
await setTimeout(restartCount > 0 ? 1000 : 50, { ref: false }); // Prevent throttling
restartCount++;
completedStart = false;
restartMethod();
}
});
await Promise.race([once(child, 'exit'), once(child, 'error')]);
return { stderr, stdout };
}
let tmpFiles = 0;
function createTmpFile(content = 'console.log("running");') {
const file = path.join(tmpdir.path, `${tmpFiles++}.js`);
writeFileSync(file, content);
return file;
}
function removeGraceMessage(stdout, file) {
// Remove the message in case restart took long to avoid flakiness
return stdout
.replaceAll('Waiting for graceful termination...', '')
.replaceAll(`Gracefully restarted ${inspect(file)}`, '');
}
tmpdir.refresh();
// Warning: this suite can run safely with concurrency: true
// only if tests do not watch/depend on the same files
describe('watch mode', { concurrency: false, timeout: 60_0000 }, () => {
it('should watch changes to a file - event loop ended', async () => {
const file = createTmpFile();
const { stderr, stdout } = await spawnWithRestarts({ file, restarts: 1 });
assert.strictEqual(stderr, '');
assert.strictEqual(removeGraceMessage(stdout, file), [
'running', `Completed running ${inspect(file)}`, `Restarting ${inspect(file)}`,
'running', `Completed running ${inspect(file)}`, '',
].join('\n'));
});
it('should watch changes to a failing file', async () => {
const file = fixtures.path('watch-mode/failing.js');
const { stderr, stdout } = await spawnWithRestarts({ file, restarts: 1 });
assert.match(stderr, /Error: fails\r?\n/);
assert.strictEqual(stderr.match(/Error: fails\r?\n/g).length, 2);
assert.strictEqual(removeGraceMessage(stdout, file), [`Failed running ${inspect(file)}`, `Restarting ${inspect(file)}`,
`Failed running ${inspect(file)}`, ''].join('\n'));
});
it('should not watch when running an non-existing file', async () => {
const file = fixtures.path('watch-mode/non-existing.js');
const { stderr, stdout } = await spawnWithRestarts({ file, restarts: 0, restartMethod: () => {} });
assert.match(stderr, /code: 'MODULE_NOT_FOUND'/);
assert.strictEqual(stdout, [`Failed running ${inspect(file)}`, ''].join('\n'));
});
it('should watch when running an non-existing file - when specified under --watch-path', {
skip: !common.isOSX && !common.isWindows
}, async () => {
const file = fixtures.path('watch-mode/subdir/non-existing.js');
const watched = fixtures.path('watch-mode/subdir/file.js');
const { stderr, stdout } = await spawnWithRestarts({
file,
args: ['--watch-path', fixtures.path('./watch-mode/subdir/'), file],
restarts: 1,
restartMethod: () => writeFileSync(watched, readFileSync(watched))
});
assert.strictEqual(stderr, '');
assert.strictEqual(removeGraceMessage(stdout, file), [`Failed running ${inspect(file)}`, `Restarting ${inspect(file)}`,
`Failed running ${inspect(file)}`, ''].join('\n'));
});
it('should watch changes to a file - event loop blocked', async () => {
const file = fixtures.path('watch-mode/infinite-loop.js');
const { stderr, stdout } = await spawnWithRestarts({
file,
restarts: 2,
startedPredicate: (data) => data.startsWith('running'),
});
assert.strictEqual(stderr, '');
assert.strictEqual(removeGraceMessage(stdout, file),
['running', `Restarting ${inspect(file)}`, 'running', `Restarting ${inspect(file)}`, 'running', ''].join('\n'));
});
it('should watch changes to dependencies - cjs', async () => {
const file = fixtures.path('watch-mode/dependant.js');
const dependency = fixtures.path('watch-mode/dependency.js');
const { stderr, stdout } = await spawnWithRestarts({
file,
restarts: 1,
restartMethod: () => writeFileSync(dependency, readFileSync(dependency)),
});
assert.strictEqual(stderr, '');
assert.strictEqual(removeGraceMessage(stdout, file), [
'{}', `Completed running ${inspect(file)}`, `Restarting ${inspect(file)}`,
'{}', `Completed running ${inspect(file)}`, '',
].join('\n'));
});
it('should watch changes to dependencies - esm', async () => {
const file = fixtures.path('watch-mode/dependant.mjs');
const dependency = fixtures.path('watch-mode/dependency.mjs');
const { stderr, stdout } = await spawnWithRestarts({
file,
restarts: 1,
restartMethod: () => writeFileSync(dependency, readFileSync(dependency)),
});
assert.strictEqual(stderr, '');
assert.strictEqual(removeGraceMessage(stdout, file), [
'{}', `Completed running ${inspect(file)}`, `Restarting ${inspect(file)}`,
'{}', `Completed running ${inspect(file)}`, '',
].join('\n'));
});
it('should restart multiple times', async () => {
const file = createTmpFile();
const { stderr, stdout } = await spawnWithRestarts({ file, restarts: 3 });
assert.strictEqual(stderr, '');
assert.strictEqual(stdout.match(new RegExp(`Restarting ${inspect(file).replace(/\\/g, '\\\\')}`, 'g')).length, 3);
});
it('should gracefully wait when restarting', { skip: common.isWindows }, async () => {
const file = fixtures.path('watch-mode/graceful-sigterm.js');
const { stderr, stdout } = await spawnWithRestarts({
file,
restarts: 1,
startedPredicate: (data) => data.startsWith('running'),
});
// This message appearing is very flaky depending on a race between the
// inner process and the outer process. it is acceptable for the message not to appear
// as long as the SIGTERM handler is respected.
if (stdout.includes('Waiting for graceful termination...')) {
assert.strictEqual(stdout, ['running', `Restarting ${inspect(file)}`, 'Waiting for graceful termination...',
'exiting gracefully', `Gracefully restarted ${inspect(file)}`, 'running', ''].join('\n'));
} else {
assert.strictEqual(stdout, ['running', `Restarting ${inspect(file)}`, 'exiting gracefully', 'running', ''].join('\n'));
}
assert.strictEqual(stderr, '');
});
it('should pass arguments to file', async () => {
const file = fixtures.path('watch-mode/parse_args.js');
const random = Date.now().toString();
const args = [file, '--random', random];
const { stderr, stdout } = await spawnWithRestarts({ file, args, restarts: 1 });
assert.strictEqual(stderr, '');
assert.strictEqual(removeGraceMessage(stdout, args.join(' ')), [
random, `Completed running ${inspect(args.join(' '))}`, `Restarting ${inspect(args.join(' '))}`,
random, `Completed running ${inspect(args.join(' '))}`, '',
].join('\n'));
});
it('should not load --require modules in main process', async () => {
const file = createTmpFile('');
const required = fixtures.path('watch-mode/process_exit.js');
const args = ['--require', required, file];
const { stderr, stdout } = await spawnWithRestarts({ file, args, restarts: 1 });
assert.strictEqual(stderr, '');
assert.strictEqual(removeGraceMessage(stdout, file), [
`Completed running ${inspect(file)}`, `Restarting ${inspect(file)}`, `Completed running ${inspect(file)}`, '',
].join('\n'));
});
it('should not load --import modules in main process', async () => {
const file = createTmpFile('');
const imported = fixtures.fileURL('watch-mode/process_exit.js');
const args = ['--import', imported, file];
const { stderr, stdout } = await spawnWithRestarts({ file, args, restarts: 1 });
assert.strictEqual(stderr, '');
assert.strictEqual(removeGraceMessage(stdout, file), [
`Completed running ${inspect(file)}`, `Restarting ${inspect(file)}`, `Completed running ${inspect(file)}`, '',
].join('\n'));
});
});