File tree Expand file tree Collapse file tree
packages/vitest/src/runtime Expand file tree Collapse file tree Original file line number Diff line number Diff 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
Original file line number Diff line number Diff 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 > {
Original file line number Diff line number Diff 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+ } )
You can’t perform that action at this time.
0 commit comments