Skip to content

Commit 77a3067

Browse files
authored
fix: give each test attempt a new signal (#11998)
1 parent 016581e commit 77a3067

5 files changed

Lines changed: 86 additions & 17 deletions

File tree

‎docs/guide/test-context.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ An [`AbortSignal`](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal)
111111
- [`vitest.cancelCurrentRun`](/api/advanced/vitest#cancelcurrentrun) was called programmatically
112112
- Another test failed in parallel and the [`bail`](/config/bail) flag is set
113113

114+
Every [retry](/config/retry) and [repeat](/config/repeats) of a test gets a new signal.
115+
114116
```ts
115117
it('stop request when test times out', async ({ signal }) => {
116118
await fetch('/resource', { signal })

‎packages/vitest/src/runtime/runner/context.ts‎

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import type {
1111
} from './types'
1212
import { manageArtifactAttachment, recordArtifact, recordAsyncOperation } from './artifact'
1313
import { TaskDeadline } from './deadline'
14-
import { PendingError } from './errors'
14+
import { PendingError, TestRunAbortError } from './errors'
1515
import { finishSendTasksUpdate } from './run'
1616
import { getRunner } from './suite'
1717

@@ -116,8 +116,9 @@ export function withTimeout<T extends (...args: any[]) => any>(
116116
} as T
117117
}
118118

119-
export function withCancel<T extends (...args: any[]) => any>(fn: T, signal: AbortSignal): T {
119+
export function withCancel<T extends (...args: any[]) => any>(fn: T, context: TestContext): T {
120120
return function runWithCancel(...args: T extends (...args: infer A) => any ? A : never) {
121+
const signal = context.signal
121122
return new Promise((resolve, reject) => {
122123
const onAbort = () => reject(signal.reason)
123124
signal.addEventListener('abort', onAbort, { once: true })
@@ -163,19 +164,22 @@ export function abortContextSignal(context: TestContext, error: Error): void {
163164
abortController?.abort(error)
164165
}
165166

167+
export function resetContextSignal(context: WriteableTestContext): void {
168+
// the next attempt of a cancelled run starts aborted
169+
if (abortControllers.get(context)?.signal.reason instanceof TestRunAbortError) {
170+
return
171+
}
172+
const abortController = new AbortController()
173+
abortControllers.set(context, abortController)
174+
context.signal = abortController.signal
175+
}
176+
166177
export function createTestContext(test: Test, runner: VitestRunner): TestContext {
167178
const context = function () {
168179
throw new Error('done() callback is deprecated, use promise instead')
169180
} as unknown as WriteableTestContext
170181

171-
let abortController = abortControllers.get(context)
172-
173-
if (!abortController) {
174-
abortController = new AbortController()
175-
abortControllers.set(context, abortController)
176-
}
177-
178-
context.signal = abortController.signal
182+
resetContextSignal(context)
179183
context.task = test
180184

181185
context.skip = (condition?: boolean | string, note?: string): never => {
@@ -237,7 +241,7 @@ export function createTestContext(test: Test, runner: VitestRunner): TestContext
237241
timeout ?? runner.config.hookTimeout,
238242
true,
239243
new Error('STACK_TRACE_ERROR'),
240-
(_, error) => abortController.abort(error),
244+
(_, error) => abortContextSignal(context, error),
241245
),
242246
)
243247
}
@@ -250,7 +254,7 @@ export function createTestContext(test: Test, runner: VitestRunner): TestContext
250254
timeout ?? runner.config.hookTimeout,
251255
true,
252256
new Error('STACK_TRACE_ERROR'),
253-
(_, error) => abortController.abort(error),
257+
(_, error) => abortContextSignal(context, error),
254258
),
255259
)
256260
}

‎packages/vitest/src/runtime/runner/run.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import { getSafeTimers } from '@vitest/utils/timers'
2727
import { limitConcurrency } from '../../utils/limit-concurrency'
2828
import { hasFailed } from '../../utils/tasks'
2929
import { collectTests } from './collect'
30-
import { abortContextSignal } from './context'
30+
import { abortContextSignal, resetContextSignal } from './context'
3131
import {
3232
AroundHookMultipleCallsError,
3333
AroundHookSetupError,
@@ -621,6 +621,9 @@ async function runTest(test: Test, runner: VitestRunner): Promise<void> {
621621
test.result.state = 'run' as TaskState
622622
const retry = getRetryCount(test.retry)
623623
for (let retryCount = 0; retryCount <= retry; retryCount++) {
624+
if (retryCount > 0 || repeatCount > 0) {
625+
resetContextSignal(test.context)
626+
}
624627
const attemptStart = now()
625628
const attemptStartTime = unixNow()
626629
const attemptErrorsStart = test.result.errors?.length ?? 0

‎packages/vitest/src/runtime/runner/suite.ts‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -415,10 +415,7 @@ function createSuiteCollector(
415415
setFn(
416416
task,
417417
withTimeout(
418-
withCancel(
419-
withAwaitAsyncAssertions(withFixtures(handler, { context }), task),
420-
task.context.signal,
421-
),
418+
withCancel(withAwaitAsyncAssertions(withFixtures(handler, { context }), task), context),
422419
timeout,
423420
false,
424421
stackTraceError,

‎test/e2e/test/signal.test.ts‎

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,69 @@ test('timeout aborts all signals in concurrent tests', async () => {
7777
})
7878
})
7979

80+
test('every attempt after a timeout gets a new signal', async () => {
81+
const { buildTree } = await runInlineTests({
82+
'basic.test.ts': /* ts */ `
83+
import { test } from 'vitest'
84+
import { setTimeout } from 'node:timers/promises'
85+
86+
const retryDelays = [1000, 0]
87+
test('retried', { retry: 1, timeout: 100 }, async ({ signal }) => {
88+
await setTimeout(retryDelays.shift(), undefined, { signal })
89+
})
90+
91+
const repeatDelays = [0, 1000, 0]
92+
test('repeated', { repeats: 2, timeout: 100 }, async ({ signal }) => {
93+
await setTimeout(repeatDelays.shift(), undefined, { signal })
94+
})
95+
`,
96+
})
97+
expect(buildTree(getAttempts)).toMatchInlineSnapshot(`
98+
{
99+
"basic.test.ts": {
100+
"repeated": [
101+
"passed",
102+
[
103+
"Test timed out in 100ms.
104+
If this is a long-running test, pass a timeout value as the last argument or configure it globally with "testTimeout".",
105+
],
106+
"passed",
107+
],
108+
"retried": [
109+
[
110+
"Test timed out in 100ms.
111+
If this is a long-running test, pass a timeout value as the last argument or configure it globally with "testTimeout".",
112+
],
113+
"passed",
114+
],
115+
},
116+
}
117+
`)
118+
})
119+
120+
test('onTestFinished timeout aborts the signal of the current attempt', async () => {
121+
const { results } = await runInlineTests({
122+
'basic.test.ts': /* ts */ `
123+
import { test } from 'vitest'
124+
125+
test('retried', { retry: 1 }, ({ signal, task, onTestFinished }) => {
126+
signal.addEventListener('abort', () => {
127+
task.meta.abortedAttempts ??= []
128+
task.meta.abortedAttempts.push(task.result.retryCount)
129+
})
130+
onTestFinished(() => new Promise(() => {}), 50)
131+
})
132+
`,
133+
})
134+
expect(results[0].children.at(0)?.meta()).toEqual({
135+
abortedAttempts: [0, 1],
136+
})
137+
})
138+
139+
function getAttempts(test: TestCase) {
140+
return test.attempts().map((attempt) => attempt.errors?.map((error) => error.message) ?? 'passed')
141+
}
142+
80143
class AbortReporter implements Reporter {
81144
vitest!: Vitest
82145
onInit(vitest: Vitest) {

0 commit comments

Comments
 (0)