Skip to content

Commit

Permalink
Enhance the missing_expect_assertions rule to,
Browse files Browse the repository at this point in the history
	* Not report any error, if expect.hasAssertions() call is present
After this change, either expect.hasAssertions() or expect.assertions({number of assertions}) is required to pass the rule.
  • Loading branch information
tushardhole committed Jan 1, 2018
1 parent e3203d0 commit e8aefb1
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 10 deletions.
19 changes: 18 additions & 1 deletion rules/__tests__/missing_expect_assertions_call_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const rules = require('../..').rules;

const ruleTester = new RuleTester();
const expectedMsg =
'Every test should have expect.assertions({number of assertions}) as first expression';
'Every test should have expect.assertions({number of assertions}) OR expect.hasAssertions() as first expression';

ruleTester.run(
'missing-expect-assertions-call',
Expand Down Expand Up @@ -49,6 +49,22 @@ ruleTester.run(
},
],
},
{
code: 'it("it1", function() {expect.assertions(1,2);})',
errors: [
{
message: expectedMsg,
},
],
},
{
code: 'it("it1", function() {expect.assertions("1");})',
errors: [
{
message: expectedMsg,
},
],
},
],

valid: [
Expand All @@ -57,6 +73,7 @@ ruleTester.run(
parserOptions: { ecmaVersion: 6 },
},
'test("it1", function() {expect.assertions(0);})',
'test("it1", function() {expect.hasAssertions();})',
'it("it1", function() {expect.assertions(0);})',
'it("it1", function() {\n\t\t\texpect.assertions(1);' +
'\n\t\t\texpect(someValue).toBe(true)\n' +
Expand Down
26 changes: 17 additions & 9 deletions rules/missing_expect_assertions_call.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,32 @@
'use strict';

const ruleMsg =
'Every test should have expect.assertions({number of assertions}) as first expression';
'Every test should have expect.assertions({number of assertions}) OR expect.hasAssertions() as first expression';

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

const isExpectAssertionsCall = expression => {
const isExpectAssertionsOrHasAssertionsCall = expression => {
try {
return (
const expectAssertionOrHasAssertionCall =
expression.type == 'CallExpression' &&
expression.callee.type == 'MemberExpression' &&
expression.callee.object.name == 'expect' &&
expression.callee.property.name == 'assertions' &&
isValidArgument(expression)
);
(expression.callee.property.name == 'assertions' ||
expression.callee.property.name == 'hasAssertions');

if (expression.callee.property.name == 'assertions') {
return expectAssertionOrHasAssertionCall && validateArguments(expression);
}
return expectAssertionOrHasAssertionCall;
} catch (e) {
return false;
}
Expand Down Expand Up @@ -62,9 +69,10 @@ module.exports = context => {
if (isTestOrItFunction(node)) {
if (!isFirstLineExprStmt(node)) {
reportMsg(context, node);
return;
} else {
const testFuncFirstLine = getTestFunctionFirstLine(node);
if (testFuncFirstLine && !isExpectAssertionsCall(testFuncFirstLine)) {
if (!isExpectAssertionsOrHasAssertionsCall(testFuncFirstLine)) {
reportMsg(context, node);
}
}
Expand Down

0 comments on commit e8aefb1

Please # to comment.