oxc-project/oxc · warning

Use `toBe` when expecting primitive literals.

Error message

Use `toBe` when expecting primitive literals.

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:17). It fires when an equality matcher (`toBe`, `toEqual`, `toStrictEqual`) is called with a primitive literal such as a number, string, or boolean. All equality matchers behave identically for primitives, so the rule recommends `toBe` as the most natural reading.

Source

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

use oxc_ast::{
    AstKind,
    ast::{Argument, CallExpression, Expression},
};
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.")

View on GitHub (pinned to e1e7af627c)

Solutions

  1. Change the matcher to `toBe`: `expect(value).not.toBe(5)`, `expect(getMessage()).toBe('hello world')`.
  2. Run `oxlint --fix` if the rule ships a fixer in your oxlint version, otherwise apply the rename manually.
  3. Suppress the single line with `// oxlint-disable-next-line jest/prefer-to-be` when the literal-style assertion is deliberate.
  4. Turn the rule off repo-wide in `.oxlintrc.json` if the team prefers `toEqual` uniformly.

Example fix

// before
expect(value).not.toEqual(5);
expect(getMessage()).toStrictEqual('hello world');

// after
expect(value).not.toBe(5);
expect(getMessage()).toBe('hello world');
Defensive patterns

Strategy: validation

Validate before calling

// rg -n "\.toEqual\((?:true|false|\d+|')" tests/ -t ts

Prevention

When it happens

Trigger: `expect(value).toEqual(5)`, `expect(getMessage()).toStrictEqual('hello world')`, or `expect(loadMessage()).resolves.toEqual('hello world')` - i.e. an `expect(...)` call whose matcher (any of toBe/toEqual/toStrictEqual) receives a primitive literal argument. The rule is a single implementation shared by the jest and vitest oxlint plugins.

Common situations: Codebases that standardized on `toEqual` everywhere and then enabled the jest plugin's recommended preset; copy-pasted assertions; migration from eslint-plugin-jest where this rule (added in v25.0.0+) was already surfacing.

Related errors


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