close

prefer-to-have-length

Configuration

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