Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

fix(no-disabled-tests): switch to using jest function call parser #1125

Merged
merged 1 commit into from May 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 });
},
G-Rath marked this conversation as resolved.
Show resolved Hide resolved
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--;
},
};
},
});