For AI agents: the complete documentation index is available at https://rslint.rs/llms.txt, the full documentation bundle is available at https://rslint.rs/llms-full.txt, and this page is available as Markdown at https://rslint.rs/rules/jest/prefer-to-have-length.md.
close

prefer-to-have-length

Added in v0.4.2

Configuration

PresetConfigured Value
✅ jestPlugin.configs.all"error"
✅ jestPlugin.configs.style"error"
rslint.config.ts
import { defineConfig, jestPlugin } from '@rslint/core';

export default defineConfig([
  jestPlugin.configs.recommended,
  {
    rules: {
      'jest/prefer-to-have-length': 'error',
    },
  },
]);

Rule Details

Prefer toHaveLength() when asserting the length of a value. It produces clearer failure output than comparing .length with toBe(), toEqual(), or toStrictEqual().

Examples of incorrect code for this rule:

expect(files.length).toBe(1);

expect(files.length).toEqual(1);

expect(files.length).toStrictEqual(1);

Examples of correct code for this rule:

expect(files).toHaveLength(1);

Original Documentation