From 7b775787f0199ba0f6beca7d2d592f6b39f13477 Mon Sep 17 00:00:00 2001 From: Janne Liuhtonen Date: Sat, 25 May 2024 12:30:28 +0300 Subject: [PATCH] Fix uncleared timeout in test utils --- test/util/rxUtil.ts | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/test/util/rxUtil.ts b/test/util/rxUtil.ts index bb36206..9eae92d 100644 --- a/test/util/rxUtil.ts +++ b/test/util/rxUtil.ts @@ -6,28 +6,48 @@ export const gatherObservableResults = ( timeout = 5000, ): Promise => { return new Promise((resolve, reject) => { + let timeoutRef: NodeJS.Timeout | undefined const results: T[] = [] + + const onComplete = () => { + if (timeoutRef) { + clearTimeout(timeoutRef) + } + resolve(results) + } + + const onError = (error: Error) => { + if (timeoutRef) { + clearTimeout(timeoutRef) + } + reject(error) + } + const subscription = observable.subscribe({ next(result: T) { results.push(result) if (results.length === numberOfResults) { subscription.unsubscribe() - resolve(results) + onComplete() } }, error(error: Error) { subscription.unsubscribe() - reject(error) + onError(error) }, complete() { subscription.unsubscribe() - resolve(results) + onComplete() }, }) - setTimeout(() => { + timeoutRef = setTimeout(() => { subscription.unsubscribe() - resolve(results) + reject( + new Error( + `Timeout after ${timeout} milliseconds waiting for observable results`, + ), + ) }, timeout) }) }