Skip to content

Commit fdce162

Browse files
macklinuSimenB
authored andcommittedSep 21, 2018
fix(no-disabled-tests): fix false positives for pending() usage (#155)
Fixes #149
1 parent 1361d59 commit fdce162

File tree

3 files changed

+76
-2
lines changed

3 files changed

+76
-2
lines changed
 

Diff for: ‎.eslintrc.js

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ module.exports = {
1414
},
1515
env: {
1616
node: true,
17+
es6: true,
1718
},
1819
rules: {
1920
eqeqeq: ['error', 'smart'],

Diff for: ‎rules/__tests__/no-disabled-tests.test.js

+34-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@
33
const RuleTester = require('eslint').RuleTester;
44
const rule = require('../no-disabled-tests');
55

6-
const ruleTester = new RuleTester();
6+
const ruleTester = new RuleTester({
7+
parserOptions: {
8+
sourceType: 'module',
9+
},
10+
});
711

812
ruleTester.run('no-disabled-tests', rule, {
913
valid: [
@@ -17,6 +21,35 @@ ruleTester.run('no-disabled-tests', rule, {
1721
'var calledSkip = it.skip; calledSkip.call(it)',
1822
'({ f: function () {} }).f()',
1923
'(a || b).f()',
24+
[
25+
'import { pending } from "actions"',
26+
'',
27+
'test("foo", () => {',
28+
' expect(pending()).toEqual({})',
29+
'})',
30+
].join('\n'),
31+
[
32+
'const { pending } = require("actions")',
33+
'',
34+
'test("foo", () => {',
35+
' expect(pending()).toEqual({})',
36+
'})',
37+
].join('\n'),
38+
[
39+
'test("foo", () => {',
40+
' const pending = getPending()',
41+
' expect(pending()).toEqual({})',
42+
'})',
43+
].join('\n'),
44+
[
45+
'test("foo", () => {',
46+
' expect(pending()).toEqual({})',
47+
'})',
48+
'',
49+
'function pending() {',
50+
' return {}',
51+
'}',
52+
].join('\n'),
2053
],
2154

2255
invalid: [

Diff for: ‎rules/no-disabled-tests.js

+41-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,33 @@ function getName(node) {
1919
return null;
2020
}
2121

22+
function collectReferences(scope) {
23+
const locals = new Set();
24+
const unresolved = new Set();
25+
26+
let currentScope = scope;
27+
28+
while (currentScope !== null) {
29+
for (const ref of currentScope.variables) {
30+
const isReferenceDefined = ref.defs.some(def => {
31+
return def.type !== 'ImplicitGlobalVariable';
32+
});
33+
34+
if (isReferenceDefined) {
35+
locals.add(ref.name);
36+
}
37+
}
38+
39+
for (const ref of currentScope.through) {
40+
unresolved.add(ref.identifier.name);
41+
}
42+
43+
currentScope = currentScope.upper;
44+
}
45+
46+
return { locals, unresolved };
47+
}
48+
2249
module.exports = {
2350
meta: {
2451
docs: {
@@ -58,7 +85,19 @@ module.exports = {
5885
context.report({ message: 'Skipped test', node });
5986
break;
6087

61-
case 'pending':
88+
case 'pending': {
89+
const references = collectReferences(context.getScope());
90+
91+
if (
92+
// `pending` was found as a local variable or function declaration.
93+
references.locals.has('pending') ||
94+
// `pending` was not found as an unresolved reference,
95+
// meaning it is likely not an implicit global reference.
96+
!references.unresolved.has('pending')
97+
) {
98+
break;
99+
}
100+
62101
if (testDepth > 0) {
63102
context.report({
64103
message: 'Call to pending() within test',
@@ -76,6 +115,7 @@ module.exports = {
76115
});
77116
}
78117
break;
118+
}
79119

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

0 commit comments

Comments
 (0)
Please sign in to comment.