Skip to content

useExpectToHaveLength (JavaScript)

Language JavaScript (and super languages)
biome.json
{
"linter": {
"rules": {
"nursery": {
"useExpectToHaveLength": "error"
}
}
}
}

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.

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