Skip to content

Commit aa32bd0

Browse files
apapirovskiMylesBorins
authored andcommitted
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: #17557 Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Colin Ihrig <[email protected]>
1 parent 49d6628 commit aa32bd0

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

‎test/.eslintrc.yaml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ rules:
1010
# Custom rules in tools/eslint-rules
1111
prefer-assert-iferror: error
1212
prefer-assert-methods: error
13+
prefer-common-expectserror: error
1314
prefer-common-mustnotcall: error
1415
crypto-check: error
1516
inspector-check: error
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
'use strict';
2+
3+
require('../common');
4+
5+
constRuleTester=require('../../tools/eslint').RuleTester;
6+
construle=require('../../tools/eslint-rules/prefer-common-expectserror');
7+
8+
constmessage='Please use common.expectsError(fn, err) instead of '+
9+
'assert.throws(fn, common.expectsError(err)).';
10+
11+
newRuleTester().run('prefer-common-expectserror',rule,{
12+
valid: [
13+
'assert.throws(fn, /[a-z]/)',
14+
'assert.throws(function (){}, function(){})',
15+
'common.expectsError(function(){}, err)'
16+
],
17+
invalid: [
18+
{
19+
code: 'assert.throws(function(){}, common.expectsError(err))',
20+
errors: [{ message }]
21+
},
22+
{
23+
code: 'assert.throws(fn, common.expectsError(err))',
24+
errors: [{ message }]
25+
}
26+
]
27+
});
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
'use strict';
2+
3+
//------------------------------------------------------------------------------
4+
// Rule Definition
5+
//------------------------------------------------------------------------------
6+
7+
constmsg='Please use common.expectsError(fn, err) instead of '+
8+
'assert.throws(fn, common.expectsError(err)).';
9+
10+
constastSelector=
11+
'CallExpression[arguments.length=2]'+
12+
'[callee.object.name="assert"]'+
13+
'[callee.property.name="throws"]'+
14+
'[arguments.1.callee.object.name="common"]'+
15+
'[arguments.1.callee.property.name="expectsError"]';
16+
17+
module.exports=function(context){
18+
return{
19+
[astSelector]: (node)=>context.report(node,msg)
20+
};
21+
};

0 commit comments

Comments
(0)