Skip to content

prefer-expect-assertions: test-context expect not detected in it.for or when not the first destructured property #968

Description

@christopher-buss

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

Activity

  1. added a commit that references this issue on Sep 30, 2026
    88d0bd8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions