From b222f860c5e1c70b3de7a77f29bdc9f3eaec79ad Mon Sep 17 00:00:00 2001 From: Mackie Underdown Date: Sat, 13 Oct 2018 16:09:09 -0700 Subject: [PATCH] fix(expect-expect): support MemberExpressions in assertFunctionNames Fixes #175 --- rules/__tests__/expect-expect.test.js | 8 ++++++++ rules/expect-expect.js | 23 +++++++++++++---------- rules/no-disabled-tests.js | 20 ++------------------ rules/util.js | 16 +++++++++++++--- 4 files changed, 36 insertions(+), 31 deletions(-) diff --git a/rules/__tests__/expect-expect.test.js b/rules/__tests__/expect-expect.test.js index cd1c78836..465e29100 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("...", () => {', + ' td.verify(someFunctionCall())', + '})', + ].join('\n'), + options: [{ assertFunctionNames: ['td.verify'] }], + }, ], invalid: [ diff --git a/rules/expect-expect.js b/rules/expect-expect.js index aaceb9a77..b83e4a028 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: { @@ -33,16 +34,18 @@ module.exports = { : ['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.includes(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 =>