Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added the prefer-strict-boolean-matchers rule #650

Merged
merged 3 commits into from
Feb 4, 2025

Conversation

marekdedic
Copy link
Contributor

@marekdedic marekdedic commented Feb 4, 2025

I am explicitly not reporting for .toEqual(true) or .toStrictEqual(true), there's already prefer-to-be for that.

Closes #639

@marekdedic marekdedic changed the title feat: added the prefere-strict-boolean-matchers rule feat: added the prefer-strict-boolean-matchers rule Feb 4, 2025
@marekdedic marekdedic force-pushed the prefer-strict-boolean-matchers branch from e57a83a to 0baa457 Compare February 4, 2025 10:31
@marekdedic marekdedic marked this pull request as ready for review February 4, 2025 12:59
@marekdedic
Copy link
Contributor Author

Additionally, IMO, this should be in the recommended ruleset, but that should probably come in a major version down the line

Copy link
Member

@veritem veritem left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks!

@veritem veritem merged commit 4d86836 into vitest-dev:main Feb 4, 2025
@marekdedic marekdedic deleted the prefer-strict-boolean-matchers branch February 5, 2025 10:26
@FloEdelmann
Copy link
Contributor

Nice, thanks for adding this! Unfortunately, the rule is not documented in docs/rules or in the readme. Could you please add this?

@veritem
Copy link
Member

veritem commented Feb 7, 2025

aah, I forgot about that. @marekdedic, could you please add documentation for this rule? the docs generator is broken due to dependency issues. Feel free to look at how other rules are documented as a reference.

@marekdedic
Copy link
Contributor Author

Ok, will add it in a separate PR :)

@marekdedic
Copy link
Contributor Author

Hmm, I just realized I added an auto-fix for this rule, but it just replaces .toBeTruthy with .toBe(true) - which can make some tests fail if they rely on type coercion. Is that OK?

@marekdedic
Copy link
Contributor Author

See #653

@veritem
Copy link
Member

veritem commented Feb 7, 2025

Hmm, I just realized I added an auto-fix for this rule, but it just replaces .toBeTruthy with .toBe(true) - which can make some tests fail if they rely on type coercion. Is that OK?

i think that's okay, did you want todo otherwise?

@marekdedic
Copy link
Contributor Author

i think that's okay, did you want todo otherwise?

No, I'm fine with it, just wanted to make sure it's ok for the autofix to potentially break tests :) If so (I think it's warranted here), then everything's ok :)

@gtbuchanan
Copy link

The introduction of this rule in the all config causes an infinite recursive fix situation. Using toBe(true) triggers prefer-to-be-truthy, but changing it to toBeTruthy() results in prefer-strict-boolean-matchers.

@marekdedic
Copy link
Contributor Author

Hi, yes, that is true, they are basically opposites of each other. @veritem it seems to me that the all config is automatically updated, so I don't know how to fix this :/

@veritem
Copy link
Member

veritem commented Feb 12, 2025

@marekdedic
we can disable this rule by default and have users enable it when they want. check in src/index.ts and change this rule from warn to off

@marekdedic
Copy link
Contributor Author

Ok, that makes sense. One question though: Wouldn't it be better to have this rule on by default instead of prefer-to-be-truthy? This rule forces the users to be more precise with their matchers, lowering the chance of a misbehaving test. The other one does the exact opposite and I honestly see it as bad advice and a bit of an antipattern...

@FloEdelmann
Copy link
Contributor

I agree, prefer-to-be-truthy and prefer-to-be-falsy are less useful than this new rule.

@veritem
Copy link
Member

veritem commented Feb 12, 2025

prefer-to-be-truthy has been turned off by default in favor of this rule

@marekdedic
Copy link
Contributor Author

Aaah, sorry, prefer-to-be-falsy has to be turned off as well, sorry, I didn't write that down exactly...

@veritem
Copy link
Member

veritem commented Feb 12, 2025

done

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add rule disallowing toBeTruthy
4 participants