-
Notifications
You must be signed in to change notification settings - Fork 241
feat: create prefer-to-be
rule
#864
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
Merged
Merged
Changes from 9 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
cfd40ce
feat: create `prefer-to-be`
G-Rath 97bd9cd
feat(prefer-to-be): support `null`
G-Rath 8160cef
feat(prefer-to-be): support `undefined`
G-Rath fe2754f
feat(prefer-to-be): support `NaN`
G-Rath 45a7b83
refactor(prefer-to-be): simplify matcher checks
G-Rath 5b843f2
feat(prefer-to-be): support `toBeDefined`
G-Rath 4eb2333
refactor(prefer-to-be): deduplicate `context.report` calls
G-Rath 8201c23
refactor(prefer-to-be): simplify some checks
G-Rath 858a1a8
docs(prefer-to-be): update with info about advanced `toBe*` matchers
G-Rath 04a9884
chore(prefer-to-be): add a few more tests and mention boolean literals
G-Rath File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# Suggest using `toBe()` for primitive literals (`prefer-to-be`) | ||
|
||
When asserting against primitive literals such as numbers and strings, the | ||
equality matchers all operate the same, but read slightly differently in code. | ||
|
||
This rule recommends using the `toBe` matcher in these situations, as it forms | ||
the most grammatically natural sentence. For `null`, `undefined`, and `NaN` this | ||
rule recommends using their specific `toBe` matchers, as they give better error | ||
messages as well. | ||
|
||
## Rule details | ||
|
||
This rule triggers a warning if `toEqual()` or `toStrictEqual()` are used to | ||
assert a primitive literal value such as a string or a number. | ||
|
||
The following patterns are considered warnings: | ||
|
||
```js | ||
expect(value).not.toEqual(5); | ||
expect(getMessage()).toStrictEqual('hello world'); | ||
expect(loadMessage()).resolves.toEqual('hello world'); | ||
``` | ||
|
||
The following pattern is not warning: | ||
|
||
```js | ||
expect(value).not.toBe(5); | ||
expect(getMessage()).toBe('hello world'); | ||
expect(loadMessage()).resolves.toBe('hello world'); | ||
|
||
expect(catchError()).toStrictEqual({ message: 'oh noes!' }); | ||
``` | ||
|
||
For `null`, `undefined`, and `NaN`, this rule triggers a warning if `toBe` is | ||
used to assert against those literal values instead of their more specific | ||
`toBe` counterparts: | ||
|
||
```js | ||
expect(value).not.toBe(undefined); | ||
expect(getMessage()).toBe(null); | ||
expect(countMessages()).resolves.not.toBe(NaN); | ||
``` | ||
|
||
The following pattern is not warning: | ||
|
||
```js | ||
expect(value).toBeDefined(); | ||
expect(getMessage()).toBeNull(); | ||
expect(countMessages()).resolves.not.toBeNaN(); | ||
|
||
expect(catchError()).toStrictEqual({ message: undefined }); | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,260 @@ | ||
import { TSESLint } from '@typescript-eslint/experimental-utils'; | ||
import rule from '../prefer-to-be'; | ||
|
||
const ruleTester = new TSESLint.RuleTester(); | ||
|
||
ruleTester.run('prefer-to-be', rule, { | ||
valid: [ | ||
'expect(null).toBeNull();', | ||
'expect(null).not.toBeNull();', | ||
'expect(null).toBe(1);', | ||
'expect(obj).toStrictEqual([ x, 1 ]);', | ||
'expect(obj).toStrictEqual({ x: 1 });', | ||
'expect(obj).not.toStrictEqual({ x: 1 });', | ||
'expect(value).toMatchSnapshot();', | ||
"expect(catchError()).toStrictEqual({ message: 'oh noes!' })", | ||
'expect("something");', | ||
], | ||
invalid: [ | ||
{ | ||
code: 'expect(value).toEqual("my string");', | ||
output: 'expect(value).toBe("my string");', | ||
errors: [{ messageId: 'useToBe', column: 15, line: 1 }], | ||
}, | ||
{ | ||
code: 'expect(value).toStrictEqual("my string");', | ||
output: 'expect(value).toBe("my string");', | ||
errors: [{ messageId: 'useToBe', column: 15, line: 1 }], | ||
}, | ||
{ | ||
code: 'expect(loadMessage()).resolves.toStrictEqual("hello world");', | ||
output: 'expect(loadMessage()).resolves.toBe("hello world");', | ||
errors: [{ messageId: 'useToBe', column: 32, line: 1 }], | ||
}, | ||
], | ||
}); | ||
|
||
ruleTester.run('prefer-to-be: null', rule, { | ||
valid: [ | ||
'expect(null).toBeNull();', | ||
'expect(null).not.toBeNull();', | ||
'expect(null).toBe(1);', | ||
'expect(obj).toStrictEqual([ x, 1 ]);', | ||
'expect(obj).toStrictEqual({ x: 1 });', | ||
'expect(obj).not.toStrictEqual({ x: 1 });', | ||
'expect(value).toMatchSnapshot();', | ||
"expect(catchError()).toStrictEqual({ message: 'oh noes!' })", | ||
'expect("something");', | ||
// | ||
'expect(null).not.toEqual();', | ||
'expect(null).toBe();', | ||
'expect(null).toMatchSnapshot();', | ||
'expect("a string").toMatchSnapshot(null);', | ||
'expect("a string").not.toMatchSnapshot();', | ||
'expect(null).toBe', | ||
], | ||
invalid: [ | ||
{ | ||
code: 'expect(null).toBe(null);', | ||
output: 'expect(null).toBeNull();', | ||
errors: [{ messageId: 'useToBeNull', column: 14, line: 1 }], | ||
}, | ||
{ | ||
code: 'expect(null).toEqual(null);', | ||
output: 'expect(null).toBeNull();', | ||
errors: [{ messageId: 'useToBeNull', column: 14, line: 1 }], | ||
}, | ||
{ | ||
code: 'expect(null).toStrictEqual(null);', | ||
output: 'expect(null).toBeNull();', | ||
errors: [{ messageId: 'useToBeNull', column: 14, line: 1 }], | ||
}, | ||
{ | ||
code: 'expect("a string").not.toBe(null);', | ||
output: 'expect("a string").not.toBeNull();', | ||
errors: [{ messageId: 'useToBeNull', column: 24, line: 1 }], | ||
}, | ||
{ | ||
code: 'expect("a string").not.toEqual(null);', | ||
output: 'expect("a string").not.toBeNull();', | ||
errors: [{ messageId: 'useToBeNull', column: 24, line: 1 }], | ||
}, | ||
{ | ||
code: 'expect("a string").not.toStrictEqual(null);', | ||
output: 'expect("a string").not.toBeNull();', | ||
errors: [{ messageId: 'useToBeNull', column: 24, line: 1 }], | ||
}, | ||
], | ||
}); | ||
|
||
ruleTester.run('prefer-to-be: undefined', rule, { | ||
valid: [ | ||
'expect(undefined).toBeUndefined();', | ||
'expect(true).toBeDefined();', | ||
'expect({}).toEqual({});', | ||
'expect(something).toBe()', | ||
'expect(something).toBe(somethingElse)', | ||
'expect(something).toEqual(somethingElse)', | ||
'expect(something).not.toBe(somethingElse)', | ||
'expect(something).not.toEqual(somethingElse)', | ||
'expect(undefined).toBe', | ||
'expect("something");', | ||
], | ||
|
||
invalid: [ | ||
{ | ||
code: 'expect(undefined).toBe(undefined);', | ||
output: 'expect(undefined).toBeUndefined();', | ||
errors: [{ messageId: 'useToBeUndefined', column: 19, line: 1 }], | ||
}, | ||
{ | ||
code: 'expect(undefined).toEqual(undefined);', | ||
output: 'expect(undefined).toBeUndefined();', | ||
errors: [{ messageId: 'useToBeUndefined', column: 19, line: 1 }], | ||
}, | ||
{ | ||
code: 'expect(undefined).toStrictEqual(undefined);', | ||
output: 'expect(undefined).toBeUndefined();', | ||
errors: [{ messageId: 'useToBeUndefined', column: 19, line: 1 }], | ||
}, | ||
{ | ||
code: 'expect("a string").not.toBe(undefined);', | ||
output: 'expect("a string").toBeDefined();', | ||
errors: [{ messageId: 'useToBeDefined', column: 24, line: 1 }], | ||
}, | ||
{ | ||
code: 'expect("a string").not.toEqual(undefined);', | ||
output: 'expect("a string").toBeDefined();', | ||
errors: [{ messageId: 'useToBeDefined', column: 24, line: 1 }], | ||
}, | ||
{ | ||
code: 'expect("a string").not.toStrictEqual(undefined);', | ||
output: 'expect("a string").toBeDefined();', | ||
errors: [{ messageId: 'useToBeDefined', column: 24, line: 1 }], | ||
}, | ||
], | ||
}); | ||
|
||
ruleTester.run('prefer-to-be: NaN', rule, { | ||
valid: [ | ||
'expect(NaN).toBeNaN();', | ||
'expect(true).not.toBeNaN();', | ||
'expect({}).toEqual({});', | ||
'expect(something).toBe()', | ||
'expect(something).toBe(somethingElse)', | ||
'expect(something).toEqual(somethingElse)', | ||
'expect(something).not.toBe(somethingElse)', | ||
'expect(something).not.toEqual(somethingElse)', | ||
'expect(undefined).toBe', | ||
'expect("something");', | ||
], | ||
invalid: [ | ||
{ | ||
code: 'expect(NaN).toBe(NaN);', | ||
output: 'expect(NaN).toBeNaN();', | ||
errors: [{ messageId: 'useToBeNaN', column: 13, line: 1 }], | ||
}, | ||
{ | ||
code: 'expect(NaN).toEqual(NaN);', | ||
output: 'expect(NaN).toBeNaN();', | ||
errors: [{ messageId: 'useToBeNaN', column: 13, line: 1 }], | ||
}, | ||
{ | ||
code: 'expect(NaN).toStrictEqual(NaN);', | ||
output: 'expect(NaN).toBeNaN();', | ||
errors: [{ messageId: 'useToBeNaN', column: 13, line: 1 }], | ||
}, | ||
{ | ||
code: 'expect("a string").not.toBe(NaN);', | ||
output: 'expect("a string").not.toBeNaN();', | ||
errors: [{ messageId: 'useToBeNaN', column: 24, line: 1 }], | ||
}, | ||
{ | ||
code: 'expect("a string").not.toEqual(NaN);', | ||
output: 'expect("a string").not.toBeNaN();', | ||
errors: [{ messageId: 'useToBeNaN', column: 24, line: 1 }], | ||
}, | ||
{ | ||
code: 'expect("a string").not.toStrictEqual(NaN);', | ||
output: 'expect("a string").not.toBeNaN();', | ||
errors: [{ messageId: 'useToBeNaN', column: 24, line: 1 }], | ||
}, | ||
], | ||
}); | ||
|
||
ruleTester.run('prefer-to-be: undefined vs defined', rule, { | ||
valid: [ | ||
'expect(NaN).toBeNaN();', | ||
'expect(true).not.toBeNaN();', | ||
'expect({}).toEqual({});', | ||
'expect(something).toBe()', | ||
'expect(something).toBe(somethingElse)', | ||
'expect(something).toEqual(somethingElse)', | ||
'expect(something).not.toBe(somethingElse)', | ||
'expect(something).not.toEqual(somethingElse)', | ||
'expect(undefined).toBe', | ||
'expect("something");', | ||
], | ||
invalid: [ | ||
{ | ||
code: 'expect(undefined).not.toBeDefined();', | ||
output: 'expect(undefined).toBeUndefined();', | ||
errors: [{ messageId: 'useToBeUndefined', column: 23, line: 1 }], | ||
}, | ||
{ | ||
code: 'expect(undefined).resolves.not.toBeDefined();', | ||
output: 'expect(undefined).resolves.toBeUndefined();', | ||
errors: [{ messageId: 'useToBeUndefined', column: 32, line: 1 }], | ||
}, | ||
{ | ||
code: 'expect("a string").not.toBeUndefined();', | ||
output: 'expect("a string").toBeDefined();', | ||
errors: [{ messageId: 'useToBeDefined', column: 24, line: 1 }], | ||
}, | ||
{ | ||
code: 'expect("a string").rejects.not.toBeUndefined();', | ||
output: 'expect("a string").rejects.toBeDefined();', | ||
errors: [{ messageId: 'useToBeDefined', column: 32, line: 1 }], | ||
}, | ||
], | ||
}); | ||
|
||
new TSESLint.RuleTester({ | ||
parser: require.resolve('@typescript-eslint/parser'), | ||
}).run('prefer-to-be: typescript edition', rule, { | ||
valid: [ | ||
"(expect('Model must be bound to an array if the multiple property is true') as any).toHaveBeenTipped()", | ||
], | ||
invalid: [ | ||
{ | ||
code: 'expect(null).toEqual(1 as unknown as string as unknown as any);', | ||
output: 'expect(null).toBe(1 as unknown as string as unknown as any);', | ||
errors: [{ messageId: 'useToBe', column: 14, line: 1 }], | ||
}, | ||
{ | ||
code: 'expect("a string").not.toStrictEqual("string" as number);', | ||
output: 'expect("a string").not.toBe("string" as number);', | ||
errors: [{ messageId: 'useToBe', column: 24, line: 1 }], | ||
}, | ||
{ | ||
code: 'expect(null).toBe(null as unknown as string as unknown as any);', | ||
output: 'expect(null).toBeNull();', | ||
errors: [{ messageId: 'useToBeNull', column: 14, line: 1 }], | ||
}, | ||
{ | ||
code: 'expect("a string").not.toEqual(null as number);', | ||
output: 'expect("a string").not.toBeNull();', | ||
errors: [{ messageId: 'useToBeNull', column: 24, line: 1 }], | ||
}, | ||
{ | ||
code: 'expect(undefined).toBe(undefined as unknown as string as any);', | ||
output: 'expect(undefined).toBeUndefined();', | ||
errors: [{ messageId: 'useToBeUndefined', column: 19, line: 1 }], | ||
}, | ||
{ | ||
code: 'expect("a string").toEqual(undefined as number);', | ||
output: 'expect("a string").toBeUndefined();', | ||
errors: [{ messageId: 'useToBeUndefined', column: 20, line: 1 }], | ||
}, | ||
], | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.