Skip to content

Files

Latest commit

 

History

History
23 lines (17 loc) · 577 Bytes

prefer-to-have-length.md

File metadata and controls

23 lines (17 loc) · 577 Bytes

Pattern: Direct length comparison

Issue: -

Description

Using toBe() or toEqual() to check length properties provides less readable error messages. The toHaveLength() matcher is specifically designed for checking array and string lengths with clearer failure output.

Examples

Example of incorrect code:

expect(array.length).toBe(2);
expect(string.length).toEqual(5);
expect(list["length"]).toBe(0);

Example of correct code:

expect(array).toHaveLength(2);
expect(string).toHaveLength(5);
expect(list).toHaveLength(0);