Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

refactor(no-disabled-tests): use ESQuery selector syntax #169

Merged
merged 1 commit into from
Oct 2, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
119 changes: 51 additions & 68 deletions rules/no-disabled-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,91 +57,74 @@ module.exports = {
let testDepth = 0;

return {
'CallExpression[callee.name="describe"]'() {
suiteDepth++;
},
'CallExpression[callee.name=/^it|test$/]'() {
testDepth++;
},
'CallExpression[callee.name=/^it|test$/][arguments.length<2]'(node) {
context.report({
message: 'Test is missing function argument',
node,
});
},
CallExpression(node) {
const functionName = getName(node.callee);

switch (functionName) {
case 'describe':
suiteDepth++;
break;

case 'describe.skip':
context.report({ message: 'Skipped test suite', node });
break;

case 'it':
case 'test':
testDepth++;
if (node.arguments.length < 2) {
context.report({
message: 'Test is missing function argument',
node,
});
}
break;

case 'it.skip':
case 'test.skip':
context.report({ message: 'Skipped test', node });
break;

case 'pending': {
const references = collectReferences(context.getScope());

if (
// `pending` was found as a local variable or function declaration.
references.locals.has('pending') ||
// `pending` was not found as an unresolved reference,
// meaning it is likely not an implicit global reference.
!references.unresolved.has('pending')
) {
break;
}

if (testDepth > 0) {
context.report({
message: 'Call to pending() within test',
node,
});
} else if (suiteDepth > 0) {
context.report({
message: 'Call to pending() within test suite',
node,
});
} else {
context.report({
message: 'Call to pending()',
node,
});
}
break;
}

case 'xdescribe':
context.report({ message: 'Disabled test suite', node });
break;

case 'xit':
case 'xtest':
context.report({ message: 'Disabled test', node });
break;
}
},
'CallExpression[callee.name="pending"]'(node) {
const references = collectReferences(context.getScope());

if (
// `pending` was found as a local variable or function declaration.
references.locals.has('pending') ||
// `pending` was not found as an unresolved reference,
// meaning it is likely not an implicit global reference.
!references.unresolved.has('pending')
) {
return;
}

'CallExpression:exit'(node) {
const functionName = getName(node.callee);

switch (functionName) {
case 'describe':
suiteDepth--;
break;

case 'it':
case 'test':
testDepth--;
break;
if (testDepth > 0) {
context.report({
message: 'Call to pending() within test',
node,
});
} else if (suiteDepth > 0) {
context.report({
message: 'Call to pending() within test suite',
node,
});
} else {
context.report({
message: 'Call to pending()',
node,
});
}
},
'CallExpression[callee.name="xdescribe"]'(node) {
context.report({ message: 'Disabled test suite', node });
},
'CallExpression[callee.name=/^xit|xtest$/]'(node) {
context.report({ message: 'Disabled test', node });
},
'CallExpression[callee.name="describe"]:exit'() {
suiteDepth--;
},
'CallExpression[callee.name=/^it|test$/]:exit'() {
testDepth--;
},
};
},
};