useExpectEqualityMatcher (JavaScript)
Language JavaScript (and super languages)
Summary
Section titled “Summary”- Rule available since:
v2.6.9 - Diagnostic Category:
lint/nursery/useExpectEqualityMatcher - 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-equality-matcher - Same as
vitest/prefer-equality-matcher - Same as
playwright/prefer-equality-matcher
- Same as
How to configure
Section titled “How to configure”{ "linter": { "rules": { "nursery": { "useExpectEqualityMatcher": "error" } } }}Description
Section titled “Description”Enforce using an equality matcher instead of comparing values with === or !== inside expect().
Passing a === b to expect() and checking the result with toBe(true) works, but when
the test fails, the error message only says that true was expected and false was
received. Passing a to expect() and b to an equality matcher, such as toBe(),
toEqual(), or toStrictEqual(), makes the error message show both values.
The fix uses toBe(), which is the matcher closest to ===. It is unsafe because
toBe() and === don’t compare every value in the same way. For example,
NaN === NaN is false, while expect(NaN).toBe(NaN) passes.
Examples
Section titled “Examples”Invalid
Section titled “Invalid”expect(total === 10).toBe(true);code-block.js:1:22 lint/nursery/useExpectEqualityMatcher FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ The result of a === comparison is checked against a boolean.
> 1 │ expect(total === 10).toBe(true);
│ ^^^^
2 │
ℹ When this check fails, the error message only shows that a boolean didn't match, not the values that were compared.
ℹ Pass the left value to expect() and the right value to an equality matcher such as toBe(), toEqual(), or toStrictEqual().
ℹ 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 toBe() instead.
1 │ - expect(total·===·10).toBe(true);
1 │ + expect(total).toBe(10);
2 2 │
expect(name !== "admin").toEqual(true);code-block.js:1:26 lint/nursery/useExpectEqualityMatcher FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ The result of a !== comparison is checked against a boolean.
> 1 │ expect(name !== "admin").toEqual(true);
│ ^^^^^^^
2 │
ℹ When this check fails, the error message only shows that a boolean didn't match, not the values that were compared.
ℹ Pass the left value to expect() and the right value to a negated equality matcher such as .not.toBe(), .not.toEqual(), or .not.toStrictEqual().
ℹ 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 toBe() instead.
1 │ - expect(name·!==·"admin").toEqual(true);
1 │ + expect(name).not.toBe("admin");
2 2 │
expect(user.id === 1).not.toStrictEqual(false);code-block.js:1:27 lint/nursery/useExpectEqualityMatcher FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ The result of a === comparison is checked against a boolean.
> 1 │ expect(user.id === 1).not.toStrictEqual(false);
│ ^^^^^^^^^^^^^
2 │
ℹ When this check fails, the error message only shows that a boolean didn't match, not the values that were compared.
ℹ Pass the left value to expect() and the right value to an equality matcher such as toBe(), toEqual(), or toStrictEqual().
ℹ 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 toBe() instead.
1 │ - expect(user.id·===·1).not.toStrictEqual(false);
1 │ + expect(user.id).toBe(1);
2 2 │
expect(total).toBe(10);expect(name).not.toEqual("admin");expect(user.id).toStrictEqual(1);Related links
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.