diff --git a/rules/__tests__/expect-expect.test.js b/rules/__tests__/expect-expect.test.js index cd1c78836..78c5126e0 100644 --- a/rules/__tests__/expect-expect.test.js +++ b/rules/__tests__/expect-expect.test.js @@ -23,6 +23,14 @@ ruleTester.run('expect-expect', rule, { code: 'it("should return undefined",() => expectSaga(mySaga).returns());', options: [{ assertFunctionNames: ['expectSaga'] }], }, + { + code: [ + 'test("verifies the function call", () => {', + ' td.verify(someFunctionCall())', + '})', + ].join('\n'), + options: [{ assertFunctionNames: ['td.verify'] }], + }, ], invalid: [ diff --git a/rules/expect-expect.js b/rules/expect-expect.js index aaceb9a77..85976808b 100644 --- a/rules/expect-expect.js +++ b/rules/expect-expect.js @@ -6,6 +6,7 @@ */ const getDocsUrl = require('./util').getDocsUrl; +const getNodeName = require('./util').getNodeName; module.exports = { meta: { @@ -27,22 +28,25 @@ module.exports = { }, create(context) { const unchecked = []; - const assertFunctionNames = + const assertFunctionNames = new Set( context.options[0] && context.options[0].assertFunctionNames ? context.options[0].assertFunctionNames - : ['expect']; + : ['expect'] + ); return { - 'CallExpression[callee.name=/^it|test$/]'(node) { - unchecked.push(node); - }, - [`CallExpression[callee.name=/^${assertFunctionNames.join('|')}$/]`]() { - // Return early in case of nested `it` statements. - for (const ancestor of context.getAncestors()) { - const index = unchecked.indexOf(ancestor); - if (index !== -1) { - unchecked.splice(index, 1); - break; + CallExpression(node) { + const name = getNodeName(node.callee); + if (name === 'it' || name === 'test') { + unchecked.push(node); + } else if (assertFunctionNames.has(name)) { + // Return early in case of nested `it` statements. + for (const ancestor of context.getAncestors()) { + const index = unchecked.indexOf(ancestor); + if (index !== -1) { + unchecked.splice(index, 1); + break; + } } } }, diff --git a/rules/no-disabled-tests.js b/rules/no-disabled-tests.js index 83bc12dbf..307e643e6 100644 --- a/rules/no-disabled-tests.js +++ b/rules/no-disabled-tests.js @@ -1,23 +1,7 @@ 'use strict'; const getDocsUrl = require('./util').getDocsUrl; - -function getName(node) { - function joinNames(a, b) { - return a && b ? `${a}.${b}` : null; - } - - switch (node && node.type) { - case 'Identifier': - return node.name; - case 'Literal': - return node.value; - case 'MemberExpression': - return joinNames(getName(node.object), getName(node.property)); - } - - return null; -} +const getNodeName = require('./util').getNodeName; function collectReferences(scope) { const locals = new Set(); @@ -70,7 +54,7 @@ module.exports = { }); }, CallExpression(node) { - const functionName = getName(node.callee); + const functionName = getNodeName(node.callee); switch (functionName) { case 'describe.skip': diff --git a/rules/util.js b/rules/util.js index 2dd9def8b..35aa005d1 100644 --- a/rules/util.js +++ b/rules/util.js @@ -98,10 +98,20 @@ const testCaseNames = Object.assign(Object.create(null), { }); const getNodeName = node => { - if (node.type === 'MemberExpression') { - return `${node.object.name}.${node.property.name}`; + function joinNames(a, b) { + return a && b ? `${a}.${b}` : null; } - return node.name; + + switch (node && node.type) { + case 'Identifier': + return node.name; + case 'Literal': + return node.value; + case 'MemberExpression': + return joinNames(getNodeName(node.object), getNodeName(node.property)); + } + + return null; }; const isTestCase = node =>