Skip to content

Commit

Permalink
feat(no-global-setup): add no-global-setup rule
Browse files Browse the repository at this point in the history
  • Loading branch information
ganimomer authored and Nicolas Fernandez committed Nov 7, 2016
1 parent 9151c33 commit 285dfaf
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 2 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`)
Expand Down Expand Up @@ -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
Expand Down
22 changes: 22 additions & 0 deletions docs/rules/no-global-setup.md
Original file line number Diff line number Diff line change
@@ -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() { /* ... */ })
})
```
6 changes: 4 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -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
}
}
}
Expand Down
27 changes: 27 additions & 0 deletions lib/rules/no-global-setup.js
Original file line number Diff line number Diff line change
@@ -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--
}
}
}
}
22 changes: 22 additions & 0 deletions test/rules/no-global-setup.js
Original file line number Diff line number Diff line change
@@ -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`.'}]
}
]
})

0 comments on commit 285dfaf

Please sign in to comment.