Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(rules): require-tothrow-message (#160)
This rule checks if `toThrow` or `toThrowError` has a message. Fixes #154.
- Loading branch information
Showing
5 changed files
with
97 additions
and
0 deletions.
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, | ||
}); | ||
} | ||
}, | ||
}; | ||
}, | ||
}; |