Skip to content

Commit

Permalink
fix: close timeout in ConditionalModule
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Ilia Beliaev committed Mar 22, 2024
1 parent 7a7dc4d commit 246bb77
Showing 1 changed file with 5 additions and 8 deletions.
13 changes: 5 additions & 8 deletions lib/conditional.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<DynamicModule, 'module' | 'imports' | 'exports'>
Expand All @@ -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);
Expand Down

0 comments on commit 246bb77

Please sign in to comment.