Skip to content

jest/prefer-to-have-length Style

🛠️ An auto-fix is available for this rule.

What it does

In order to have a better failure message, toHaveLength() should be used upon asserting expectations on objects length property.

Why is this bad?

This rule triggers a warning if toBe(), toEqual() or toStrictEqual() is used to assert objects length property.

Example

javascript
// valid
expect.hasAssertions;
expect.hasAssertions();
expect(files).toHaveLength(1);
expect(files.name).toBe("file");

// invalid
expect(files["length"]).toBe(1);
expect(files["length"]).toBe(1);
expect(files["length"])["not"].toBe(1);

References

Released under the MIT License.