-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
344 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,64 @@ | ||
# Disallow hooks for a single test or test suite (no-hooks-for-single-case) | ||
|
||
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. These hooks are not useful when there is only one test case, as it would then make more sense to move the hooks' operations in the test directly. | ||
|
||
## Rule Details | ||
|
||
This rule looks for every call to `before`, `after`, `beforeEach` and `afterEach` and reports them if they are called when there is less than two tests and/or tests suites in the same test suite. | ||
|
||
The following patterns are considered warnings: | ||
|
||
```js | ||
describe('foo', function () { | ||
before(function () { /* ... */ }); // Not allowed as there is only a test suite next to this hook. | ||
|
||
describe('bar', function() { | ||
/* ... */ | ||
}); | ||
}); | ||
|
||
describe('foo', function () { | ||
after(function () { /* ... */ }); // Not allowed as there is only a test case next to this hook. | ||
|
||
it('should XYZ', function() { | ||
/* ... */ | ||
}); | ||
}); | ||
|
||
describe('foo', function () { | ||
beforeEach(function () { /* ... */ }); // Not allowed as there is no test suites or cases next to this hook. | ||
}); | ||
``` | ||
|
||
These patterns would not be considered warnings: | ||
|
||
```js | ||
describe('foo', function () { | ||
before(function () { /* ... */ }); | ||
|
||
it('should XYZ', function() { | ||
/* ... */ | ||
}); | ||
|
||
it('should ABC', function() { | ||
/* ... */ | ||
}); | ||
}); | ||
|
||
describe('foo', function () { | ||
before(function () { /* ... */ }); | ||
|
||
it('should XYZ', function() { | ||
/* ... */ | ||
}); | ||
|
||
describe('bar', function() { | ||
/* ... */ | ||
}); | ||
}); | ||
``` | ||
|
||
## 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. | ||
* If you turned `no-hooks` on, you should turn this rule off, because it would raise several warnings for the same root cause. |
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,56 @@ | ||
'use strict'; | ||
|
||
var astUtil = require('../util/ast'); | ||
|
||
function newDescribeLayer(describeNode) { | ||
return { | ||
describeNode: describeNode, | ||
hookNodes: [], | ||
testCount: 0 | ||
}; | ||
} | ||
|
||
module.exports = function (context) { | ||
var layers = []; | ||
|
||
function popLayer(node) { | ||
var layer = layers[layers.length - 1]; | ||
if (layer.describeNode === node) { | ||
if (layer.testCount <= 1) { | ||
layer.hookNodes.forEach(function (hookNode) { | ||
context.report({ | ||
node: hookNode, | ||
// message: 'Unexpected use of duplicate Mocha `' + name + '` hook' | ||
message: 'Unexpected use of duplicate Mocha `before` hook' | ||
}); | ||
}); | ||
} | ||
layers.pop(); | ||
} | ||
} | ||
|
||
return { | ||
Program: function (node) { | ||
layers.push(newDescribeLayer(node)); | ||
}, | ||
|
||
CallExpression: function (node) { | ||
if (astUtil.isDescribe(node)) { | ||
layers[layers.length - 1].testCount += 1; | ||
layers.push(newDescribeLayer(node)); | ||
return; | ||
} | ||
|
||
if (astUtil.isTestCase(node)) { | ||
layers[layers.length - 1].testCount += 1; | ||
} | ||
|
||
if (astUtil.isHookIdentifier(node.callee)) { | ||
layers[layers.length - 1].hookNodes.push(node.callee); | ||
} | ||
}, | ||
|
||
'CallExpression:exit': popLayer, | ||
'Program:exit': popLayer | ||
}; | ||
}; |
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,205 @@ | ||
'use strict'; | ||
|
||
var RuleTester = require('eslint').RuleTester, | ||
rules = require('../../').rules, | ||
ruleTester = new RuleTester(); | ||
|
||
ruleTester.run('no-hooks-for-single-case', rules['no-hooks-for-single-case'], { | ||
|
||
valid: [ | ||
[ | ||
'describe(function() {', | ||
' it(function() {});', | ||
'});' | ||
].join('\n'), | ||
[ | ||
'describe(function() {', | ||
' before(function() {});', | ||
' it(function() {});', | ||
' it(function() {});', | ||
'});' | ||
].join('\n'), | ||
[ | ||
'describe(function() {', | ||
' after(function() {});', | ||
' it(function() {});', | ||
' it(function() {});', | ||
'});' | ||
].join('\n'), | ||
[ | ||
'describe(function() {', | ||
' beforeEach(function() {});', | ||
' it(function() {});', | ||
' it(function() {});', | ||
'});' | ||
].join('\n'), | ||
[ | ||
'describe(function() {', | ||
' afterEach(function() {});', | ||
' it(function() {});', | ||
' it(function() {});', | ||
'});' | ||
].join('\n'), | ||
[ | ||
'before(function() {});', | ||
'it(function() {});', | ||
'it(function() {});' | ||
].join('\n'), | ||
[ | ||
'describe(function() {', | ||
' before(function() {});', | ||
' it(function() {});', | ||
' describe(function() {});', | ||
'});' | ||
].join('\n'), | ||
[ | ||
'describe(function() {', | ||
' before(function() {});', | ||
' it(function() {});', | ||
' xdescribe(function() {});', | ||
'});' | ||
].join('\n'), | ||
[ | ||
'describe(function() {', | ||
' before(function() {});', | ||
' describe(function() {});', | ||
' describe(function() {});', | ||
'});' | ||
].join('\n'), | ||
[ | ||
'describe(function() {', | ||
' before(function() {});', | ||
' it.only(function() {});', | ||
' it(function() {});', | ||
'});' | ||
].join('\n'), | ||
[ | ||
'describe(function() {', | ||
' before(function() {});', | ||
' it(function() {});', | ||
' describe(function() {', | ||
' before(function() {});', | ||
' it(function() {});', | ||
' it(function() {});', | ||
' });', | ||
'});' | ||
].join('\n') | ||
], | ||
|
||
invalid: [ | ||
{ | ||
code: [ | ||
'describe(function() {', | ||
' before(function() {});', | ||
'});' | ||
].join('\n'), | ||
errors: [ { message: 'Unexpected use of duplicate Mocha `before` hook', column: 5, line: 2 } ] | ||
}, | ||
{ | ||
code: [ | ||
'describe(function() {', | ||
' before(function() {});', | ||
' it(function() {});', | ||
'});' | ||
].join('\n'), | ||
errors: [ { message: 'Unexpected use of duplicate Mocha `before` hook', column: 5, line: 2 } ] | ||
}, | ||
{ | ||
code: [ | ||
'describe(function() {', | ||
' it(function() {});', | ||
' before(function() {});', | ||
'});' | ||
].join('\n'), | ||
errors: [ { message: 'Unexpected use of duplicate Mocha `before` hook', column: 5, line: 3 } ] | ||
}, | ||
{ | ||
code: [ | ||
'describe(function() {', | ||
' after(function() {});', | ||
' it(function() {});', | ||
'});' | ||
].join('\n'), | ||
errors: [ { message: 'Unexpected use of duplicate Mocha `before` hook', column: 5, line: 2 } ] | ||
}, | ||
{ | ||
code: [ | ||
'describe(function() {', | ||
' beforeEach(function() {});', | ||
' it(function() {});', | ||
'});' | ||
].join('\n'), | ||
errors: [ { message: 'Unexpected use of duplicate Mocha `before` hook', column: 5, line: 2 } ] | ||
}, | ||
{ | ||
code: [ | ||
'describe(function() {', | ||
' afterEach(function() {});', | ||
' it(function() {});', | ||
'});' | ||
].join('\n'), | ||
errors: [ { message: 'Unexpected use of duplicate Mocha `before` hook', column: 5, line: 2 } ] | ||
}, | ||
{ | ||
code: [ | ||
'before(function() {});', | ||
'it(function() {});' | ||
].join('\n'), | ||
errors: [ { message: 'Unexpected use of duplicate Mocha `before` hook', column: 1, line: 1 } ] | ||
}, | ||
{ | ||
code: [ | ||
'describe(function() {', | ||
' before(function() {});', | ||
' describe(function() {});', | ||
'});' | ||
].join('\n'), | ||
errors: [ { message: 'Unexpected use of duplicate Mocha `before` hook', column: 5, line: 2 } ] | ||
}, | ||
{ | ||
code: [ | ||
'describe(function() {', | ||
' before(function() {});', | ||
' xdescribe(function() {});', | ||
'});' | ||
].join('\n'), | ||
errors: [ { message: 'Unexpected use of duplicate Mocha `before` hook', column: 5, line: 2 } ] | ||
}, | ||
{ | ||
code: [ | ||
'describe(function() {', | ||
' before(function() {});', | ||
' it.only(function() {});', | ||
'});' | ||
].join('\n'), | ||
errors: [ { message: 'Unexpected use of duplicate Mocha `before` hook', column: 5, line: 2 } ] | ||
}, | ||
{ | ||
code: [ | ||
'describe(function() {', | ||
' before(function() {});', | ||
' it(function() {});', | ||
' describe(function() {', | ||
' before(function() {});', | ||
' it(function() {});', | ||
' });', | ||
'});' | ||
].join('\n'), | ||
errors: [ { message: 'Unexpected use of duplicate Mocha `before` hook', column: 9, line: 5 } ] | ||
}, | ||
{ | ||
code: [ | ||
'describe(function() {', | ||
' before(function() {});', | ||
' describe(function() {', | ||
' before(function() {});', | ||
' it(function() {});', | ||
' it(function() {});', | ||
' });', | ||
'});' | ||
].join('\n'), | ||
errors: [ { message: 'Unexpected use of duplicate Mocha `before` hook', column: 5, line: 2 } ] | ||
} | ||
] | ||
|
||
}); |