oxc-project/oxc · warning

Use `toBeDefined` instead.

Error message

Use `toBeDefined` 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:29). The source's `has_not_modifier` branch maps `expect(x).not.toBeUndefined()` (and `toEqual(undefined)` negations) to the dedicated `toBeDefined()` matcher for better failure messages.

Source

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

        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.")
        .with_help(format!("Replace `{source_text}` with `{suggestion}`."))
        .with_label(span)
}

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

View on GitHub (pinned to e1e7af627c)

Solutions

  1. Rewrite as `expect(value).toBeDefined()`.
  2. Keep the positive form for the inverse case: `expect(value).toBeUndefined()`.
  3. Suppress with `// oxlint-disable-next-line jest/prefer-to-be` if the `not` phrasing is intentional.

Example fix

// before
expect(config.apiKey).not.toBeUndefined();

// after
expect(config.apiKey).toBeDefined();
Defensive patterns

Strategy: validation

Validate before calling

// rg -n "\.not\.toBeUndefined\(\)" tests/ -t ts -t js

Prevention

When it happens

Trigger: `expect(value).not.toBeUndefined()` or `expect(value).not.toEqual(undefined)` - an expect call with the `not` modifier paired with an undefined-related equality assertion.

Common situations: Guards asserting that a field was set: `expect(config.apiKey).not.toBeUndefined()`; optionality checks on function results; suites linted with the jest plugin's recommended rules.

Related errors


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