Skip to content

Commit

Permalink
fix: cleanup timeout on error
Browse files Browse the repository at this point in the history
  • Loading branch information
privatenumber committed Sep 25, 2022
1 parent 26142c0 commit 42f93bc
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 14 deletions.
15 changes: 8 additions & 7 deletions src/create-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,14 @@ export function createTest(
try {
if (timeout) {
const controller = new AbortController();

await Promise.race([
testFunction(),
throwOnTimeout(timeout, controller),
]);

controller.abort();
try {
await Promise.race([
testFunction(),
throwOnTimeout(timeout, controller),
]);
} finally {
controller.abort();
}
} else {
await testFunction();
}
Expand Down
24 changes: 17 additions & 7 deletions tests/specs/asynchronous-timeout.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
import { setTimeout } from 'timers/promises';
import { test } from '#manten';
import { test, describe } from '#manten';

(async () => {
await test('should fail', async () => {
await setTimeout(10);
}, 1);

await test(
'timeout checker should be cleaned up from event loop',
// eslint-disable-next-line @typescript-eslint/no-empty-function
() => {},
10_000,
);
await describe('timeout to be cleaned up from event loop', ({ test }) => {
test(
'on pass',
// eslint-disable-next-line @typescript-eslint/no-empty-function
() => {},
5000,
);

test(
'on fail',
async () => {
throw new Error('1');
},
5000,
);
});
})();

0 comments on commit 42f93bc

Please sign in to comment.