oxc-project/oxc · warning

Use `toBeNull` instead.

Error message

Use `toBeNull` 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:35). When an equality matcher receives the `null` literal (detected by the `first_matcher_arg.is_null()` style check in the run implementation), the rule recommends the dedicated `toBeNull()` matcher.

Source

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

    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

Recommends using `toBe` matcher for primitive literals and specific
matchers for `null`, `undefined`, and `NaN`.

### Why is this bad?

When asserting against primitive literals such as numbers and strings,

View on GitHub (pinned to e1e7af627c)

Solutions

  1. Change to `expect(value).toBeNull()`.
  2. For negations use `expect(value).not.toBeNull()` (still allowed) or restructure to a more specific matcher if one fits.
  3. Suppress inline with `// oxlint-disable-next-line jest/prefer-to-be` for deliberate cases.

Example fix

// before
expect(queryResult.row).toEqual(null);

// after
expect(queryResult.row).toBeNull();
Defensive patterns

Strategy: validation

Validate before calling

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

Prevention

When it happens

Trigger: `expect(value).toEqual(null)`, `expect(value).toBe(null)`, or `expect(value).toStrictEqual(null)` - any equality matcher whose single argument is the literal `null`.

Common situations: Asserting absent optional values such as `expect(queryResult.row).toEqual(null)`; migrating codebases where `toEqual(null)` was the house style; enabling jest recommended rules in oxlint CI.

Related errors


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