From 285dfafa85976d8f4fe2499bfdfd38ee37bde190 Mon Sep 17 00:00:00 2001 From: omerganim Date: Sun, 6 Nov 2016 07:39:54 +0200 Subject: [PATCH] feat(no-global-setup): add no-global-setup rule --- README.md | 2 ++ docs/rules/no-global-setup.md | 22 ++++++++++++++++++++++ index.js | 6 ++++-- lib/rules/no-global-setup.js | 27 +++++++++++++++++++++++++++ test/rules/no-global-setup.js | 22 ++++++++++++++++++++++ 5 files changed, 77 insertions(+), 2 deletions(-) create mode 100644 docs/rules/no-global-setup.md create mode 100644 lib/rules/no-global-setup.js create mode 100644 test/rules/no-global-setup.js diff --git a/README.md b/README.md index 9cf247e..7ecfd5a 100644 --- a/README.md +++ b/README.md @@ -76,6 +76,7 @@ Rule | Recommended | Options [valid-expect][] | 1 | [no-assign-spyon][] | 0 | [no-unsafe-spy][] | 1 | +[no-global-setup][] | 1 | For example, using the recommended configuration, the `no-focused-tests` rule is enabled and will cause ESLint to throw an error (with an exit code of `1`) @@ -108,6 +109,7 @@ See [configuring rules][] for more information. [valid-expect]: docs/rules/valid-expect.md [no-assign-spyon]: docs/rules/no-assign-spyon.md [no-unsafe-spy]: docs/rules/no-unsafe-spy.md +[no-global-setup]: docs/rules/no-global-setup.md [configuring rules]: http://eslint.org/docs/user-guide/configuring#configuring-rules diff --git a/docs/rules/no-global-setup.md b/docs/rules/no-global-setup.md new file mode 100644 index 0000000..f6a39af --- /dev/null +++ b/docs/rules/no-global-setup.md @@ -0,0 +1,22 @@ +# Disallow using setup and teardown methods outside a suite + +Make sure that all uses of the global `beforeEach`, `afterEach`, `beforeAll`, and `afterAll` methods are within a suite. + +## Rule details + +The following are considered warnings: + +```js +beforeEach(function() { /* ... */ }) + +afterEach(function() { /* ... */ }) + +``` + +The following patterns are not warnings: + +```js +describe(function() { + beforeEach(function() { /* ... */ }) +}) +``` diff --git a/index.js b/index.js index 0608add..c29bef2 100644 --- a/index.js +++ b/index.js @@ -11,7 +11,8 @@ module.exports = { 'no-suite-callback-args': require('./lib/rules/no-suite-callback-args'), 'valid-expect': require('./lib/rules/valid-expect'), 'no-assign-spyon': require('./lib/rules/no-assign-spyon'), - 'no-unsafe-spy': require('./lib/rules/no-unsafe-spy') + 'no-unsafe-spy': require('./lib/rules/no-unsafe-spy'), + 'no-global-setup': require('./lib/rules/no-global-setup') }, configs: { recommended: { @@ -25,7 +26,8 @@ module.exports = { 'jasmine/no-suite-callback-args': 2, 'jasmine/valid-expect': 1, 'jasmine/no-assign-spyon': 0, - 'jasmine/no-unsafe-spy': 1 + 'jasmine/no-unsafe-spy': 1, + 'jasmine/no-global-setup': 1 } } } diff --git a/lib/rules/no-global-setup.js b/lib/rules/no-global-setup.js new file mode 100644 index 0000000..ff932c7 --- /dev/null +++ b/lib/rules/no-global-setup.js @@ -0,0 +1,27 @@ +'use strict' + +/** + * @fileoverview Disallow using setup and teardown methods outside a suite + * @author Omer Ganim + */ + +var suiteRegexp = /^(f|d|x)?describe$/ +var setupRegexp = /^(before|after)(Each|All)$/ + +module.exports = function (context) { + var suiteDepth = 0 + return { + CallExpression: function (node) { + if (suiteRegexp.test(node.callee.name)) { + suiteDepth++ + } else if (setupRegexp.test(node.callee.name) && suiteDepth === 0) { + context.report(node, 'Do not use `' + node.callee.name + '` outside a `describe`.') + } + }, + 'CallExpression:exit': function (node) { + if (suiteRegexp.test(node.callee.name)) { + suiteDepth-- + } + } + } +} diff --git a/test/rules/no-global-setup.js b/test/rules/no-global-setup.js new file mode 100644 index 0000000..672e5f5 --- /dev/null +++ b/test/rules/no-global-setup.js @@ -0,0 +1,22 @@ +'use strict' + +var rule = require('../../lib/rules/no-global-setup') +var RuleTester = require('eslint').RuleTester + +var eslintTester = new RuleTester() + +eslintTester.run('no-global-setup', rule, { + valid: [ + 'describe("", function() { beforeEach(function() {}) })' + ], + invalid: [ + { + code: 'beforeEach(function() {})', + errors: [{message: 'Do not use `beforeEach` outside a `describe`.'}] + }, + { + code: 'afterEach(function() {})', + errors: [{message: 'Do not use `afterEach` outside a `describe`.'}] + } + ] +})