Skip to content

Commit 36934b2

Browse files
committed
Fix error handling in watcher tests
If the previous handler failed, perhaps due to an assertion, it wouldn't trigger the next run and the tests would hang. Fix by including a failure of the previous handler in the await condition for the next run.
1 parent 31a1262 commit 36934b2

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

test/watch-mode/helpers/watch.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,17 @@ import {cwd, exec} from '../../helpers/exec.js';
1212
export const test = available(fileURLToPath(import.meta.url)) ? ava : ava.skip;
1313
export const serial = available(fileURLToPath(import.meta.url)) ? ava.serial : ava.serial.skip;
1414

15+
/**
16+
* Races between `promises` and returns the result, unless `possiblyErroring` rejects first.
17+
*
18+
* `possiblyErroring` may be any value and is ignored, unless it rejects.
19+
*/
20+
const raceUnlessError = async (possiblyErroring, ...promises) => {
21+
const race = Promise.race(promises);
22+
const intermediate = await Promise.race([Promise.resolve(possiblyErroring).then(() => raceUnlessError), race]);
23+
return intermediate === raceUnlessError ? race : intermediate;
24+
};
25+
1526
export const withFixture = fixture => async (t, task) => {
1627
let completedTask = false;
1728
await temporaryDirectoryTask(async dir => {
@@ -124,7 +135,7 @@ export const withFixture = fixture => async (t, task) => {
124135
try {
125136
let nextResult = results.next();
126137
while (!isDone) { // eslint-disable-line no-unmodified-loop-condition
127-
const item = await Promise.race([nextResult, idlePromise, donePromise]); // eslint-disable-line no-await-in-loop
138+
const item = await raceUnlessError(pendingState, nextResult, idlePromise, donePromise); // eslint-disable-line no-await-in-loop
128139
process ??= item.value?.process;
129140

130141
if (item.value) {

0 commit comments

Comments
 (0)