-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #84 from jfmengels/issue-39
Add rule `no-hooks` (fixes #39)
- Loading branch information
Showing
5 changed files
with
114 additions
and
1 deletion.
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,48 @@ | ||
# Disallow hooks (no-hooks) | ||
|
||
Mocha proposes hooks that allow code to be run before or after every or all tests. This helps define a common setup or teardown process for every test. The use of these hooks promotes the use of shared state between the tests, and defeats the purpose of having isolated unit tests. | ||
|
||
## Rule Details | ||
|
||
This rule looks for every call to `before`, `after`, `beforeEach` and `afterEach`. | ||
|
||
The following patterns are considered warnings: | ||
|
||
```js | ||
describe('foo', function () { | ||
var mockUser; | ||
|
||
before(function () { // Not allowed | ||
mockUser = {age: 50}; | ||
}); | ||
|
||
after(function () { /* ... */ }); // Not allowed | ||
beforeEach(function () { /* ... */ }); // Not allowed | ||
afterEach(function () { /* ... */ }); // Not allowed | ||
|
||
it(function () { | ||
assert.equals(lib.method(mockUser), 'expectedValue'); | ||
}); | ||
}); | ||
``` | ||
|
||
These patterns would not be considered warnings: | ||
|
||
```js | ||
function createFixtures() { | ||
return { | ||
mockUser: {age: 50} | ||
}; | ||
} | ||
|
||
describe('foo', function () { | ||
it(function () { | ||
var fixtures = createFixtures(); | ||
assert.equals(lib.method(fixtures.mockUser), 'expectedValue'); | ||
}); | ||
}); | ||
``` | ||
|
||
## When Not To Use It | ||
|
||
* If you use another library which exposes a similar API as mocha (e.g. `before`, `after`), you should turn this rule off, because it would raise warnings. |
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,16 @@ | ||
'use strict'; | ||
|
||
module.exports = function (context) { | ||
var hooks = [ 'before', 'after', 'beforeEach', 'afterEach' ]; | ||
|
||
return { | ||
CallExpression: function (node) { | ||
if (node.callee.type === 'Identifier' && hooks.indexOf(node.callee.name) !== -1) { | ||
context.report({ | ||
node: node.callee, | ||
message: 'Unexpected use of Mocha `' + node.callee.name + '` hook' | ||
}); | ||
} | ||
} | ||
}; | ||
}; |
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,47 @@ | ||
'use strict'; | ||
|
||
var RuleTester = require('eslint').RuleTester, | ||
rules = require('../../').rules, | ||
ruleTester = new RuleTester(); | ||
|
||
ruleTester.run('no-hooks', rules['no-hooks'], { | ||
|
||
valid: [ | ||
'describe(function() { it(function() {}); });', | ||
'describe(function() { it(function() {}); it(function() {}); });', | ||
'describe(function() { describe(function() { it(function() {}); }); });', | ||
'foo.before()', | ||
'foo.after()', | ||
'foo.beforeEach()', | ||
'foo.afterEach()', | ||
'before.foo()', | ||
'after.foo()', | ||
'beforeEach.foo()', | ||
'afterEach.foo()', | ||
'var before = 2; before + 3;' | ||
], | ||
|
||
invalid: [ | ||
{ | ||
code: 'describe(function() { before(function() {}); });', | ||
errors: [ { message: 'Unexpected use of Mocha `before` hook', column: 23, line: 1 } ] | ||
}, | ||
{ | ||
code: 'describe(function() { after(function() {}); });', | ||
errors: [ { message: 'Unexpected use of Mocha `after` hook', column: 23, line: 1 } ] | ||
}, | ||
{ | ||
code: 'describe(function() { beforeEach(function() {}); });', | ||
errors: [ { message: 'Unexpected use of Mocha `beforeEach` hook', column: 23, line: 1 } ] | ||
}, | ||
{ | ||
code: 'describe(function() { afterEach(function() {}); });', | ||
errors: [ { message: 'Unexpected use of Mocha `afterEach` hook', column: 23, line: 1 } ] | ||
}, | ||
{ | ||
code: 'describe(function() { describe(function() { before(function() {}); }); });', | ||
errors: [ { message: 'Unexpected use of Mocha `before` hook', column: 45, line: 1 } ] | ||
} | ||
] | ||
|
||
}); |