Skip to content

Commit 50dcdb3

Browse files
hi-ogawaclaude
andauthored
fix: reset local expect state between retries for expect.assertions/hasAssertions (#11900)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 71ad4df commit 50dcdb3

3 files changed

Lines changed: 47 additions & 6 deletions

File tree

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1366,7 +1366,10 @@ export interface TestContext {
13661366
* @see {@link https://vitest.dev/guide/benchmarking}
13671367
*/
13681368
readonly bench: Bench
1369-
/** @internal */
1369+
/**
1370+
* Whether the test used its own `expect`. Reading this does not create it.
1371+
* @internal
1372+
*/
13701373
_local: boolean
13711374
}
13721375

‎packages/vitest/src/runtime/runners/test.ts‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -174,18 +174,24 @@ export class TestRunner implements VitestTestRunner {
174174
}
175175
clearModuleMocks(this.config)
176176
this.snapshotClient.clearTest(test.file.filepath, test.id)
177+
const assertionState = {
178+
assertionCalls: 0,
179+
isExpectingAssertions: false,
180+
isExpectingAssertionsError: null,
181+
expectedAssertionsNumber: null,
182+
expectedAssertionsNumberErrorGen: null,
183+
}
177184
setState(
178185
{
179-
assertionCalls: 0,
180-
isExpectingAssertions: false,
181-
isExpectingAssertionsError: null,
182-
expectedAssertionsNumber: null,
183-
expectedAssertionsNumberErrorGen: null,
186+
...assertionState,
184187
currentTestName: getTestName(test),
185188
snapshotState: this.snapshotClient.getSnapshotState(test.file.filepath),
186189
},
187190
(globalThis as any)[GLOBAL_EXPECT],
188191
)
192+
if (test.type === 'test' && test.context._local) {
193+
setState(assertionState, test.context.expect)
194+
}
189195
}
190196

191197
async onAfterRetryTask(test: Test, _options: TestTryOptions): Promise<void> {

‎test/e2e/test/retry.test.ts‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,3 +461,35 @@ describe('failure summary header', () => {
461461
`)
462462
})
463463
})
464+
465+
test('local expect counts assertions per retry and repeat', async () => {
466+
const { stderr, errorTree } = await runInlineTests({
467+
'basic.test.js': `
468+
import { test } from 'vitest'
469+
470+
let attempt = 0
471+
test('retry', { retry: 1 }, ({ expect }) => {
472+
expect.assertions(1)
473+
expect(1).toBe(1)
474+
if (++attempt === 1) {
475+
throw new Error('first attempt')
476+
}
477+
})
478+
479+
test('repeats', { repeats: 1 }, ({ expect }) => {
480+
expect.assertions(1)
481+
expect(1).toBe(1)
482+
})
483+
`,
484+
})
485+
486+
expect(stderr).toBe('')
487+
expect(errorTree()).toMatchInlineSnapshot(`
488+
{
489+
"basic.test.js": {
490+
"repeats": "passed",
491+
"retry": "passed",
492+
},
493+
}
494+
`)
495+
})

0 commit comments

Comments
 (0)