Skip to content

Commit

Permalink
fix(no-disabled-tests): switch to using jest function call parser (#1125
Browse files Browse the repository at this point in the history
)
  • Loading branch information
G-Rath committed May 28, 2022
1 parent 6d75e8d commit 32931c3
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 59 deletions.
38 changes: 19 additions & 19 deletions src/rules/__tests__/no-disabled-tests.test.ts
Expand Up @@ -65,63 +65,63 @@ ruleTester.run('no-disabled-tests', rule, {
invalid: [
{
code: 'describe.skip("foo", function () {})',
errors: [{ messageId: 'skippedTestSuite', column: 1, line: 1 }],
errors: [{ messageId: 'disabledSuite', column: 1, line: 1 }],
},
{
code: 'describe.skip.each([1, 2, 3])("%s", (a, b) => {});',
errors: [{ messageId: 'skippedTestSuite', column: 1, line: 1 }],
errors: [{ messageId: 'disabledSuite', column: 1, line: 1 }],
},
{
code: 'xdescribe.each([1, 2, 3])("%s", (a, b) => {});',
errors: [{ messageId: 'skippedTestSuite', column: 1, line: 1 }],
errors: [{ messageId: 'disabledSuite', column: 1, line: 1 }],
},
{
code: 'describe[`skip`]("foo", function () {})',
errors: [{ messageId: 'skippedTestSuite', column: 1, line: 1 }],
errors: [{ messageId: 'disabledSuite', column: 1, line: 1 }],
},
{
code: 'describe["skip"]("foo", function () {})',
errors: [{ messageId: 'skippedTestSuite', column: 1, line: 1 }],
errors: [{ messageId: 'disabledSuite', column: 1, line: 1 }],
},
{
code: 'it.skip("foo", function () {})',
errors: [{ messageId: 'skippedTest', column: 1, line: 1 }],
errors: [{ messageId: 'disabledTest', column: 1, line: 1 }],
},
{
code: 'it.concurrent.skip("foo", function () {})',
errors: [{ messageId: 'skippedTest', column: 1, line: 1 }],
errors: [{ messageId: 'disabledTest', column: 1, line: 1 }],
},
{
code: 'it["skip"]("foo", function () {})',
errors: [{ messageId: 'skippedTest', column: 1, line: 1 }],
errors: [{ messageId: 'disabledTest', column: 1, line: 1 }],
},
{
code: 'test.skip("foo", function () {})',
errors: [{ messageId: 'skippedTest', column: 1, line: 1 }],
errors: [{ messageId: 'disabledTest', column: 1, line: 1 }],
},
{
code: 'it.skip.each``("foo", function () {})',
errors: [{ messageId: 'skippedTest', column: 1, line: 1 }],
errors: [{ messageId: 'disabledTest', column: 1, line: 1 }],
},
{
code: 'test.skip.each``("foo", function () {})',
errors: [{ messageId: 'skippedTest', column: 1, line: 1 }],
errors: [{ messageId: 'disabledTest', column: 1, line: 1 }],
},
{
code: 'it.skip.each([])("foo", function () {})',
errors: [{ messageId: 'skippedTest', column: 1, line: 1 }],
errors: [{ messageId: 'disabledTest', column: 1, line: 1 }],
},
{
code: 'test.skip.each([])("foo", function () {})',
errors: [{ messageId: 'skippedTest', column: 1, line: 1 }],
errors: [{ messageId: 'disabledTest', column: 1, line: 1 }],
},
{
code: 'test.concurrent.skip("foo", function () {})',
errors: [{ messageId: 'skippedTest', column: 1, line: 1 }],
errors: [{ messageId: 'disabledTest', column: 1, line: 1 }],
},
{
code: 'test["skip"]("foo", function () {})',
errors: [{ messageId: 'skippedTest', column: 1, line: 1 }],
errors: [{ messageId: 'disabledTest', column: 1, line: 1 }],
},
{
code: 'xdescribe("foo", function () {})',
Expand All @@ -137,19 +137,19 @@ ruleTester.run('no-disabled-tests', rule, {
},
{
code: 'xit.each``("foo", function () {})',
errors: [{ messageId: 'skippedTest', column: 1, line: 1 }],
errors: [{ messageId: 'disabledTest', column: 1, line: 1 }],
},
{
code: 'xtest.each``("foo", function () {})',
errors: [{ messageId: 'skippedTest', column: 1, line: 1 }],
errors: [{ messageId: 'disabledTest', column: 1, line: 1 }],
},
{
code: 'xit.each([])("foo", function () {})',
errors: [{ messageId: 'skippedTest', column: 1, line: 1 }],
errors: [{ messageId: 'disabledTest', column: 1, line: 1 }],
},
{
code: 'xtest.each([])("foo", function () {})',
errors: [{ messageId: 'skippedTest', column: 1, line: 1 }],
errors: [{ messageId: 'disabledTest', column: 1, line: 1 }],
},
{
code: 'it("has title but no callback")',
Expand Down
80 changes: 40 additions & 40 deletions src/rules/no-disabled-tests.ts
@@ -1,4 +1,9 @@
import { createRule, getNodeName, scopeHasLocalReference } from './utils';
import {
createRule,
getAccessorValue,
parseJestFnCall,
scopeHasLocalReference,
} from './utils';

export default createRule({
name: __filename,
Expand All @@ -10,8 +15,6 @@ export default createRule({
},
messages: {
missingFunction: 'Test is missing function argument',
skippedTestSuite: 'Skipped test suite',
skippedTest: 'Skipped test',
pending: 'Call to pending()',
pendingSuite: 'Call to pending() within test suite',
pendingTest: 'Call to pending() within test',
Expand All @@ -27,40 +30,49 @@ export default createRule({
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({ messageId: 'missingFunction', node });
},
CallExpression(node) {
const functionName = getNodeName(node.callee);
const jestFnCall = parseJestFnCall(node, context.getScope());

// prevent duplicate warnings for it.each()()
if (node.callee.type === 'CallExpression') {
if (!jestFnCall) {
return;
}

switch (functionName) {
case 'describe.skip.each':
case 'xdescribe.each':
case 'describe.skip':
context.report({ messageId: 'skippedTestSuite', node });
break;
if (jestFnCall.type === 'describe') {
suiteDepth++;
}

if (jestFnCall.type === 'test') {
testDepth++;
}

case 'it.skip':
case 'it.concurrent.skip':
case 'test.skip':
case 'test.concurrent.skip':
case 'it.skip.each':
case 'test.skip.each':
case 'xit.each':
case 'xtest.each':
context.report({ messageId: 'skippedTest', node });
break;
if (
// the only jest functions that are with "x" are "xdescribe", "xtest", and "xit"
jestFnCall.name.startsWith('x') ||
jestFnCall.members.some(s => getAccessorValue(s) === 'skip')
) {
context.report({
messageId:
jestFnCall.type === 'describe' ? 'disabledSuite' : 'disabledTest',
node,
});
}
},
'CallExpression:exit'(node) {
const jestFnCall = parseJestFnCall(node, context.getScope());

if (!jestFnCall) {
return;
}

if (jestFnCall.type === 'describe') {
suiteDepth--;
}

if (jestFnCall.type === 'test') {
testDepth--;
}
},
'CallExpression[callee.name="pending"]'(node) {
Expand All @@ -76,18 +88,6 @@ export default createRule({
context.report({ messageId: 'pending', node });
}
},
'CallExpression[callee.name="xdescribe"]'(node) {
context.report({ messageId: 'disabledSuite', node });
},
'CallExpression[callee.name=/^(xit|xtest)$/]'(node) {
context.report({ messageId: 'disabledTest', node });
},
'CallExpression[callee.name="describe"]:exit'() {
suiteDepth--;
},
'CallExpression[callee.name=/^(it|test)$/]:exit'() {
testDepth--;
},
};
},
});

0 comments on commit 32931c3

Please sign in to comment.