Skip to content

Commit

Permalink
Merge pull request #293 from lo1tuma/no-setup-in-describe-fix
Browse files Browse the repository at this point in the history
Fix false positive in no-setup-in-describe
  • Loading branch information
lo1tuma committed May 26, 2021
2 parents d830482 + 8cbda0a commit 4446dc2
Show file tree
Hide file tree
Showing 2 changed files with 172 additions and 95 deletions.
11 changes: 11 additions & 0 deletions lib/rules/no-setup-in-describe.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,17 @@ module.exports = {
}
},

FunctionExpression(node) {
if (nesting.length && !isParentDescribe(node)) {
nesting.push(FUNCTION);
}
},
'FunctionExpression:exit'(node) {
if (nesting.length && !isParentDescribe(node)) {
nesting.pop();
}
},

ArrowFunctionExpression(node) {
if (nesting.length && !isParentDescribe(node)) {
nesting.push(FUNCTION);
Expand Down
256 changes: 161 additions & 95 deletions test/rules/no-setup-in-describe.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
const RuleTester = require('eslint').RuleTester;
const rule = require('../../lib/rules/no-setup-in-describe');
const ruleTester = new RuleTester();
const memberExpressionError = 'Unexpected member expression in describe block. ' +
'Member expressions may call functions via getters.';
const memberExpressionError =
'Unexpected member expression in describe block. ' +
'Member expressions may call functions via getters.';

ruleTester.run('no-setup-in-describe', rule, {
valid: [
Expand Down Expand Up @@ -50,26 +51,40 @@ ruleTester.run('no-setup-in-describe', rule, {
code: 'describe("", function () { before(() => { b(); }); it(); })',
parserOptions: { ecmaVersion: 6 }
},
{ code: 'describe("", function () { var a = () => b(); it(); })', parserOptions: { ecmaVersion: 6 } },
{ code: 'describe("", function () { var a = () => b.c; it(); })', parserOptions: { ecmaVersion: 6 } },
{
code: 'describe("", function () { var a = () => b(); it(); })',
parserOptions: { ecmaVersion: 6 }
},
{
code: 'describe("", function () { var a = () => b.c; it(); })',
parserOptions: { ecmaVersion: 6 }
},
'describe("", function () { describe("", function () { it(); }); it(); })',
{
code: 'foo("", function () { it(); })',
settings: {
'mocha/additionalCustomNames': [ { name: 'foo', type: 'suite', interfaces: [ 'BDD' ] } ]
'mocha/additionalCustomNames': [
{ name: 'foo', type: 'suite', interfaces: [ 'BDD' ] }
]
}
}, {
},
{
code: 'foo("", function () { it(); })',
settings: {
mocha: {
additionalCustomNames: [ { name: 'foo', type: 'suite', interfaces: [ 'BDD' ] } ]
additionalCustomNames: [
{ name: 'foo', type: 'suite', interfaces: [ 'BDD' ] }
]
}
}
}, {
},
{
code: 'foo("", function () { it("", function () { b(); }); })',
settings: {
mocha: {
additionalCustomNames: [ { name: 'foo', type: 'suite', interfaces: [ 'BDD' ] } ]
additionalCustomNames: [
{ name: 'foo', type: 'suite', interfaces: [ 'BDD' ] }
]
}
}
},
Expand All @@ -82,125 +97,176 @@ ruleTester.run('no-setup-in-describe', rule, {
});
});`,
parserOptions: { ecmaVersion: 2015 }
}
},
'describe("", function () { function bar() { a.b = "c"; } it(); })',
{
code:
'describe("", function () { const bar = () => { a.b = "c"; }; it(); })',
parserOptions: { ecmaVersion: 2015 }
},
'describe("", function () { var bar = function () { a.b = "c"; }; it(); })'
],

invalid: [
{
code: 'suite("", function () { a(); });',
errors: [ {
message: 'Unexpected function call in describe block.',
line: 1,
column: 25
} ]
}, {
errors: [
{
message: 'Unexpected function call in describe block.',
line: 1,
column: 25
}
]
},
{
code: 'describe("", function () { a(); });',
errors: [ {
message: 'Unexpected function call in describe block.',
line: 1,
column: 28
} ]
}, {
errors: [
{
message: 'Unexpected function call in describe block.',
line: 1,
column: 28
}
]
},
{
code: 'describe("", () => { a(); });',
parserOptions: { ecmaVersion: 2015 },
errors: [ {
message: 'Unexpected function call in describe block.',
line: 1,
column: 22
} ]
}, {
errors: [
{
message: 'Unexpected function call in describe block.',
line: 1,
column: 22
}
]
},
{
code: 'foo("", function () { a(); });',
settings: {
mocha: {
additionalCustomNames: [ { name: 'foo', type: 'suite', interfaces: [ 'BDD' ] } ]
additionalCustomNames: [
{ name: 'foo', type: 'suite', interfaces: [ 'BDD' ] }
]
}
},
errors: [ {
message: 'Unexpected function call in describe block.',
line: 1,
column: 23
} ]
}, {
errors: [
{
message: 'Unexpected function call in describe block.',
line: 1,
column: 23
}
]
},
{
code: 'foo("", function () { a[b]; });',
settings: {
mocha: {
additionalCustomNames: [ { name: 'foo', type: 'suite', interfaces: [ 'BDD' ] } ]
additionalCustomNames: [
{ name: 'foo', type: 'suite', interfaces: [ 'BDD' ] }
]
}
},
errors: [ {
message: memberExpressionError,
line: 1,
column: 23
} ]
}, {
errors: [
{
message: memberExpressionError,
line: 1,
column: 23
}
]
},
{
code: 'foo("", function () { a["b"]; });',
settings: {
mocha: {
additionalCustomNames: [ { name: 'foo', type: 'suite', interfaces: [ 'BDD' ] } ]
additionalCustomNames: [
{ name: 'foo', type: 'suite', interfaces: [ 'BDD' ] }
]
}
},
errors: [ {
message: memberExpressionError,
line: 1,
column: 23
} ]
errors: [
{
message: memberExpressionError,
line: 1,
column: 23
}
]
},
{
code: 'describe("", function () { a.b; });',
errors: [ {
message: memberExpressionError,
line: 1,
column: 28
} ]
}, {
errors: [
{
message: memberExpressionError,
line: 1,
column: 28
}
]
},
{
code: 'describe("", function () { this.a(); });',
errors: [ {
message: 'Unexpected function call in describe block.',
line: 1,
column: 28
}, {
message: memberExpressionError,
line: 1,
column: 28
} ]
}, {
errors: [
{
message: 'Unexpected function call in describe block.',
line: 1,
column: 28
},
{
message: memberExpressionError,
line: 1,
column: 28
}
]
},
{
code: 'foo("", function () { a.b; });',
settings: {
mocha: {
additionalCustomNames: [ { name: 'foo', type: 'suite', interfaces: [ 'BDD' ] } ]
additionalCustomNames: [
{ name: 'foo', type: 'suite', interfaces: [ 'BDD' ] }
]
}
},
errors: [ {
message: memberExpressionError,
line: 1,
column: 23
} ]
}, {
errors: [
{
message: memberExpressionError,
line: 1,
column: 23
}
]
},
{
code: 'describe("", function () { it("", function () {}).a(); });',
errors: [ {
message: 'Unexpected function call in describe block.',
line: 1,
column: 28
}, {
message: memberExpressionError,
line: 1,
column: 28
} ]
}, {
code: 'describe("", function () { something("", function () {}).timeout(); });',
errors: [ {
message: 'Unexpected function call in describe block.',
line: 1,
column: 28
}, {
message: memberExpressionError,
line: 1,
column: 28
}, {
message: 'Unexpected function call in describe block.',
line: 1,
column: 28
} ]
errors: [
{
message: 'Unexpected function call in describe block.',
line: 1,
column: 28
},
{
message: memberExpressionError,
line: 1,
column: 28
}
]
},
{
code:
'describe("", function () { something("", function () {}).timeout(); });',
errors: [
{
message: 'Unexpected function call in describe block.',
line: 1,
column: 28
},
{
message: memberExpressionError,
line: 1,
column: 28
},
{
message: 'Unexpected function call in describe block.',
line: 1,
column: 28
}
]
}
]
});

0 comments on commit 4446dc2

Please sign in to comment.