Pattern: Direct length comparison
Issue: -
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.
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);