From f2d2dbe545fe2e55e8af8cdef1e05dbcd291c896 Mon Sep 17 00:00:00 2001 From: garyking Date: Sun, 30 Sep 2018 16:05:16 -0400 Subject: [PATCH] fix(require-tothrow-message): cover more cases (#161) --- rules/__tests__/require-tothrow-message.js | 32 ------------ .../__tests__/require-tothrow-message.test.js | 52 +++++++++++++++++++ rules/require-tothrow-message.js | 13 +++-- 3 files changed, 62 insertions(+), 35 deletions(-) delete mode 100644 rules/__tests__/require-tothrow-message.js create mode 100644 rules/__tests__/require-tothrow-message.test.js diff --git a/rules/__tests__/require-tothrow-message.js b/rules/__tests__/require-tothrow-message.js deleted file mode 100644 index 536e9ec6c..000000000 --- a/rules/__tests__/require-tothrow-message.js +++ /dev/null @@ -1,32 +0,0 @@ -'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, - }, - ], - }, - ], -}); diff --git a/rules/__tests__/require-tothrow-message.test.js b/rules/__tests__/require-tothrow-message.test.js new file mode 100644 index 000000000..a68f283d8 --- /dev/null +++ b/rules/__tests__/require-tothrow-message.test.js @@ -0,0 +1,52 @@ +'use strict'; + +const RuleTester = require('eslint').RuleTester; +const rule = require('../require-tothrow-message'); + +const ruleTester = new RuleTester({ + parserOptions: { + ecmaVersion: 6, + }, +}); + +ruleTester.run('require-tothrow-message', rule, { + valid: [ + // String + "expect(() => { throw new Error('a'); }).toThrow('a');", + "expect(() => { throw new Error('a'); }).toThrowError('a');", + + // Template literal + "const a = 'a'; expect(() => { throw new Error('a'); }).toThrow(`${a}`);", + + // Regex + "expect(() => { throw new Error('a'); }).toThrow(/^a$/);", + + // Function + "expect(() => { throw new Error('a'); })" + + ".toThrow((() => { return 'a'; })());", + + // Allow no message for `not`. + "expect(() => { throw new Error('a'); }).not.toThrow();", + ], + + invalid: [ + // Empty toThrow + { + code: "expect(() => { throw new Error('a'); }).toThrow();", + errors: [ + { message: 'Add an error message to toThrow()', column: 41, line: 1 }, + ], + }, + // Empty toThrowError + { + code: "expect(() => { throw new Error('a'); }).toThrowError();", + errors: [ + { + message: 'Add an error message to toThrowError()', + column: 41, + line: 1, + }, + ], + }, + ], +}); diff --git a/rules/require-tothrow-message.js b/rules/require-tothrow-message.js index f637c39b3..3be06606e 100644 --- a/rules/require-tothrow-message.js +++ b/rules/require-tothrow-message.js @@ -1,6 +1,9 @@ 'use strict'; +const argument = require('./util').argument; +const expectCase = require('./util').expectCase; const getDocsUrl = require('./util').getDocsUrl; +const method = require('./util').method; module.exports = { meta: { @@ -11,19 +14,23 @@ module.exports = { create(context) { return { CallExpression(node) { - const propertyName = node.callee.property && node.callee.property.name; + if (!expectCase(node)) { + return; + } + + const propertyName = method(node) && method(node).name; // Look for `toThrow` calls with no arguments. if ( ['toThrow', 'toThrowError'].indexOf(propertyName) > -1 && - !(node.arguments[0] && node.arguments[0].type === 'Literal') + !argument(node) ) { context.report({ message: `Add an error message to {{ propertyName }}()`, data: { propertyName, }, - node: node.callee.property, + node: method(node), }); } },