useExpectToContain (JavaScript)
Language JavaScript (and super languages)
Summary
Section titled “Summary”- Rule available since:
v2.6.3 - Diagnostic Category:
lint/nursery/useExpectToContain - 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-contain - Same as
vitest/prefer-to-contain - Same as
playwright/prefer-to-contain
- Same as
How to configure
Section titled “How to configure”{ "linter": { "rules": { "nursery": { "useExpectToContain": "error" } } }}Description
Section titled “Description”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.
Examples
Section titled “Examples”Invalid
Section titled “Invalid”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");Related links
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.