Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit aa32bd0

Browse filesBrowse 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 <ruben@bridgewater.de> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
1 parent 49d6628 commit aa32bd0
Copy full SHA for aa32bd0

File tree

Expand file treeCollapse file tree

3 files changed

+49
-0
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

3 files changed

+49
-0
lines changed
Open diff view settings
Collapse file

‎test/.eslintrc.yaml‎

Copy file name to clipboardExpand all lines: test/.eslintrc.yaml
+1Lines 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
Collapse file
+27Lines 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+
const RuleTester = require('../../tools/eslint').RuleTester;
6+
const rule = require('../../tools/eslint-rules/prefer-common-expectserror');
7+
8+
const message = 'Please use common.expectsError(fn, err) instead of ' +
9+
'assert.throws(fn, common.expectsError(err)).';
10+
11+
new RuleTester().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+
});
Collapse file
+21Lines 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+
const msg = 'Please use common.expectsError(fn, err) instead of ' +
8+
'assert.throws(fn, common.expectsError(err)).';
9+
10+
const astSelector =
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.