From 246bb77f002e1ea1cdf1fd3d5b4d1969809ab965 Mon Sep 17 00:00:00 2001 From: Ilia Beliaev Date: Thu, 21 Mar 2024 22:33:35 +0100 Subject: [PATCH] fix: close timeout in ConditionalModule When conditional module was used in integration tests with jest, then an open handle would be reported due to setTimeout promise not triggering before the tests execution finishes. To avoid open handles close the timeout instead of setting a boolean variable and checking its value after 5 (by default) seconds. --- lib/conditional.module.ts | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/lib/conditional.module.ts b/lib/conditional.module.ts index 41875988..f7d70545 100644 --- a/lib/conditional.module.ts +++ b/lib/conditional.module.ts @@ -13,14 +13,11 @@ export class ConditionalModule { condition: string | ((env: NodeJS.ProcessEnv) => boolean), options?: { timeout: number }, ) { - let configResolved = false; const { timeout = 5000 } = options ?? {}; - setTimeout(() => { - if (!configResolved) { - throw new Error( - `Nest was not able to resolve the config variables within ${timeout} milliseconds. Bause of this, the ConditionalModule was not able to determine if ${module.toString()} should be registered or not`, - ); - } + const timer = setTimeout(() => { + throw new Error( + `Nest was not able to resolve the config variables within ${timeout} milliseconds. Bause of this, the ConditionalModule was not able to determine if ${module.toString()} should be registered or not`, + ); }, timeout); const returnModule: Required< Pick @@ -32,7 +29,7 @@ export class ConditionalModule { }; } await ConfigModule.envVariablesLoaded; - configResolved = true; + clearTimeout(timer); const evaluation = condition(process.env); if (evaluation) { returnModule.imports.push(module);