From 0ff29dc7ea7096db07fb2edc76db29c34dfdf94c Mon Sep 17 00:00:00 2001 From: Mathias Schreck Date: Fri, 25 Oct 2019 16:26:29 +0200 Subject: [PATCH] Fix no-setup-in-describe to allow mocha config calls --- lib/util/ast.js | 22 ++++++++++++++------- test/rules/no-setup-in-describe.js | 31 ++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 7 deletions(-) diff --git a/lib/util/ast.js b/lib/util/ast.js index 13285c6..205b659 100644 --- a/lib/util/ast.js +++ b/lib/util/ast.js @@ -49,20 +49,28 @@ function isHookCall(node) { return isCallExpression(node) && isHookIdentifier(node.callee); } +function isTestCase(node) { + return isCallExpression(node) && testCaseNames.indexOf(getNodeName(node.callee)) > -1; +} + function isSuiteConfigExpression(node) { - return node.type === 'MemberExpression' && - node.object.type === 'ThisExpression' && - suiteConfig.indexOf(getPropertyName(node.property)) !== -1; + if (node.type !== 'MemberExpression') { + return false; + } + + const usingThis = node.object.type === 'ThisExpression'; + + if (usingThis || isTestCase(node.object)) { + return suiteConfig.includes(getPropertyName(node.property)); + } + + return false; } function isSuiteConfigCall(node) { return isCallExpression(node) && isSuiteConfigExpression(node.callee); } -function isTestCase(node) { - return isCallExpression(node) && testCaseNames.indexOf(getNodeName(node.callee)) > -1; -} - function findReference(scope, node) { const hasSameRangeAsNode = pathEq([ 'identifier', 'range' ], node.range); diff --git a/test/rules/no-setup-in-describe.js b/test/rules/no-setup-in-describe.js index 21e98ad..6bbd875 100644 --- a/test/rules/no-setup-in-describe.js +++ b/test/rules/no-setup-in-describe.js @@ -29,6 +29,11 @@ ruleTester.run('no-setup-in-describe', rule, { 'describe("", function () { it("", function () { a["b"]; }); })', 'describe("", function () { it("", function () { this.slow(1); }); })', 'describe("", function () { it("", function () { this.timeout(1); }); })', + 'describe("", function () { it("", function () {}).timeout(1); })', + 'describe("", function () { it("", function () {}).slow(1); })', + 'describe("", function () { it.only("", function () {}).timeout(1); })', + 'describe("", function () { it.skip("", function () {}).timeout(1); })', + 'describe("", function () { xspecify("", function () {}).timeout(1); })', 'describe("", function () { it("", function () { this.retries(1); }); })', 'describe("", function () { it("", function () { this["retries"](1); }); })', 'describe("", function () { function a() { b(); }; it(); })', @@ -156,6 +161,32 @@ ruleTester.run('no-setup-in-describe', rule, { 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 + }, { + messagee: memberExpressionError, + line: 1, + column: 28 + }, { + message: 'Unexpected function call in describe block.', + line: 1, + column: 28 + } ] } ] });