From 2fb585aea696c27c31fa8f64e0d00940516ecc1c Mon Sep 17 00:00:00 2001 From: Thomas Riffard Date: Mon, 9 Dec 2024 15:57:56 +0100 Subject: [PATCH] fix(runner): long synchronous tasks does not time out (fix #2920) (#6944) Co-authored-by: Vladimir --- packages/runner/src/context.ts | 4 +++- test/cli/fixtures/fails/test-timeout.test.ts | 7 +++++++ test/cli/test/__snapshots__/fails.test.ts.snap | 1 + test/reporters/tests/merge-reports.test.ts | 2 ++ 4 files changed, 13 insertions(+), 1 deletion(-) diff --git a/packages/runner/src/context.ts b/packages/runner/src/context.ts index ef5ada1c6213..a4fe8436d42f 100644 --- a/packages/runner/src/context.ts +++ b/packages/runner/src/context.ts @@ -43,7 +43,6 @@ export function withTimeout 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) @@ -52,6 +51,9 @@ export function withTimeout 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 }) as T } diff --git a/test/cli/fixtures/fails/test-timeout.test.ts b/test/cli/fixtures/fails/test-timeout.test.ts index 010697ce7e51..afb5b6658c86 100644 --- a/test/cli/fixtures/fails/test-timeout.test.ts +++ b/test/cli/fixtures/fails/test-timeout.test.ts @@ -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, }, () => { diff --git a/test/cli/test/__snapshots__/fails.test.ts.snap b/test/cli/test/__snapshots__/fails.test.ts.snap index 8b6c55f29a16..778b78138863 100644 --- a/test/cli/test/__snapshots__/fails.test.ts.snap +++ b/test/cli/test/__snapshots__/fails.test.ts.snap @@ -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." `; diff --git a/test/reporters/tests/merge-reports.test.ts b/test/reporters/tests/merge-reports.test.ts index df5fcd885e4e..9dc10dd84fbc 100644 --- a/test/reporters/tests/merge-reports.test.ts +++ b/test/reporters/tests/merge-reports.test.ts @@ -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)