Skip to content
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

Some fixes for require-tothrow-message #161

Merged
merged 7 commits into from Sep 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 0 additions & 32 deletions rules/__tests__/require-tothrow-message.js

This file was deleted.

52 changes: 52 additions & 0 deletions 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,
},
],
},
],
});
13 changes: 10 additions & 3 deletions 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: {
Expand All @@ -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),
});
}
},
Expand Down