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: create prefer-to-be-for-literals #821

Closed
wants to merge 2 commits into from
Closed

Conversation

G-Rath
Copy link
Collaborator

@G-Rath G-Rath commented Apr 11, 2021

Resolves #801

I'm open to bikeshedding on the name:

  • prefer-to-be - we've already had a rule named this, but maybe thats ok?
  • prefer-to-be-for-literals - a little wordy, arguably a little inaccurate
  • prefer-to-be-for-primitive-literal - wordier, but the most accurate

I think this could be good to have in styles config when it comes time to do our next major.

Copy link
Member

@SimenB SimenB left a comment

Choose a reason for hiding this comment

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

I like it!

README.md Outdated Show resolved Hide resolved
docs/rules/prefer-to-be-for-literals.md Outdated Show resolved Hide resolved
src/rules/prefer-to-be-for-literals.ts Outdated Show resolved Hide resolved
@LitoMore
Copy link

LitoMore commented Jul 15, 2021

We should support all literal types, such as null, undefined, and NaN.

And make its API defaults to:

'jest/prefer-to-be-for-literals': ['error', {
 	types: ['number', 'string', 'boolean']
}]

Its full API:

'jest/prefer-to-be-for-literals': ['error', {
 	types: ['number', 'string', 'boolean', 'undefined', 'null', 'NaN']
}]

What do you think?

@razh
Copy link

razh commented Jul 15, 2021

There's already toBeNull() and toBeUndefined(), the former of which is included as a valid case:

valid: [
'expect(null).toBeNull();',
'expect(null).not.toBeNull();',

They're handled by the prefer-to-be-null and prefer-to-be-undefined rules.

I assume that NaN already counts as a primitive literal? Please correct me if I'm wrong!

@LitoMore
Copy link

LitoMore commented Jul 15, 2021

Another brainstorm:

Deprecate prefer-to-be-null and prefer-to-be-undefined.

Use a new rule prefer-to-be instead, and make its API to:

'jest/prefer-to-be': ['error', {
  types: ['number', 'string', 'boolean', 'undefined', 'null', 'NaN']
}]

prefer-to-be-null, prefer-to-be-undefined, prefer-to-be-literals are doing the same thing, we could combine them into one.

We could keep all of them for the current major version, including the prefer-to-be. And mark prefer-to-be-xxx as deprecated. Then remove prefer-to-be-xxx with next major.

Co-authored-by: Simen Bekkhus <sbekkhus91@gmail.com>
@G-Rath
Copy link
Collaborator Author

G-Rath commented Jul 15, 2021

@SimenB cheers, good catch on that silly "too" 😅

@G-Rath
Copy link
Collaborator Author

G-Rath commented Jul 15, 2021

@LitoMore as @razh said, prefer-to-be-undefined & prefer-to-be-null already cover undefined & null, but yeah I had totally forgotten about toBeNaN.

I think doing it all via one prefer-to-be rule makes sense, though I don't think we need the options for now. That'd actually let us support beDefined which is something we'd been stuck on because of the name

@SimenB do you have any opinions on that? mainly around having it in our style config, since prefer-to-be-undefined & prefer-to-be-null are there and so would want to be replaced.

@G-Rath
Copy link
Collaborator Author

G-Rath commented Jul 15, 2021

also, I just realised this errors on toBe itself 🤦

@G-Rath
Copy link
Collaborator Author

G-Rath commented Jul 22, 2021

I've created prefer-to-be in #864 which superseeds this :)

@G-Rath G-Rath closed this Sep 25, 2021
@github-actions
Copy link

🎉 This issue has been resolved in version 24.5.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@github-actions
Copy link

🎉 This issue has been resolved in version 25.0.0-next.5 🎉

The release is available on:

Your semantic-release bot 📦🚀

@G-Rath G-Rath deleted the create-prefer-to-be branch October 3, 2021 01:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[new-rule] recommend toBe over toEqual when expecting primitive literals
4 participants