oxc-project/oxc · warning
Suggest using `toContain()`.
Error message
Suggest using `toContain()`.
What it means
Warning from the oxlint `jest/prefer-to-contain` rule (source: crates/oxc_linter/src/rules/shared/jest_vitest/prefer_to_contain.rs:18). The rule inspects `expect()` calls whose argument is an `includes(...)` call and whose matcher is an equality matcher (`toBe`/`toEqual`/`toStrictEqual`) with a boolean literal; it recommends `toContain()`, whose failure output shows the actual array contents.
Source
Thrown at crates/oxc_linter/src/rules/shared/jest_vitest/prefer_to_contain.rs:18
use oxc_ast::{
AstKind,
ast::{Argument, CallExpression, Expression},
};
use oxc_diagnostics::OxcDiagnostic;
use oxc_ecmascript::{ToBoolean, WithoutGlobalReferenceInformation};
use oxc_span::Span;
use crate::{
context::LintContext,
utils::{
KnownMemberExpressionParentKind, PossibleJestNode, is_equality_matcher,
parse_expect_jest_fn_call,
},
};
fn use_to_contain(span: Span) -> OxcDiagnostic {
OxcDiagnostic::warn("Suggest using `toContain()`.").with_label(span)
}
pub const DOCUMENTATION: &str = r"### What it does
In order to have a better failure message, `toContain()` should be used upon
asserting expectations on an array containing an object.
### Why is this bad?
This rule triggers a warning if `toBe()`, `toEqual()` or `toStrictEqual()` is
used to assert object inclusion in an array
### Examples
Examples of **incorrect** code for this rule:
```javascript
expect(a.includes(b)).toBe(true);
expect(a.includes(b)).not.toBe(true);View on GitHub (pinned to e1e7af627c)
Solutions
- Rewrite the assertion to move the array into `expect` and use `toContain`: `expect(arr).toContain(item)`.
- For negative membership use `expect(arr).not.toContain(item)`.
- Suppress inline with `// oxlint-disable-next-line jest/prefer-to-contain` when asserting on the boolean itself is the point (e.g. testing an `includes` polyfill).
Example fix
// before
expect(allowedUsers.includes('alice')).toBe(true);
// after
expect(allowedUsers).toContain('alice'); Defensive patterns
Strategy: validation
Validate before calling
// rg -n "\.includes\([^)]*\)\)\.(toBe|toEqual|toStrictEqual)\((true|false)\)" tests/ -t ts -t js
Prevention
- Write membership assertions directly with toContain from the start
- When reviewing, flag any expect() whose argument is an includes() call
When it happens
Trigger: `expect(arr.includes(item)).toBe(true)`, `expect(arr.includes(item)).toEqual(false)`, or `expect(list.includes(x)).not.toBe(true)` - i.e. an equality matcher applied to the boolean result of `Array.prototype.includes`. The implementation uses `oxc_ecmascript::ToBoolean` (with `WithoutGlobalReferenceInformation`) to recognize the boolean-literal argument.
Common situations: Developers unfamiliar with `toContain` asserting membership the manual way; refactors of loops that used `indexOf(...) !== -1`; CI running oxlint with the jest plugin after migration from ESLint.
Related errors
- Suggest using `toStrictEqual()`.
- Use `toBe` when expecting primitive literals.
- Use `toBeUndefined` instead.
- Use `toBeDefined` instead.
- Use `toBeNull` instead.
AI-assisted analysis of oxc-project/oxc@e1e7af627c (2026-08-20).
Data as JSON: /api/errors/07606644c22b4d5a.
Report an issue: GitHub.