-
Notifications
You must be signed in to change notification settings - Fork 228
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
Add new rule: require-tothrow-message #160
Merged
SimenB
merged 3 commits into
jest-community:master
from
garyking:require-tothrow-message
Sep 29, 2018
Merged
Changes from all commits
Commits
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 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 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,29 @@ | ||
# Require a message for `toThrow()` (require-tothrow-message) | ||
|
||
`toThrow()`, and its alias `toThrowError()`, are used to check if an error is | ||
thrown by a function call, such as in `expect(() => a()).toThrow()`. However, if | ||
no message is defined, then the test will pass for any thrown error. Requiring a | ||
message ensures that the intended error is thrown. | ||
|
||
## Rule details | ||
|
||
This rule triggers a warning if `toThrow()` or `toThrowError()` is used without | ||
an error message. | ||
|
||
### Default configuration | ||
|
||
The following patterns are considered warnings: | ||
|
||
```js | ||
expect(() => a()).toThrow(); | ||
|
||
expect(() => a()).toThrowError(); | ||
``` | ||
|
||
The following patterns are not considered warnings: | ||
|
||
```js | ||
expect(() => a()).toThrow('a'); | ||
|
||
expect(() => a()).toThrowError('a'); | ||
``` |
This file contains 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 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,32 @@ | ||
'use strict'; | ||
|
||
const RuleTester = require('eslint').RuleTester; | ||
const rule = require('../require-tothrow-message'); | ||
|
||
const ruleTester = new RuleTester(); | ||
|
||
ruleTester.run('require-tothrow-message', rule, { | ||
valid: [ | ||
"expect(function() { a() }).toThrow('a');", | ||
"expect(function() { a() }).toThrowError('a');", | ||
], | ||
|
||
invalid: [ | ||
{ | ||
code: 'expect(function() { a() }).toThrow();', | ||
errors: [ | ||
{ message: 'Add an error message to toThrow()', column: 28, line: 1 }, | ||
], | ||
}, | ||
{ | ||
code: 'expect(function() { a() }).toThrowError();', | ||
errors: [ | ||
{ | ||
message: 'Add an error message to toThrowError()', | ||
column: 28, | ||
line: 1, | ||
}, | ||
], | ||
}, | ||
], | ||
}); |
This file contains 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,32 @@ | ||
'use strict'; | ||
|
||
const getDocsUrl = require('./util').getDocsUrl; | ||
|
||
module.exports = { | ||
meta: { | ||
docs: { | ||
url: getDocsUrl(__filename), | ||
}, | ||
}, | ||
create(context) { | ||
return { | ||
CallExpression(node) { | ||
const propertyName = node.callee.property && node.callee.property.name; | ||
|
||
// Look for `toThrow` calls with no arguments. | ||
if ( | ||
['toThrow', 'toThrowError'].indexOf(propertyName) > -1 && | ||
!(node.arguments[0] && node.arguments[0].type === 'Literal') | ||
) { | ||
context.report({ | ||
message: `Add an error message to {{ propertyName }}()`, | ||
data: { | ||
propertyName, | ||
}, | ||
node: node.callee.property, | ||
}); | ||
} | ||
}, | ||
}; | ||
}, | ||
}; |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can use
includes
whenever we merge thenext
branch 🙂