[READ-ONLY] Mirror of https://github.com/vitest-dev/vitest. Next generation testing framework powered by Vite. vitest.dev
test testing-tools vite
12

Configure Feed

Select the types of activity you want to include in your feed.

fix(runner): timeout long sync hook (#7289)

authored by

Hiroshi Ogawa and committed by
GitHub
(Jan 21, 2025, 3:16 PM +0100) c60ee27c 33ab8a24

+30 -16
+8 -14
packages/runner/src/context.ts
··· 54 54 55 55 function resolve(result: unknown) { 56 56 clearTimeout(timer) 57 + // if test/hook took too long in microtask, setTimeout won't be triggered, 58 + // but we still need to fail the test, see 59 + // https://github.com/vitest-dev/vitest/issues/2920 60 + if (now() - startTime >= timeout) { 61 + reject_(new Error(makeTimeoutMsg(isHook, timeout))) 62 + return 63 + } 57 64 resolve_(result) 58 65 } 59 66 ··· 68 75 // the result is a thenable, we don't wrap this in Promise.resolve 69 76 // to avoid creating new promises 70 77 if (typeof result === 'object' && result != null && typeof result.then === 'function') { 71 - result.then( 72 - (result) => { 73 - // if sync test/hook took too long, setTimeout won't be triggered, 74 - // but we still need to fail the test, see 75 - // https://github.com/vitest-dev/vitest/issues/2920 76 - if (now() - startTime >= timeout) { 77 - reject(new Error(makeTimeoutMsg(isHook, timeout))) 78 - } 79 - else { 80 - resolve(result) 81 - } 82 - }, 83 - reject, 84 - ) 78 + result.then(resolve, reject) 85 79 } 86 80 else { 87 81 resolve(result)
+19 -1
test/cli/fixtures/fails/test-timeout.test.ts
··· 1 - import { expect, suite, test } from 'vitest' 1 + import { beforeAll, beforeEach, expect, suite, test } from 'vitest' 2 2 3 3 test('hi', async () => { 4 4 await new Promise(resolve => setTimeout(resolve, 1000)) ··· 10 10 while (Date.now() < start + 20) { 11 11 } 12 12 }, 15) 13 + 14 + suite('timeout beforeAll', () => { 15 + beforeAll(() => { 16 + const start = Date.now(); 17 + while (Date.now() < start + 20) {} 18 + }, 16) 19 + 20 + test("ok", () => {}) 21 + }) 22 + 23 + suite('timeout beforeEach', () => { 24 + beforeEach(() => { 25 + const start = Date.now(); 26 + while (Date.now() < start + 20) {} 27 + }, 17) 28 + 29 + test("ok", () => {}) 30 + }) 13 31 14 32 suite('suite timeout', { 15 33 timeout: 100,
+3 -1
test/cli/test/__snapshots__/fails.test.ts.snap
··· 118 118 "Error: Test timed out in 20ms. 119 119 Error: Test timed out in 200ms. 120 120 Error: Test timed out in 100ms. 121 + Error: Hook timed out in 17ms. 121 122 Error: Test timed out in 15ms. 122 - Error: Test timed out in 10ms." 123 + Error: Test timed out in 10ms. 124 + Error: Hook timed out in 16ms." 123 125 `; 124 126 125 127 exports[`should fail unhandled.test.ts 1`] = `