Skip to content

Commit 7515458

Browse files
garykingSimenB
authored andcommittedSep 29, 2018
feat(rules): require-tothrow-message (#160)
This rule checks if `toThrow` or `toThrowError` has a message. Fixes #154.
1 parent a194da6 commit 7515458

File tree

5 files changed

+97
-0
lines changed

5 files changed

+97
-0
lines changed
 

‎README.md

+2
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ for more information about extending configuration files.
9898
| [prefer-to-be-undefined][] | Suggest using `toBeUndefined()` | | ![fixable-green][] |
9999
| [prefer-to-have-length][] | Suggest using `toHaveLength()` | ![recommended][] | ![fixable-green][] |
100100
| [prefer-inline-snapshots][] | Suggest using `toMatchInlineSnapshot()` | | ![fixable-green][] |
101+
| [require-tothrow-message][] | Require that `toThrow()` and `toThrowError` includes a message | | |
101102
| [valid-describe][] | Enforce valid `describe()` callback | | |
102103
| [valid-expect-in-promise][] | Enforce having return statement when testing with promises | | |
103104
| [valid-expect][] | Enforce valid `expect()` usage | ![recommended][] | |
@@ -125,6 +126,7 @@ for more information about extending configuration files.
125126
[prefer-to-be-undefined]: docs/rules/prefer-to-be-undefined.md
126127
[prefer-to-have-length]: docs/rules/prefer-to-have-length.md
127128
[prefer-inline-snapshots]: docs/rules/prefer-inline-snapshots.md
129+
[require-tothrow-message]: docs/rules/require-tothrow-message.md
128130
[valid-describe]: docs/rules/valid-describe.md
129131
[valid-expect-in-promise]: docs/rules/valid-expect-in-promise.md
130132
[valid-expect]: docs/rules/valid-expect.md

‎docs/rules/require-tothrow-message.md

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Require a message for `toThrow()` (require-tothrow-message)
2+
3+
`toThrow()`, and its alias `toThrowError()`, are used to check if an error is
4+
thrown by a function call, such as in `expect(() => a()).toThrow()`. However, if
5+
no message is defined, then the test will pass for any thrown error. Requiring a
6+
message ensures that the intended error is thrown.
7+
8+
## Rule details
9+
10+
This rule triggers a warning if `toThrow()` or `toThrowError()` is used without
11+
an error message.
12+
13+
### Default configuration
14+
15+
The following patterns are considered warnings:
16+
17+
```js
18+
expect(() => a()).toThrow();
19+
20+
expect(() => a()).toThrowError();
21+
```
22+
23+
The following patterns are not considered warnings:
24+
25+
```js
26+
expect(() => a()).toThrow('a');
27+
28+
expect(() => a()).toThrowError('a');
29+
```

‎index.js

+2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ const preferExpectAssertions = require('./rules/prefer-expect-assertions');
2121
const validExpectInPromise = require('./rules/valid-expect-in-promise');
2222
const preferInlineSnapshots = require('./rules/prefer-inline-snapshots');
2323
const preferStrictEqual = require('./rules/prefer-strict-equal');
24+
const requireTothrowMessage = require('./rules/require-tothrow-message');
2425

2526
const snapshotProcessor = require('./processors/snapshot-processor');
2627

@@ -89,5 +90,6 @@ module.exports = {
8990
'valid-expect-in-promise': validExpectInPromise,
9091
'prefer-inline-snapshots': preferInlineSnapshots,
9192
'prefer-strict-equal': preferStrictEqual,
93+
'require-tothrow-message': requireTothrowMessage,
9294
},
9395
};
+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
'use strict';
2+
3+
const RuleTester = require('eslint').RuleTester;
4+
const rule = require('../require-tothrow-message');
5+
6+
const ruleTester = new RuleTester();
7+
8+
ruleTester.run('require-tothrow-message', rule, {
9+
valid: [
10+
"expect(function() { a() }).toThrow('a');",
11+
"expect(function() { a() }).toThrowError('a');",
12+
],
13+
14+
invalid: [
15+
{
16+
code: 'expect(function() { a() }).toThrow();',
17+
errors: [
18+
{ message: 'Add an error message to toThrow()', column: 28, line: 1 },
19+
],
20+
},
21+
{
22+
code: 'expect(function() { a() }).toThrowError();',
23+
errors: [
24+
{
25+
message: 'Add an error message to toThrowError()',
26+
column: 28,
27+
line: 1,
28+
},
29+
],
30+
},
31+
],
32+
});

‎rules/require-tothrow-message.js

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
'use strict';
2+
3+
const getDocsUrl = require('./util').getDocsUrl;
4+
5+
module.exports = {
6+
meta: {
7+
docs: {
8+
url: getDocsUrl(__filename),
9+
},
10+
},
11+
create(context) {
12+
return {
13+
CallExpression(node) {
14+
const propertyName = node.callee.property && node.callee.property.name;
15+
16+
// Look for `toThrow` calls with no arguments.
17+
if (
18+
['toThrow', 'toThrowError'].indexOf(propertyName) > -1 &&
19+
!(node.arguments[0] && node.arguments[0].type === 'Literal')
20+
) {
21+
context.report({
22+
message: `Add an error message to {{ propertyName }}()`,
23+
data: {
24+
propertyName,
25+
},
26+
node: node.callee.property,
27+
});
28+
}
29+
},
30+
};
31+
},
32+
};

0 commit comments

Comments
 (0)
Please sign in to comment.