Skip to content

Commit

Permalink
Add rule no-hooks (fixes #39)
Browse files Browse the repository at this point in the history
  • Loading branch information
jfmengels committed Jul 26, 2016
1 parent 185b419 commit 6366a3a
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 1 deletion.
1 change: 1 addition & 0 deletions docs/rules/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@
* [valid-test-description](valid-test-description.md) - match test descriptions against a pre-configured regular expression
* [valid-suite-description](valid-suite-description.md) - match suite descriptions against a pre-configured regular expression
* [no-mocha-arrows](no-mocha-arrows.md) - disallow arrow functions as arguments to mocha globals
* [no-hooks](no-hooks.md) - disallow hooks
48 changes: 48 additions & 0 deletions docs/rules/no-hooks.md
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.
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ module.exports = {
'no-global-tests': require('./lib/rules/no-global-tests'),
'valid-test-description': require('./lib/rules/valid-test-description'),
'valid-suite-description': require('./lib/rules/valid-suite-description'),
'no-mocha-arrows': require('./lib/rules/no-mocha-arrows')
'no-mocha-arrows': require('./lib/rules/no-mocha-arrows'),
'no-hooks': require('./lib/rules/no-hooks')
},
configs: {
recommended: {
Expand Down
16 changes: 16 additions & 0 deletions lib/rules/no-hooks.js
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'
});
}
}
};
};
47 changes: 47 additions & 0 deletions test/rules/no-hooks.js
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 } ]
}
]

});

0 comments on commit 6366a3a

Please sign in to comment.