-
-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(no-global-setup): add no-global-setup rule
- Loading branch information
Showing
5 changed files
with
77 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() { /* ... */ }) | ||
}) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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-- | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`.'}] | ||
} | ||
] | ||
}) |