Skip to content

useExpectToContain (JavaScript)

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

Enforce using toContain() to check whether an array or string contains a value.

Checking the result of includes() with toBe(), toEqual(), or toStrictEqual() works, but when the test fails, the error message only says that true was expected and false was received. toContain() reports the value that was searched and the item that was missing, which makes failures easier to understand.

The fix is unsafe because includes() and toContain() don’t compare values in exactly the same way. For example, [NaN].includes(NaN) is true, while expect([NaN]).toContain(NaN) fails.

expect(fruits.includes("apple")).toBe(true);
code-block.js:1:34 lint/nursery/useExpectToContain  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ The result of includes() is compared to a boolean.

> 1 │ expect(fruits.includes("apple")).toBe(true);
│ ^^^^
2 │

ℹ When this check fails, the error message only shows that a boolean didn't match, not which value was missing.

ℹ Use toContain() to check whether a value is present.

ℹ 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 toContain() instead.

1 │ - expect(fruits.includes("apple")).toBe(true);
1 │ + expect(fruits).toContain("apple");
2 2 │

expect(fruits.includes("apple")).toEqual(false);
code-block.js:1:34 lint/nursery/useExpectToContain  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ The result of includes() is compared to a boolean.

> 1 │ expect(fruits.includes("apple")).toEqual(false);
│ ^^^^^^^
2 │

ℹ When this check fails, the error message only shows that a boolean didn't match, not which value was missing.

ℹ Use toContain() to check whether a value is present.

ℹ 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 toContain() instead.

1 │ - expect(fruits.includes("apple")).toEqual(false);
1 │ + expect(fruits).not.toContain("apple");
2 2 │

expect(fruits.includes("apple")).not.toStrictEqual(true);
code-block.js:1:38 lint/nursery/useExpectToContain  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ The result of includes() is compared to a boolean.

> 1 │ expect(fruits.includes("apple")).not.toStrictEqual(true);
│ ^^^^^^^^^^^^^
2 │

ℹ When this check fails, the error message only shows that a boolean didn't match, not which value was missing.

ℹ Use toContain() to check whether a value is present.

ℹ 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 toContain() instead.

1 │ - expect(fruits.includes("apple")).not.toStrictEqual(true);
1 │ + expect(fruits).not.toContain("apple");
2 2 │

expect(fruits).toContain("apple");
expect(fruits).not.toContain("apple");