Skip to content

Commit

Permalink
Clear timeout if promise has already resolves to avoid open handle in…
Browse files Browse the repository at this point in the history
… jest test
  • Loading branch information
vietanhtran16 committed Sep 27, 2023
1 parent bc56253 commit f324cea
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,14 @@ const checkUrlFilters = (request: MockedRequest, options: PactMswAdapterOptionsI
};

const addTimeout = async<T>(promise: Promise<T>, label: string, timeout: number) => {
let timeoutId: NodeJS.Timeout
const asyncTimeout = new Promise<void>((_, reject) => {
setTimeout(() => {
timeoutId = setTimeout(() => {
reject(new Error(`[pact-msw-adapter] ${label} timed out after ${timeout}ms`));
}, timeout);
});

return Promise.race([promise, asyncTimeout]);
return Promise.race([promise, asyncTimeout]).then(() => clearTimeout(timeoutId));
}

export { log, logGroup, createWriter, checkUrlFilters, addTimeout };

0 comments on commit f324cea

Please sign in to comment.