Description
prefer-expect-assertions reports tests that call expect.assertions() on the test-context expect when expect is not the first property of the first destructured parameter. it.for is the common case: the row is the first parameter and the context is the second.
Reproduction
import { it } from 'vitest'
// ❌ reported, but should pass
it.for([{ a: 1 }])('case $a', ({ a }, { expect }) => {
expect.assertions(1)
expect(a).toBe(1)
})
// ❌ reported, but should pass
it('case', ({ task, expect }) => {
expect.assertions(1)
expect(task.name).toBe('case')
})
// ✅ passes
it('case', ({ expect }) => {
expect.assertions(1)
expect(1).toBe(1)
})
Every test should have either `expect.assertions(<number of assertions>)` or `expect.hasAssertions()` as its first expression vitest/prefer-expect-assertions
Cause
resolveScope in src/utils/parse-vitest-fn-call.ts looks only at the first ObjectPattern parameter and only at its first Property. For ({ a }, { expect }) or ({ task, expect }) it returns 'local', so the call is not parsed as expect and the expect.assertions(1) statement is not counted.
Checking every destructured parameter and every property fixes it. This also affects other rules that rely on the testContext resolution.
Version
@vitest/eslint-plugin 1.6.27
Description
prefer-expect-assertionsreports tests that callexpect.assertions()on the test-contextexpectwhenexpectis not the first property of the first destructured parameter.it.foris the common case: the row is the first parameter and the context is the second.Reproduction
Cause
resolveScopeinsrc/utils/parse-vitest-fn-call.tslooks only at the firstObjectPatternparameter and only at its firstProperty. For({ a }, { expect })or({ task, expect })it returns'local', so the call is not parsed asexpectand theexpect.assertions(1)statement is not counted.Checking every destructured parameter and every property fixes it. This also affects other rules that rely on the
testContextresolution.Version
@vitest/eslint-plugin1.6.27