Skip to content

Commit

Permalink
fix(expect-expect): support MemberExpressions in assertFunctionNames (#…
Browse files Browse the repository at this point in the history
…176)

Fixes #175
  • Loading branch information
macklinu authored and SimenB committed Oct 14, 2018
1 parent 0276985 commit 9466959
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 33 deletions.
8 changes: 8 additions & 0 deletions rules/__tests__/expect-expect.test.js
Expand Up @@ -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: [
Expand Down
28 changes: 16 additions & 12 deletions rules/expect-expect.js
Expand Up @@ -6,6 +6,7 @@
*/

const getDocsUrl = require('./util').getDocsUrl;
const getNodeName = require('./util').getNodeName;

module.exports = {
meta: {
Expand All @@ -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;
}
}
}
},
Expand Down
20 changes: 2 additions & 18 deletions 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();
Expand Down Expand Up @@ -70,7 +54,7 @@ module.exports = {
});
},
CallExpression(node) {
const functionName = getName(node.callee);
const functionName = getNodeName(node.callee);

switch (functionName) {
case 'describe.skip':
Expand Down
16 changes: 13 additions & 3 deletions rules/util.js
Expand Up @@ -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 =>
Expand Down

0 comments on commit 9466959

Please sign in to comment.