You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
As far as I know, there's nearly no difference in using toBe over toEqual when the expected value is a primitive literal, i.e
expect(response.statusCode).toBe(200);
// vs
expect(response.statusCode).toEqual(200);
expect(response.statusCode).toStrictEqual(200);
expect(isValid(obj)).toBe(true);
// vs
expect(isValid(obj)).toEqual(true);
expect(isValid(obj)).toStrictEqual(true);
expect(join(['hello', 'world')).toBe('hello world');
// vs
expect(join(['hello', 'world')).toEqual('hello world');
expect(join(['hello', 'world')).toStrictEqual('hello world');
IMO toBe(200) reads better than toEqual(200) (or toStrictEqual(200)).
It would be nice to have a rule that checked for toEqual matchers that were being passed primitive literal values, and recommended replacing them with toBe.
This would not apply if you're expecting an object or array literal, nor the value of a variable (even if we could resolve that variable back to a literal, i.e if its const status = 200;).
We also probably shouldn't be reporting on floats? (which we could create prefer-to-be-close-to) - alternatively I don't think it'd be much of a stretch if prefer-to-be supported floats.
do you think we should also handle recommending toBeCloseTo for floats in this rule, or make another rule for that?
G-Rath
changed the title
create prefer-to-be rule when expecting primitive literals
recommend toBe over toEqual when expecting primitive literals
Mar 27, 2021
G-Rath
changed the title
recommend toBe over toEqual when expecting primitive literals
[new-rule] recommend toBe over toEqual when expecting primitive literals
Mar 27, 2021
As far as I know, there's nearly no difference in using
toBe
overtoEqual
when the expected value is a primitive literal, i.eIMO
toBe(200)
reads better thantoEqual(200)
(ortoStrictEqual(200)
).It would be nice to have a rule that checked for
toEqual
matchers that were being passed primitive literal values, and recommended replacing them withtoBe
.This would not apply if you're expecting an object or array literal, nor the value of a variable (even if we could resolve that variable back to a literal, i.e if its
const status = 200;
).We also probably shouldn't be reporting on floats? (which we could create
prefer-to-be-close-to
) - alternatively I don't think it'd be much of a stretch ifprefer-to-be
supported floats.my quick test setup to compare the three matchers
The text was updated successfully, but these errors were encountered: