diff --git a/rules/__tests__/missing_expect_assertions_call_test.js b/rules/__tests__/missing_expect_assertions_call_test.js index fe003738a..fa518b18e 100644 --- a/rules/__tests__/missing_expect_assertions_call_test.js +++ b/rules/__tests__/missing_expect_assertions_call_test.js @@ -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', @@ -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: [ @@ -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' + diff --git a/rules/missing_expect_assertions_call.js b/rules/missing_expect_assertions_call.js index 749657d09..fbab8e269 100644 --- a/rules/missing_expect_assertions_call.js +++ b/rules/missing_expect_assertions_call.js @@ -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; } @@ -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); } }