Skip to content

Commit 9466959

Browse files
macklinuSimenB
authored andcommittedOct 14, 2018
fix(expect-expect): support MemberExpressions in assertFunctionNames (#176)
Fixes #175
1 parent 0276985 commit 9466959

File tree

4 files changed

+39
-33
lines changed

4 files changed

+39
-33
lines changed
 

‎rules/__tests__/expect-expect.test.js

+8
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@ ruleTester.run('expect-expect', rule, {
2323
code: 'it("should return undefined",() => expectSaga(mySaga).returns());',
2424
options: [{ assertFunctionNames: ['expectSaga'] }],
2525
},
26+
{
27+
code: [
28+
'test("verifies the function call", () => {',
29+
' td.verify(someFunctionCall())',
30+
'})',
31+
].join('\n'),
32+
options: [{ assertFunctionNames: ['td.verify'] }],
33+
},
2634
],
2735

2836
invalid: [

‎rules/expect-expect.js

+16-12
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
*/
77

88
const getDocsUrl = require('./util').getDocsUrl;
9+
const getNodeName = require('./util').getNodeName;
910

1011
module.exports = {
1112
meta: {
@@ -27,22 +28,25 @@ module.exports = {
2728
},
2829
create(context) {
2930
const unchecked = [];
30-
const assertFunctionNames =
31+
const assertFunctionNames = new Set(
3132
context.options[0] && context.options[0].assertFunctionNames
3233
? context.options[0].assertFunctionNames
33-
: ['expect'];
34+
: ['expect']
35+
);
3436

3537
return {
36-
'CallExpression[callee.name=/^it|test$/]'(node) {
37-
unchecked.push(node);
38-
},
39-
[`CallExpression[callee.name=/^${assertFunctionNames.join('|')}$/]`]() {
40-
// Return early in case of nested `it` statements.
41-
for (const ancestor of context.getAncestors()) {
42-
const index = unchecked.indexOf(ancestor);
43-
if (index !== -1) {
44-
unchecked.splice(index, 1);
45-
break;
38+
CallExpression(node) {
39+
const name = getNodeName(node.callee);
40+
if (name === 'it' || name === 'test') {
41+
unchecked.push(node);
42+
} else if (assertFunctionNames.has(name)) {
43+
// Return early in case of nested `it` statements.
44+
for (const ancestor of context.getAncestors()) {
45+
const index = unchecked.indexOf(ancestor);
46+
if (index !== -1) {
47+
unchecked.splice(index, 1);
48+
break;
49+
}
4650
}
4751
}
4852
},

‎rules/no-disabled-tests.js

+2-18
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,7 @@
11
'use strict';
22

33
const getDocsUrl = require('./util').getDocsUrl;
4-
5-
function getName(node) {
6-
function joinNames(a, b) {
7-
return a && b ? `${a}.${b}` : null;
8-
}
9-
10-
switch (node && node.type) {
11-
case 'Identifier':
12-
return node.name;
13-
case 'Literal':
14-
return node.value;
15-
case 'MemberExpression':
16-
return joinNames(getName(node.object), getName(node.property));
17-
}
18-
19-
return null;
20-
}
4+
const getNodeName = require('./util').getNodeName;
215

226
function collectReferences(scope) {
237
const locals = new Set();
@@ -70,7 +54,7 @@ module.exports = {
7054
});
7155
},
7256
CallExpression(node) {
73-
const functionName = getName(node.callee);
57+
const functionName = getNodeName(node.callee);
7458

7559
switch (functionName) {
7660
case 'describe.skip':

‎rules/util.js

+13-3
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,20 @@ const testCaseNames = Object.assign(Object.create(null), {
9898
});
9999

100100
const getNodeName = node => {
101-
if (node.type === 'MemberExpression') {
102-
return `${node.object.name}.${node.property.name}`;
101+
function joinNames(a, b) {
102+
return a && b ? `${a}.${b}` : null;
103103
}
104-
return node.name;
104+
105+
switch (node && node.type) {
106+
case 'Identifier':
107+
return node.name;
108+
case 'Literal':
109+
return node.value;
110+
case 'MemberExpression':
111+
return joinNames(getNodeName(node.object), getNodeName(node.property));
112+
}
113+
114+
return null;
105115
};
106116

107117
const isTestCase = node =>

0 commit comments

Comments
 (0)
Please sign in to comment.