Skip to content

Commit

Permalink
Merge pull request #236 from lo1tuma/no-hooks-option
Browse files Browse the repository at this point in the history
no-hooks: add option to allow certain kind of hooks
  • Loading branch information
lo1tuma committed Feb 18, 2020
2 parents 36c9e67 + 8778d96 commit 7eea93d
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 2 deletions.
15 changes: 15 additions & 0 deletions docs/rules/no-hooks.md
Expand Up @@ -43,6 +43,21 @@ describe('foo', function () {
});
```

## Options

This rule supports the following options:

* `allow`: An array containing the names of hooks to allow. Defaults to an empty array.

```json
{
"rules": {
"mocha/no-hooks": ["error", {"allow": ["after"]}]
}
}
```


## 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.
24 changes: 23 additions & 1 deletion lib/rules/no-hooks.js
Expand Up @@ -3,10 +3,32 @@
const astUtil = require('../util/ast');

module.exports = {
meta: {
schema: [
{
type: 'object',
properties: {
allow: {
type: 'array',
items: {
type: 'string'
}
}
},
additionalProperties: false
}
]
},

create(context) {
const [ config = {} ] = context.options;
const { allow = [] } = config;

return {
CallExpression(node) {
if (astUtil.isHookIdentifier(node.callee)) {
const isHookAllowed = allow.includes(node.callee.name);

if (astUtil.isHookIdentifier(node.callee) && !isHookAllowed) {
context.report({
node: node.callee,
message: `Unexpected use of Mocha \`${ node.callee.name }\` hook`
Expand Down
47 changes: 46 additions & 1 deletion test/rules/no-hooks.js
Expand Up @@ -18,7 +18,47 @@ ruleTester.run('no-hooks', rules['no-hooks'], {
'after.foo()',
'beforeEach.foo()',
'afterEach.foo()',
'var before = 2; before + 3;'
'var before = 2; before + 3;',
{
code: 'describe(function() { before(function() {}); });',
options: [ { allow: [ 'before' ] } ]
},
{
code: 'describe(function() { after(function() {}); });',
options: [ { allow: [ 'after' ] } ]
},
{
code: 'describe(function() { beforeEach(function() {}); });',
options: [ { allow: [ 'beforeEach' ] } ]
},
{
code: 'describe(function() { afterEach(function() {}); });',
options: [ { allow: [ 'afterEach' ] } ]
},
{
code: 'describe(function() { beforeAll(function() {}); });',
options: [ { allow: [ 'beforeAll' ] } ]
},
{
code: 'describe(function() { afterAll(function() {}); });',
options: [ { allow: [ 'afterAll' ] } ]
},
{
code: 'describe(function() { setup(function() {}); });',
options: [ { allow: [ 'setup' ] } ]
},
{
code: 'describe(function() { teardown(function() {}); });',
options: [ { allow: [ 'teardown' ] } ]
},
{
code: 'describe(function() { suiteSetup(function() {}); });',
options: [ { allow: [ 'suiteSetup' ] } ]
},
{
code: 'describe(function() { suiteTeardown(function() {}); });',
options: [ { allow: [ 'suiteTeardown' ] } ]
}
],

invalid: [
Expand All @@ -41,6 +81,11 @@ ruleTester.run('no-hooks', rules['no-hooks'], {
{
code: 'describe(function() { describe(function() { before(function() {}); }); });',
errors: [ { message: 'Unexpected use of Mocha `before` hook', column: 45, line: 1 } ]
},
{
code: 'describe(function() { after(function() {}); });',
options: [ { allow: [ 'before' ] } ],
errors: [ { message: 'Unexpected use of Mocha `after` hook', column: 23, line: 1 } ]
}
]

Expand Down

0 comments on commit 7eea93d

Please sign in to comment.