Skip to content

Commit 8778d96

Browse files
committed
no-hooks: add option to allow certain kind of hooks
1 parent 36c9e67 commit 8778d96

File tree

3 files changed

+84
-2
lines changed

3 files changed

+84
-2
lines changed

docs/rules/no-hooks.md

+15
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,21 @@ describe('foo', function () {
4343
});
4444
```
4545

46+
## Options
47+
48+
This rule supports the following options:
49+
50+
* `allow`: An array containing the names of hooks to allow. Defaults to an empty array.
51+
52+
```json
53+
{
54+
"rules": {
55+
"mocha/no-hooks": ["error", {"allow": ["after"]}]
56+
}
57+
}
58+
```
59+
60+
4661
## When Not To Use It
4762

4863
* 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.

lib/rules/no-hooks.js

+23-1
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,32 @@
33
const astUtil = require('../util/ast');
44

55
module.exports = {
6+
meta: {
7+
schema: [
8+
{
9+
type: 'object',
10+
properties: {
11+
allow: {
12+
type: 'array',
13+
items: {
14+
type: 'string'
15+
}
16+
}
17+
},
18+
additionalProperties: false
19+
}
20+
]
21+
},
22+
623
create(context) {
24+
const [ config = {} ] = context.options;
25+
const { allow = [] } = config;
26+
727
return {
828
CallExpression(node) {
9-
if (astUtil.isHookIdentifier(node.callee)) {
29+
const isHookAllowed = allow.includes(node.callee.name);
30+
31+
if (astUtil.isHookIdentifier(node.callee) && !isHookAllowed) {
1032
context.report({
1133
node: node.callee,
1234
message: `Unexpected use of Mocha \`${ node.callee.name }\` hook`

test/rules/no-hooks.js

+46-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,47 @@ ruleTester.run('no-hooks', rules['no-hooks'], {
1818
'after.foo()',
1919
'beforeEach.foo()',
2020
'afterEach.foo()',
21-
'var before = 2; before + 3;'
21+
'var before = 2; before + 3;',
22+
{
23+
code: 'describe(function() { before(function() {}); });',
24+
options: [ { allow: [ 'before' ] } ]
25+
},
26+
{
27+
code: 'describe(function() { after(function() {}); });',
28+
options: [ { allow: [ 'after' ] } ]
29+
},
30+
{
31+
code: 'describe(function() { beforeEach(function() {}); });',
32+
options: [ { allow: [ 'beforeEach' ] } ]
33+
},
34+
{
35+
code: 'describe(function() { afterEach(function() {}); });',
36+
options: [ { allow: [ 'afterEach' ] } ]
37+
},
38+
{
39+
code: 'describe(function() { beforeAll(function() {}); });',
40+
options: [ { allow: [ 'beforeAll' ] } ]
41+
},
42+
{
43+
code: 'describe(function() { afterAll(function() {}); });',
44+
options: [ { allow: [ 'afterAll' ] } ]
45+
},
46+
{
47+
code: 'describe(function() { setup(function() {}); });',
48+
options: [ { allow: [ 'setup' ] } ]
49+
},
50+
{
51+
code: 'describe(function() { teardown(function() {}); });',
52+
options: [ { allow: [ 'teardown' ] } ]
53+
},
54+
{
55+
code: 'describe(function() { suiteSetup(function() {}); });',
56+
options: [ { allow: [ 'suiteSetup' ] } ]
57+
},
58+
{
59+
code: 'describe(function() { suiteTeardown(function() {}); });',
60+
options: [ { allow: [ 'suiteTeardown' ] } ]
61+
}
2262
],
2363

2464
invalid: [
@@ -41,6 +81,11 @@ ruleTester.run('no-hooks', rules['no-hooks'], {
4181
{
4282
code: 'describe(function() { describe(function() { before(function() {}); }); });',
4383
errors: [ { message: 'Unexpected use of Mocha `before` hook', column: 45, line: 1 } ]
84+
},
85+
{
86+
code: 'describe(function() { after(function() {}); });',
87+
options: [ { allow: [ 'before' ] } ],
88+
errors: [ { message: 'Unexpected use of Mocha `after` hook', column: 23, line: 1 } ]
4489
}
4590
]
4691

0 commit comments

Comments
 (0)