Skip to content

Commit

Permalink
Enhance missing expect assertions call rule to report error when expe…
Browse files Browse the repository at this point in the history
…ct.assertions() call is present but the argument provided is not a number.
  • Loading branch information
tushardhole committed Jan 1, 2018
1 parent 1989ffc commit e3203d0
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
8 changes: 8 additions & 0 deletions rules/__tests__/missing_expect_assertions_call_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ ruleTester.run(
},
],
},
{
code: 'it("it1", function() {expect.assertions();})',
errors: [
{
message: expectedMsg,
},
],
},
],

valid: [
Expand Down
11 changes: 10 additions & 1 deletion rules/missing_expect_assertions_call.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,22 @@
const ruleMsg =
'Every test should have expect.assertions({number of assertions}) as first expression';

const isValidArgument = expression => {
try {
return Number.isInteger(expression.arguments[0].value);
} catch (e) {
return false;
}
};

const isExpectAssertionsCall = expression => {
try {
return (
expression.type == 'CallExpression' &&
expression.callee.type == 'MemberExpression' &&
expression.callee.object.name == 'expect' &&
expression.callee.property.name == 'assertions'
expression.callee.property.name == 'assertions' &&
isValidArgument(expression)
);
} catch (e) {
return false;
Expand Down

0 comments on commit e3203d0

Please # to comment.