-
Notifications
You must be signed in to change notification settings - Fork 29.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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]>
- Loading branch information
1 parent
167daa7
commit 268ce28
Showing
3 changed files
with
49 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }] | ||
} | ||
] | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}; | ||
}; |