Skip to content

Commit

Permalink
Fix max-top-level-suites to work with ES modules
Browse files Browse the repository at this point in the history
  • Loading branch information
lo1tuma committed May 29, 2020
1 parent d095e93 commit 7b07212
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
6 changes: 5 additions & 1 deletion lib/rules/max-top-level-suites.js
Expand Up @@ -11,6 +11,10 @@ const { additionalSuiteNames } = require('../util/settings');

const defaultSuiteLimit = 1;

function isTopLevelScope(scope) {
return scope.type === 'module' || scope.upper === null;
}

module.exports = {
meta: {
type: 'suggestion',
Expand Down Expand Up @@ -43,7 +47,7 @@ module.exports = {
if (astUtil.isDescribe(node, additionalSuiteNames(settings))) {
const scope = context.getScope();

if (scope.upper === null) {
if (isTopLevelScope(scope)) {
topLevelDescribes.push(node);
}
}
Expand Down
27 changes: 26 additions & 1 deletion test/rules/max-top-level-suites.js
Expand Up @@ -54,7 +54,21 @@ ruleTester.run('max-top-level-suites', rules['max-top-level-suites'], {
}
},
'someOtherFunction();',
'describe("top", function () {}); function foo() { describe("not necessarily top", function () {}); }'
'describe("top", function () {}); function foo() { describe("not necessarily top", function () {}); }',
{
code: 'describe("", function () { });',
parserOptions: {
sourceType: 'module',
ecmaVersion: 2015
}
},
{
code: 'describe("", function () { describe("", function () {}); });',
parserOptions: {
sourceType: 'module',
ecmaVersion: 2015
}
}
],

invalid: [
Expand All @@ -65,6 +79,17 @@ ruleTester.run('max-top-level-suites', rules['max-top-level-suites'], {
{ message: 'The number of top-level suites is more than 1.' }
]
},
{
code: 'describe("", function () { });' +
'describe("", function () { });',
parserOptions: {
sourceType: 'module',
ecmaVersion: 2015
},
errors: [
{ message: 'The number of top-level suites is more than 1.' }
]
},
{
code: 'describe("this is a test", function () { });' +
'describe("this is a different test", function () { });' +
Expand Down

0 comments on commit 7b07212

Please sign in to comment.