From aa32bd08a8985cadc29ad98fbdd3d3813152d85f Mon Sep 17 00:00:00 2001 From: Anatoli Papirovski Date: Fri, 8 Dec 2017 16:06:02 -0500 Subject: [PATCH] tools: prefer common.expectsError in tests Add lint rule to validate that common.expectsError(fn, err) is being used instead of assert.throws(fn, common.expectsError(err)); PR-URL: https://github.com/nodejs/node/pull/17557 Reviewed-By: Ruben Bridgewater Reviewed-By: Colin Ihrig --- test/.eslintrc.yaml | 1 + .../test-eslint-prefer-common-expectserror.js | 27 +++++++++++++++++++ .../prefer-common-expectserror.js | 21 +++++++++++++++ 3 files changed, 49 insertions(+) create mode 100644 test/parallel/test-eslint-prefer-common-expectserror.js create mode 100644 tools/eslint-rules/prefer-common-expectserror.js diff --git a/test/.eslintrc.yaml b/test/.eslintrc.yaml index aa320996aa4b16..7a5d002e1b51e8 100644 --- a/test/.eslintrc.yaml +++ b/test/.eslintrc.yaml @@ -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 diff --git a/test/parallel/test-eslint-prefer-common-expectserror.js b/test/parallel/test-eslint-prefer-common-expectserror.js new file mode 100644 index 00000000000000..16ce66bc24e644 --- /dev/null +++ b/test/parallel/test-eslint-prefer-common-expectserror.js @@ -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 }] + } + ] +}); diff --git a/tools/eslint-rules/prefer-common-expectserror.js b/tools/eslint-rules/prefer-common-expectserror.js new file mode 100644 index 00000000000000..f33241697a68ef --- /dev/null +++ b/tools/eslint-rules/prefer-common-expectserror.js @@ -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) + }; +};