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);

How to use

To enable this rule in the CLI or using the config file, you can use:

bash
oxlint --deny jest/prefer-to-have-length --jest-plugin
json
{
  "plugins": ["jest"],
  "rules": {
    "jest/prefer-to-have-length": "error"
  }
}

References

Released under the MIT License.