Skip to content

Commit f2d2dbe

Browse files
garykingSimenB
authored andcommittedSep 30, 2018
fix(require-tothrow-message): cover more cases (#161)
1 parent ee2647e commit f2d2dbe

File tree

3 files changed

+62
-35
lines changed

3 files changed

+62
-35
lines changed
 

Diff for: ‎rules/__tests__/require-tothrow-message.js

-32
This file was deleted.

Diff for: ‎rules/__tests__/require-tothrow-message.test.js

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
'use strict';
2+
3+
const RuleTester = require('eslint').RuleTester;
4+
const rule = require('../require-tothrow-message');
5+
6+
const ruleTester = new RuleTester({
7+
parserOptions: {
8+
ecmaVersion: 6,
9+
},
10+
});
11+
12+
ruleTester.run('require-tothrow-message', rule, {
13+
valid: [
14+
// String
15+
"expect(() => { throw new Error('a'); }).toThrow('a');",
16+
"expect(() => { throw new Error('a'); }).toThrowError('a');",
17+
18+
// Template literal
19+
"const a = 'a'; expect(() => { throw new Error('a'); }).toThrow(`${a}`);",
20+
21+
// Regex
22+
"expect(() => { throw new Error('a'); }).toThrow(/^a$/);",
23+
24+
// Function
25+
"expect(() => { throw new Error('a'); })" +
26+
".toThrow((() => { return 'a'; })());",
27+
28+
// Allow no message for `not`.
29+
"expect(() => { throw new Error('a'); }).not.toThrow();",
30+
],
31+
32+
invalid: [
33+
// Empty toThrow
34+
{
35+
code: "expect(() => { throw new Error('a'); }).toThrow();",
36+
errors: [
37+
{ message: 'Add an error message to toThrow()', column: 41, line: 1 },
38+
],
39+
},
40+
// Empty toThrowError
41+
{
42+
code: "expect(() => { throw new Error('a'); }).toThrowError();",
43+
errors: [
44+
{
45+
message: 'Add an error message to toThrowError()',
46+
column: 41,
47+
line: 1,
48+
},
49+
],
50+
},
51+
],
52+
});

Diff for: ‎rules/require-tothrow-message.js

+10-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
'use strict';
22

3+
const argument = require('./util').argument;
4+
const expectCase = require('./util').expectCase;
35
const getDocsUrl = require('./util').getDocsUrl;
6+
const method = require('./util').method;
47

58
module.exports = {
69
meta: {
@@ -11,19 +14,23 @@ module.exports = {
1114
create(context) {
1215
return {
1316
CallExpression(node) {
14-
const propertyName = node.callee.property && node.callee.property.name;
17+
if (!expectCase(node)) {
18+
return;
19+
}
20+
21+
const propertyName = method(node) && method(node).name;
1522

1623
// Look for `toThrow` calls with no arguments.
1724
if (
1825
['toThrow', 'toThrowError'].indexOf(propertyName) > -1 &&
19-
!(node.arguments[0] && node.arguments[0].type === 'Literal')
26+
!argument(node)
2027
) {
2128
context.report({
2229
message: `Add an error message to {{ propertyName }}()`,
2330
data: {
2431
propertyName,
2532
},
26-
node: node.callee.property,
33+
node: method(node),
2734
});
2835
}
2936
},

0 commit comments

Comments
 (0)
Please sign in to comment.