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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ module.exports = { | |
}, | ||
env: { | ||
node: true, | ||
es6: true, | ||
}, | ||
rules: { | ||
eqeqeq: ['error', 'smart'], | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This allows for writing examples that use ES modules (i.e. |
||
}, | ||
}); | ||
|
||
ruleTester.run('no-disabled-tests', rule, { | ||
valid: [ | ||
|
@@ -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() {', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We want to ensure that a local function called |
||
' return {}', | ||
'}', | ||
].join('\n'), | ||
], | ||
|
||
invalid: [ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,33 @@ function getName(node) { | |
return null; | ||
} | ||
|
||
function collectReferences(scope) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This function takes the current scope (so wherever the |
||
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: { | ||
|
@@ -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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let me know if this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this is great! Separate |
||
} | ||
|
||
if (testDepth > 0) { | ||
context.report({ | ||
message: 'Call to pending() within test', | ||
|
@@ -76,6 +115,7 @@ module.exports = { | |
}); | ||
} | ||
break; | ||
} | ||
|
||
case 'xdescribe': | ||
context.report({ message: 'Disabled test suite', node }); | ||
|
There was a problem hiding this comment.
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 theSet
global.