Skip to content

Commit

Permalink
fix(no-disabled-tests): fix false positives for pending() usage (#155)
Browse files Browse the repository at this point in the history
Fixes #149
  • Loading branch information
macklinu authored and SimenB committed Sep 21, 2018
1 parent 1361d59 commit fdce162
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 2 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Expand Up @@ -14,6 +14,7 @@ module.exports = {
},
env: {
node: true,
es6: true,
},
rules: {
eqeqeq: ['error', 'smart'],
Expand Down
35 changes: 34 additions & 1 deletion rules/__tests__/no-disabled-tests.test.js
Expand Up @@ -3,7 +3,11 @@
const RuleTester = require('eslint').RuleTester;
const rule = require('../no-disabled-tests');

const ruleTester = new RuleTester();
const ruleTester = new RuleTester({
parserOptions: {
sourceType: 'module',
},
});

ruleTester.run('no-disabled-tests', rule, {
valid: [
Expand All @@ -17,6 +21,35 @@ ruleTester.run('no-disabled-tests', rule, {
'var calledSkip = it.skip; calledSkip.call(it)',
'({ f: function () {} }).f()',
'(a || b).f()',
[
'import { pending } from "actions"',
'',
'test("foo", () => {',
' expect(pending()).toEqual({})',
'})',
].join('\n'),
[
'const { pending } = require("actions")',
'',
'test("foo", () => {',
' expect(pending()).toEqual({})',
'})',
].join('\n'),
[
'test("foo", () => {',
' const pending = getPending()',
' expect(pending()).toEqual({})',
'})',
].join('\n'),
[
'test("foo", () => {',
' expect(pending()).toEqual({})',
'})',
'',
'function pending() {',
' return {}',
'}',
].join('\n'),
],

invalid: [
Expand Down
42 changes: 41 additions & 1 deletion rules/no-disabled-tests.js
Expand Up @@ -19,6 +19,33 @@ function getName(node) {
return null;
}

function collectReferences(scope) {
const locals = new Set();
const unresolved = new Set();

let currentScope = scope;

while (currentScope !== null) {
for (const ref of currentScope.variables) {
const isReferenceDefined = ref.defs.some(def => {
return def.type !== 'ImplicitGlobalVariable';
});

if (isReferenceDefined) {
locals.add(ref.name);
}
}

for (const ref of currentScope.through) {
unresolved.add(ref.identifier.name);
}

currentScope = currentScope.upper;
}

return { locals, unresolved };
}

module.exports = {
meta: {
docs: {
Expand Down Expand Up @@ -58,7 +85,19 @@ module.exports = {
context.report({ message: 'Skipped test', node });
break;

case 'pending':
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',
Expand All @@ -76,6 +115,7 @@ module.exports = {
});
}
break;
}

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

0 comments on commit fdce162

Please sign in to comment.