oxc-project/oxc · error · OxcDiagnostic

This test should have either `{prefix}.assertions(<number of

Error message

This test should have either `{prefix}.assertions(<number of assertions>)` or `{prefix}.hasAssertions()` as its first expression.

What it means

Emitted by the vitest/prefer-expect-assertions rule when a test body neither begins with `expect.assertions(n)` nor `expect.hasAssertions()`. The rule enforces an explicit assertion-count guard at the start of each test so silently passing tests with zero assertions are caught.

Source

Thrown at crates/oxc_linter/src/rules/vitest/prefer_expect_assertions.rs:27

use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_semantic::NodeId;
use oxc_span::Span;
use oxc_str::CompactStr;

use crate::{
    context::LintContext,
    fixer::RuleFix,
    rule::{DefaultRuleConfig, Rule},
    rules::shared::prefer_expect_assertions::{
        CallbackBody, DOCUMENTATION, PreferExpectAssertionsConfig, PreferExpectAssertionsRuleImpl,
        resolve_expect_local_name, should_check,
    },
    utils::{collect_possible_jest_call_node, get_node_name},
};

fn have_expect_assertions(span: Span, prefix: &str) -> OxcDiagnostic {
    OxcDiagnostic::warn(format!(
        "This test should have either `{prefix}.assertions(<number of assertions>)` or `{prefix}.hasAssertions()` as its first expression.",
    ))
    .with_help(format!("Add `{prefix}.hasAssertions()` or `{prefix}.assertions(<number>)` as the first statement in this test."))
    .with_label(span)
}

#[derive(Debug, Default, Clone, Deserialize)]
pub struct PreferExpectAssertions(Box<PreferExpectAssertionsConfig>);

impl std::ops::Deref for PreferExpectAssertions {
    type Target = PreferExpectAssertionsConfig;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

declare_oxc_lint!(

View on GitHub (pinned to e1e7af627c)

Solutions

  1. Add `expect.assertions(n)` as the first expression of the test, where n is the number of expected assertions
  2. Or add `expect.hasAssertions()` as the first expression
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at crates/oxc_linter/src/rules/vitest/prefer_expect_assertions.rs:27 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of oxc-project/oxc@e1e7af627c (2026-08-20). Data as JSON: /api/errors/c06f659cf7c72155. Report an issue: GitHub.