{"record":{"id":"13106d928fae3d2b","repo":"oxc-project/oxc","slug":"suggest-using-tohavelength","errorCode":null,"errorMessage":"Suggest using `toHaveLength()`.","messagePattern":"Suggest using `toHaveLength\\(\\)`\\.","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"crates/oxc_linter/src/rules/shared/jest_vitest/prefer_to_have_length.rs","lineNumber":14,"sourceCode":"use oxc_ast::{\n    AstKind,\n    ast::{CallExpression, Expression, MemberExpression, match_member_expression},\n};\nuse oxc_diagnostics::OxcDiagnostic;\nuse oxc_span::{GetSpan, Span};\n\nuse crate::{\n    context::LintContext,\n    utils::{ParsedExpectFnCall, PossibleJestNode, is_equality_matcher, parse_expect_jest_fn_call},\n};\n\nfn use_to_have_length(span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(\"Suggest using `toHaveLength()`.\").with_label(span)\n}\n\npub const DOCUMENTATION: &str = r#\"### What it does\n\nIn order to have a better failure message, `toHaveLength()` should be used upon\nasserting expectations on objects length property.\n\n### Why is this bad?\n\nThis rule triggers a warning if `toBe()`, `toEqual()` or `toStrictEqual()` is\nused to assert objects length property.\n\n### Examples\n\nExamples of **incorrect** code for this rule:\n```javascript\nexpect(files[\"length\"]).toBe(1);\nexpect(files[\"length\"]).toBe(1,);","sourceCodeStart":1,"sourceCodeEnd":32,"githubUrl":"https://github.com/oxc-project/oxc/blob/e1e7af627c8843ab64044ed466b128fcc21a035b/crates/oxc_linter/src/rules/shared/jest_vitest/prefer_to_have_length.rs#L1-L32","documentation":"Warning from the oxlint `jest/prefer-to-have-length` rule (source: crates/oxc_linter/src/rules/shared/jest_vitest/prefer_to_have_length.rs:14). It fires when `toBe()`, `toEqual()`, or `toStrictEqual()` is used to assert an object's `length` property, and recommends `toHaveLength()`, whose failure output prints both the received value and its length.","triggerScenarios":"`expect(arr.length).toBe(3)`, `expect(str.length).toEqual(0)`, `expect(container.children.length).toStrictEqual(2)` - the expect argument is a `.length` member expression and the matcher is an equality matcher (checked via `is_equality_matcher` in the source).","commonSituations":"Size assertions on arrays, strings, query results, or DOM children written in the natural `.length` style; suites newly covered by the jest plugin preset; ported code from teams with different matcher conventions.","solutions":["Move the object into `expect` and use the matcher: `expect(arr).toHaveLength(3)`.","For empty checks prefer the clearer `expect(arr).toHaveLength(0)` (or `expect(arr).toBeEmpty()` with jest-extended if configured).","Suppress inline with `// oxlint-disable-next-line jest/prefer-to-have-length` when the length value itself is what is under test."],"exampleFix":"// before\nexpect(results.length).toBe(3);\n\n// after\nexpect(results).toHaveLength(3);","handlingStrategy":"validation","validationCode":"// rg -n \"\\.length\\)\\.(toBe|toEqual|toStrictEqual)\\(\" tests/ -t ts -t js","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Put the container in expect() and assert with toHaveLength","Keep prefer-to-have-length enabled so .length equality is flagged at lint time"],"tags":["jest","vitest","oxlint","matcher","length"],"backgroundTag":"jest-tohavelength-assertion","analyzedSha":"e1e7af627c8843ab64044ed466b128fcc21a035b","analyzedAt":"2026-08-20T07:01:07.079Z","contentChangedAt":"2026-08-20T07:01:07.079Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}