Skip to content

useExpectEqualityMatcher (JavaScript)

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

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.

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