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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(no-disabled-tests): fix false positives for pending() usage #155

Merged
merged 1 commit into from Sep 21, 2018
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
1 change: 1 addition & 0 deletions .eslintrc.js
Expand Up @@ -14,6 +14,7 @@ module.exports = {
},
env: {
node: true,
es6: true,
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I used a Set() in my solution, and this was needed for ESLint to understand the Set global.

},
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',
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This allows for writing examples that use ES modules (i.e. import statements).

},
});

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() {',
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We want to ensure that a local function called pending() will not trigger a no-disabled-tests violation.

' 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) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function takes the current scope (so wherever the pending() function is called) and walks up the tree to collect all local references and unresolved references. This information is helpful for determining what local variables or functions have been defined in a file, as well as what variables that ESLint could not resolve (which likely means those are globals, like describe() & test()).

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;
Copy link
Collaborator Author

@macklinu macklinu Sep 21, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me know if this if statement is clear enough with the comment or if you see a clearer way to communicate this - I want this change to be as understandable and maintainable as possible. 鉂わ笍

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is great! Separate Sets make it quite straight forward to follow the flow 馃檪

}

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