oxc-project/oxc · error · OxcDiagnostic

Prefer `toBeObject()` for object assertions

Error message

Prefer `toBeObject()` for object assertions

What it means

Emitted by the vitest/prefer-to-be-object rule at an object assertion written as a typeof/instanceof/constructor comparison (e.g. `typeof x === 'object'` or `expect(x instanceof Object).toBe(true)`). It fires because vitest's dedicated `toBeObject()` matcher expresses the intent directly.

Source

Thrown at crates/oxc_linter/src/rules/vitest/prefer_to_be_object.rs:19

use oxc_ast::{
    AstKind,
    ast::{Argument, BinaryOperator, Expression},
};
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_span::{GetSpan, Span};

use crate::{
    context::LintContext,
    rule::Rule,
    utils::{
        KnownMemberExpressionProperty, ParsedExpectFnCall, ParsedJestFnCallNew, PossibleJestNode,
        parse_jest_fn_call,
    },
};

fn prefer_to_be_object(span: Span) -> OxcDiagnostic {
    OxcDiagnostic::warn("Prefer `toBeObject()` for object assertions")
        .with_help("Consider using `toBeObject()` to test if a value is an object.")
        .with_label(span)
}

fn is_parsed_instance_of_matcher_call(
    parsed_expect_call: &ParsedExpectFnCall,
    matcher: &KnownMemberExpressionProperty,
) -> bool {
    parsed_expect_call.args.len() == 1
        && matches!(
            parsed_expect_call.args.first(),
            Some(Argument::Identifier(id)) if matcher.name().as_deref() == Some("toBeInstanceOf") && id.name == "Object"
        )
}

#[derive(Debug, Default, Clone)]
pub struct PreferToBeObject;

View on GitHub (pinned to e1e7af627c)

Solutions

  1. Replace the manual object check with `expect(x).toBeObject()`
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at crates/oxc_linter/src/rules/vitest/prefer_to_be_object.rs:19 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/e290472586568030. Report an issue: GitHub.