Skip to content

Commit 2fb585a

Browse files
ferdodosheremet-va
andauthoredDec 9, 2024··
fix(runner): long synchronous tasks does not time out (fix #2920) (#6944)
Co-authored-by: Vladimir <sleuths.slews0s@icloud.com>
1 parent 4ddfe0c commit 2fb585a

File tree

4 files changed

+13
-1
lines changed

4 files changed

+13
-1
lines changed
 

‎packages/runner/src/context.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ export function withTimeout<T extends (...args: any[]) => any>(
4343
// this function name is used to filter error in test/cli/test/fails.test.ts
4444
return (function runWithTimeout(...args: T extends (...args: infer A) => any ? A : never) {
4545
return Promise.race([
46-
fn(...args),
4746
new Promise((resolve, reject) => {
4847
const timer = setTimeout(() => {
4948
clearTimeout(timer)
@@ -52,6 +51,9 @@ export function withTimeout<T extends (...args: any[]) => any>(
5251
// `unref` might not exist in browser
5352
timer.unref?.()
5453
}),
54+
Promise.resolve(fn(...args)).then((result) => {
55+
return new Promise(resolve => setTimeout(resolve, 0, result))
56+
}),
5557
]) as Awaitable<void>
5658
}) as T
5759
}

‎test/cli/fixtures/fails/test-timeout.test.ts

+7
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@ test('hi', async () => {
44
await new Promise(resolve => setTimeout(resolve, 1000))
55
}, 10)
66

7+
test('timeout on long synchronous task', async () => {
8+
const start = Date.now();
9+
10+
while (Date.now() < start + 20) {
11+
}
12+
}, 15)
13+
714
suite('suite timeout', {
815
timeout: 100,
916
}, () => {

‎test/cli/test/__snapshots__/fails.test.ts.snap

+1
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ exports[`should fail test-timeout.test.ts > test-timeout.test.ts 1`] = `
112112
"Error: Test timed out in 20ms.
113113
Error: Test timed out in 200ms.
114114
Error: Test timed out in 100ms.
115+
Error: Test timed out in 15ms.
115116
Error: Test timed out in 10ms."
116117
`;
117118

‎test/reporters/tests/merge-reports.test.ts

+2
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ test('merge reports', async () => {
8686
8787
stdout | first.test.ts > test 1-2
8888
beforeEach
89+
90+
stdout | first.test.ts > test 1-2
8991
test 1-2
9092
9193
❯ first.test.ts (2 tests | 1 failed) <time>

0 commit comments

Comments
 (0)
Please sign in to comment.