oxc-project/oxc · warning

Use `toBeUndefined` instead.

Error message

Use `toBeUndefined` instead.

What it means

Warning from the oxlint `jest/prefer-to-be` rule (source: crates/oxc_linter/src/rules/shared/jest_vitest/prefer_to_be.rs:23). The implementation detects `expect(...)` chained with an equality matcher whose argument is the `undefined` literal, and suggests the dedicated `toBeUndefined()` matcher, which yields clearer failure output.

Source

Thrown at crates/oxc_linter/src/rules/shared/jest_vitest/prefer_to_be.rs:23

use oxc_diagnostics::OxcDiagnostic;
use oxc_span::Span;

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

fn use_to_be(source_text: &str, suggestion: &str, span: Span) -> OxcDiagnostic {
    OxcDiagnostic::warn("Use `toBe` when expecting primitive literals.")
        .with_help(format!("Replace `{source_text}` with `{suggestion}`."))
        .with_label(span)
}

fn use_to_be_undefined(source_text: &str, suggestion: &str, span: Span) -> OxcDiagnostic {
    OxcDiagnostic::warn("Use `toBeUndefined` instead.")
        .with_help(format!("Replace `{source_text}` with `{suggestion}`."))
        .with_label(span)
}

fn use_to_be_defined(source_text: &str, suggestion: &str, span: Span) -> OxcDiagnostic {
    OxcDiagnostic::warn("Use `toBeDefined` instead.")
        .with_help(format!("Replace `{source_text}` with `{suggestion}`."))
        .with_label(span)
}

fn use_to_be_null(source_text: &str, suggestion: &str, span: Span) -> OxcDiagnostic {
    OxcDiagnostic::warn("Use `toBeNull` instead.")
        .with_help(format!("Replace `{source_text}` with `{suggestion}`."))
        .with_label(span)
}

fn use_to_be_na_n(source_text: &str, suggestion: &str, span: Span) -> OxcDiagnostic {
    OxcDiagnostic::warn("Use `toBeNaN` instead.")

View on GitHub (pinned to e1e7af627c)

Solutions

  1. Replace with `expect(value).toBeUndefined()`.
  2. For negated forms use the mirror matcher: `expect(value).toBeDefined()` instead of `expect(value).not.toBeUndefined()` (the rule flags both directions).
  3. Suppress inline with `// oxlint-disable-next-line jest/prefer-to-be` if needed.

Example fix

// before
expect(cache.get('missing')).toEqual(undefined);

// after
expect(cache.get('missing')).toBeUndefined();
Defensive patterns

Strategy: validation

Validate before calling

// rg -n "\.toEqual\(undefined\)" tests/ -t ts -t js

Prevention

When it happens

Trigger: `expect(value).toEqual(undefined)` or `expect(value).toBe(undefined)` (any equality matcher with `undefined` as the argument). Also fires through the negation path: `expect(value).not.toBeUndefined()` maps to `toBeDefined`, per the `has_not_modifier` branch in the source.

Common situations: Asserting that a lookup returned nothing, e.g. `expect(map.get(key)).toEqual(undefined)`; refactors after enabling the jest recommended preset in oxlint; code ported from other frameworks that use `assert(value === undefined)`.

Related errors


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