oxc-project/oxc · warning

Suggest using `toBeCalledWith()` or `toHaveBeenCalledWith()`

Error message

Suggest using `toBeCalledWith()` or `toHaveBeenCalledWith()`.

What it means

This is the oxlint `prefer-called-with` rule (jest/vitest plugin), diagnostic for the `toBeCalled()` matcher form. Asserting only that a mock was called proves nothing about how it was used; the rule wants the argument-asserting variants `toBeCalledWith(...)` / `toHaveBeenCalledWith(...)`. This entry (the `use_to_be_called_with` constructor) fires specifically when the parsed expect matcher chain ends in `.toBeCalled()`.

Source

Thrown at crates/oxc_linter/src/rules/shared/jest_vitest/prefer_called_with.rs:11

use oxc_ast::AstKind;
use oxc_diagnostics::OxcDiagnostic;
use oxc_span::Span;

use crate::{
    context::LintContext,
    utils::{PossibleJestNode, parse_expect_jest_fn_call},
};

fn use_to_be_called_with(span: Span) -> OxcDiagnostic {
    OxcDiagnostic::warn("Suggest using `toBeCalledWith()` or `toHaveBeenCalledWith()`.")
        .with_help("Prefer toBeCalledWith(/* expected args */)")
        .with_label(span)
}

fn use_have_been_called_with(span: Span) -> OxcDiagnostic {
    OxcDiagnostic::warn("Suggest using `toBeCalledWith()` or `toHaveBeenCalledWith()`.")
        .with_help("Prefer toHaveBeenCalledWith(/* expected args */)")
        .with_label(span)
}

pub const DOCUMENTATION: &str = r"### What it does

Suggest using `toBeCalledWith()` or `toHaveBeenCalledWith()`

### Why is this bad?

When testing function calls, it's often more valuable to assert both
that a function was called AND what arguments it was called with.

View on GitHub (pinned to e1e7af627c)

Solutions

  1. Replace `expect(mock).toBeCalled()` with `expect(mock).toBeCalledWith(expectedArg1, ...)` supplying the real expected arguments.
  2. If any invocation is acceptable, use `expect(mock.mock.calls).toContainEqual([arg])` or assert on `mock.calls.length` deliberately instead of leaving a bare toBeCalled.
  3. If the call genuinely takes no arguments, `expect(mock).toBeCalledWith()` (zero args) still satisfies the rule and pins the arity.

Example fix

// before
expect(loggingFn).toBeCalled();

// after
expect(loggingFn).toBeCalledWith('user-created', 42);
Defensive patterns

Strategy: validation

Validate before calling

// .oxlintrc.json
{ "rules": { "jest/prefer-called-with": "error" } }

npx oxlint tests/

Prevention

When it happens

Trigger: Any `expect(mockFn).toBeCalled()` expression recognized by `parse_expect_jest_fn_call` where the matcher name is exactly `toBeCalled` (also via aliases like `it` blocks, `expect.soft`, etc.).

Common situations: Quick smoke tests that check a spy fired; legacy codebases written before argument matchers were widely used; auto-completed matcher names where the developer accepted `toBeCalled` instead of `toBeCalledWith`.

Related errors


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