Skip to content

Commit

Permalink
tools: add eslintrc rule for assert.rejects
Browse files Browse the repository at this point in the history
This makes sure `assert.rejects` is always called with a second
argument. Besides that it is also changes a eslint error message
to suggest objects instead of a regular expression.

PR-URL: #19885
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
  • Loading branch information
BridgeAR authored and jasnell committed Apr 16, 2018
1 parent b3c1bd3 commit c667c87
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 3 deletions.
6 changes: 5 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,13 @@ module.exports = {
selector: "CallExpression[callee.object.name='assert'][callee.property.name='doesNotThrow']",
message: "Please replace `assert.doesNotThrow()` and add a comment next to the code instead."
},
{
selector: `CallExpression[callee.object.name='assert'][callee.property.name='rejects'][arguments.length<2]`,
message: 'assert.rejects() must be invoked with at least two arguments.',
},
{
selector: `CallExpression[callee.object.name='assert'][callee.property.name='throws'][arguments.1.type='Literal']:not([arguments.1.regex])`,
message: 'use a regular expression for second argument of assert.throws()',
message: 'Use an object as second argument of assert.throws()',
},
{
selector: `CallExpression[callee.object.name='assert'][callee.property.name='throws'][arguments.length<2]`,
Expand Down
4 changes: 3 additions & 1 deletion lib/.eslintrc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ rules:
- error
- selector: "CallExpression[callee.object.name='assert'][callee.property.name='doesNotThrow']"
message: "Please replace `assert.doesNotThrow()` and add a comment next to the code instead."
- selector: "CallExpression[callee.object.name='assert'][callee.property.name='rejects'][arguments.length<2]"
message: "assert.rejects() must be invoked with at least two arguments."
- selector: "CallExpression[callee.object.name='assert'][callee.property.name='throws'][arguments.1.type='Literal']:not([arguments.1.regex])"
message: "use a regular expression for second argument of assert.throws()"
message: "Use an object as second argument of assert.throws()"
- selector: "CallExpression[callee.object.name='assert'][callee.property.name='throws'][arguments.length<2]"
message: "assert.throws() must be invoked with at least two arguments."
- selector: "CallExpression[callee.name='setTimeout'][arguments.length<2]"
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-assert-async.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ const promises = [];

promises.push(assert.rejects(() => {
throw THROWN_ERROR;
}).catch(common.mustCall((err) => {
}, {}).catch(common.mustCall((err) => {
assert.strictEqual(err, THROWN_ERROR);
})));
}
Expand Down

0 comments on commit c667c87

Please sign in to comment.