useExpectToHaveLength (JavaScript)
Language JavaScript (and super languages)
Summary
Section titled “Summary”- Rule available since:
v2.6.9 - Diagnostic Category:
lint/nursery/useExpectToHaveLength - This rule has an unsafe fix.
- The default severity of this rule is information.
- This rule belongs to the following domains:
- Sources:
- Same as
jest/prefer-to-have-length - Same as
vitest/prefer-to-have-length - Same as
playwright/prefer-to-have-length
- Same as
How to configure
Section titled “How to configure”{ "linter": { "rules": { "nursery": { "useExpectToHaveLength": "error" } } }}Description
Section titled “Description”Enforce using toHaveLength() to check the length of an array or string.
Checking the length property with toBe(), toEqual(), or toStrictEqual() works,
but when the test fails, the error message only shows the two numbers that didn’t match.
toHaveLength() also shows the value whose length was checked, which makes failures
easier to understand.
The fix is unsafe because toHaveLength() fails when the value doesn’t have a length
property that is a number, and an equality check doesn’t. For example,
expect({}.length).not.toBe(1) passes, while expect({}).not.toHaveLength(1) fails.
Examples
Section titled “Examples”Invalid
Section titled “Invalid”expect(files.length).toBe(1);code-block.js:1:22 lint/nursery/useExpectToHaveLength FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ The length property is checked with toBe().
> 1 │ expect(files.length).toBe(1);
│ ^^^^
2 │
ℹ When this check fails, the error message only shows the two numbers that didn't match, not the value whose length was checked.
ℹ Use toHaveLength() to check the length of a value.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
ℹ Unsafe fix: Use toHaveLength() instead.
1 │ - expect(files.length).toBe(1);
1 │ + expect(files).toHaveLength(1);
2 2 │
expect(files.length).toEqual(1);code-block.js:1:22 lint/nursery/useExpectToHaveLength FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ The length property is checked with toEqual().
> 1 │ expect(files.length).toEqual(1);
│ ^^^^^^^
2 │
ℹ When this check fails, the error message only shows the two numbers that didn't match, not the value whose length was checked.
ℹ Use toHaveLength() to check the length of a value.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
ℹ Unsafe fix: Use toHaveLength() instead.
1 │ - expect(files.length).toEqual(1);
1 │ + expect(files).toHaveLength(1);
2 2 │
expect(files.length).not.toStrictEqual(1);code-block.js:1:26 lint/nursery/useExpectToHaveLength FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ The length property is checked with toStrictEqual().
> 1 │ expect(files.length).not.toStrictEqual(1);
│ ^^^^^^^^^^^^^
2 │
ℹ When this check fails, the error message only shows the two numbers that didn't match, not the value whose length was checked.
ℹ Use toHaveLength() to check the length of a value.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
ℹ Unsafe fix: Use toHaveLength() instead.
1 │ - expect(files.length).not.toStrictEqual(1);
1 │ + expect(files).not.toHaveLength(1);
2 2 │
expect(files).toHaveLength(1);expect(files).not.toHaveLength(1);expect(files.name).toBe("report.txt");Related links
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.