Skip to content

Commit

Permalink
fix(runner): long synchronous tasks does not time out (fix #2920) (#6944
Browse files Browse the repository at this point in the history
)

Co-authored-by: Vladimir <[email protected]>
  • Loading branch information
ferdodo and sheremet-va authored Dec 9, 2024
1 parent 4ddfe0c commit 2fb585a
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 1 deletion.
4 changes: 3 additions & 1 deletion packages/runner/src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ export function withTimeout<T extends (...args: any[]) => any>(
// this function name is used to filter error in test/cli/test/fails.test.ts
return (function runWithTimeout(...args: T extends (...args: infer A) => any ? A : never) {
return Promise.race([
fn(...args),
new Promise((resolve, reject) => {
const timer = setTimeout(() => {
clearTimeout(timer)
Expand All @@ -52,6 +51,9 @@ export function withTimeout<T extends (...args: any[]) => any>(
// `unref` might not exist in browser
timer.unref?.()
}),
Promise.resolve(fn(...args)).then((result) => {
return new Promise(resolve => setTimeout(resolve, 0, result))
}),
]) as Awaitable<void>
}) as T
}
Expand Down
7 changes: 7 additions & 0 deletions test/cli/fixtures/fails/test-timeout.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ test('hi', async () => {
await new Promise(resolve => setTimeout(resolve, 1000))
}, 10)

test('timeout on long synchronous task', async () => {
const start = Date.now();

while (Date.now() < start + 20) {
}
}, 15)

suite('suite timeout', {
timeout: 100,
}, () => {
Expand Down
1 change: 1 addition & 0 deletions test/cli/test/__snapshots__/fails.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ exports[`should fail test-timeout.test.ts > test-timeout.test.ts 1`] = `
"Error: Test timed out in 20ms.
Error: Test timed out in 200ms.
Error: Test timed out in 100ms.
Error: Test timed out in 15ms.
Error: Test timed out in 10ms."
`;
Expand Down
2 changes: 2 additions & 0 deletions test/reporters/tests/merge-reports.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ test('merge reports', async () => {
stdout | first.test.ts > test 1-2
beforeEach
stdout | first.test.ts > test 1-2
test 1-2
❯ first.test.ts (2 tests | 1 failed) <time>
Expand Down

0 comments on commit 2fb585a

Please sign in to comment.