Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
tools: prefer common.expectsError in tests
Add lint rule to validate that common.expectsError is being used
instead of assert.throws(fn, common.expectsError(err));
  • Loading branch information
apapirovski committed Dec 9, 2017
commit a8e3c3754bf117570dd9747e4f34e926d55a6ba2
1 change: 1 addition & 0 deletions test/.eslintrc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ rules:
# Custom rules in tools/eslint-rules
prefer-assert-iferror: error
prefer-assert-methods: error
prefer-common-expectserror: error
prefer-common-mustnotcall: error
crypto-check: error
inspector-check: error
Expand Down
27 changes: 27 additions & 0 deletions test/parallel/test-eslint-prefer-common-expectserror.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
'use strict';

require('../common');

const RuleTester = require('../../tools/eslint').RuleTester;
const rule = require('../../tools/eslint-rules/prefer-common-expectserror');

const message = 'Please use common.expectsError(fn, err) instead of ' +
'assert.throws(fn, common.expectsError(err)).';

new RuleTester().run('prefer-common-expectserror', rule, {
valid: [
'assert.throws(fn, /[a-z]/)',
'assert.throws(function () {}, function() {})',
'common.expectsError(function() {}, err)'
],
invalid: [
{
code: 'assert.throws(function() {}, common.expectsError(err))',
errors: [{ message }]
},
{
code: 'assert.throws(fn, common.expectsError(err))',
errors: [{ message }]
}
]
});
21 changes: 21 additions & 0 deletions tools/eslint-rules/prefer-common-expectserror.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict';

//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------

const msg = 'Please use common.expectsError(fn, err) instead of ' +
'assert.throws(fn, common.expectsError(err)).';

const astSelector =
'CallExpression[arguments.length=2]' +
'[callee.object.name="assert"]' +
'[callee.property.name="throws"]' +
'[arguments.1.callee.object.name="common"]' +
'[arguments.1.callee.property.name="expectsError"]';

module.exports = function(context) {
return {
[astSelector]: (node) => context.report(node, msg)
};
};