Skip to content

Commit 70a6e25

Browse files
committed
Ensure watcher tests exit cleanly
Always await the last pending state when the watch runs have completed. Use a teardown hook to ensure the watcher is aborted and has exited before ending the test. Ensure the item is always an object, even if it didn't come from the generator.
1 parent 783f62b commit 70a6e25

File tree

1 file changed

+24
-6
lines changed

1 file changed

+24
-6
lines changed

test/watch-mode/helpers/watch.js

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,22 +94,40 @@ export const withFixture = fixture => async (t, task) => {
9494
t.fail('Watcher performed a test run while it should have been idle');
9595
}
9696
}
97-
98-
return {};
99-
});
97+
}).then(() => ({}));
10098
idlePromise = promise;
10199

102100
await promise;
103101
};
104102

105103
let state = {};
106104
let pendingState;
105+
let process;
106+
107+
t.teardown(async () => {
108+
if (process?.connected) {
109+
process.send('abort-watcher');
110+
}
111+
112+
// Sending the `abort-watcher` message should suffice, but on Linux
113+
// the recursive watch handle does not close properly. See
114+
// <https://github.com/nodejs/node/issues/48437> but there seem to be
115+
// other isues.
116+
setTimeout(() => {
117+
process.kill('SIGKILL');
118+
}, 1000).unref();
119+
120+
try {
121+
await process;
122+
} catch {}
123+
});
107124

108125
const results = run(args, options);
109126
try {
110127
let nextResult = results.next();
111-
while (true) { // eslint-disable-line no-constant-condition
128+
while (!isDone) { // eslint-disable-line no-unmodified-loop-condition
112129
const item = await Promise.race([nextResult, idlePromise, donePromise]); // eslint-disable-line no-await-in-loop
130+
process ??= item.value?.process;
113131

114132
if (item.value) {
115133
failedIdleAssertion ||= assertingIdle;
@@ -124,8 +142,8 @@ export const withFixture = fixture => async (t, task) => {
124142
}
125143
}
126144

127-
if (item.done || isDone) {
128-
item.value?.process.send('abort-watcher');
145+
if (item.done) {
146+
await pendingState; // eslint-disable-line no-await-in-loop
129147
break;
130148
}
131149
}

0 commit comments

Comments
 (0)