-
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.
Merge pull request #109 from lo1tuma/no-nested-tests
Add no-nested-tests rule
- Loading branch information
Showing
6 changed files
with
200 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,47 @@ | ||
# Disallow tests to be nested within other tests (no-nested-tests) | ||
|
||
Test cases in mocha can be either global or within a suite but they can’t be nested within other tests. Unfortunately there is nothing stopping you from creating a test case within another test case but mocha will simply ignore those tests. | ||
|
||
```js | ||
it('something', function () { | ||
it('should work', function () { | ||
assert(fasle); | ||
}); | ||
}); | ||
``` | ||
Something like this could be easily happen by accident where the outer test case was actually meant to be a suite instead of a test. | ||
This rule reports such nested test cases in order to prevent problems where those nested tests are skipped silently. | ||
|
||
## Rule Details | ||
|
||
This rule looks for all test cases (`it`, `specify` and `test`) or suites (`describe`, `context` and `suite`) which are nested within another test case. | ||
|
||
The following patterns are considered problems: | ||
|
||
```js | ||
it('something', function () { | ||
it('should work', function () {}); | ||
}); | ||
|
||
test('something', function () { | ||
specify('should work', function () {}); | ||
}); | ||
|
||
it('something', function () { | ||
describe('other thing', function () { | ||
// … | ||
}); | ||
}); | ||
|
||
``` | ||
|
||
These patterns would not be considered problems: | ||
|
||
```js | ||
it('should work', function () {}); | ||
it('should work too', function () {}); | ||
|
||
describe('something', function () { | ||
it('should work', function () {}); | ||
}); | ||
``` |
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,38 @@ | ||
'use strict'; | ||
|
||
var astUtils = require('../util/ast'); | ||
|
||
module.exports = function noNestedTests(context) { | ||
var testNestingLevel = 0; | ||
|
||
function report(callExpression, isTestCase) { | ||
var message = isTestCase ? 'Unexpected test nested within another test.' : | ||
'Unexpected suite nested within a test.'; | ||
|
||
context.report({ | ||
message: message, | ||
node: callExpression.callee | ||
}); | ||
} | ||
|
||
return { | ||
CallExpression: function (node) { | ||
var isTestCase = astUtils.isTestCase(node), | ||
isDescribe = astUtils.isDescribe(node); | ||
|
||
if (testNestingLevel > 0 && (isTestCase || isDescribe)) { | ||
report(node, isTestCase); | ||
} | ||
|
||
if (isTestCase) { | ||
testNestingLevel += 1; | ||
} | ||
}, | ||
|
||
'CallExpression:exit': function (node) { | ||
if (astUtils.isTestCase(node)) { | ||
testNestingLevel -= 1; | ||
} | ||
} | ||
}; | ||
}; |
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,109 @@ | ||
'use strict'; | ||
|
||
var RuleTester = require('eslint').RuleTester, | ||
rule = require('../../lib/rules/no-nested-tests'), | ||
ruleTester = new RuleTester(); | ||
|
||
ruleTester.run('no-nested-tests', rule, { | ||
valid: [ | ||
'it()', | ||
'it(); it(); it()', | ||
'describe("", function () { it(); })', | ||
'describe("", function () { describe("", function () { it(); }); it(); })' | ||
], | ||
|
||
invalid: [ | ||
{ | ||
code: 'it("", function () { it() });', | ||
errors: [ { | ||
message: 'Unexpected test nested within another test.', | ||
line: 1, | ||
column: 22 | ||
} ] | ||
}, | ||
{ | ||
code: 'it.only("", function () { it() });', | ||
errors: [ { | ||
message: 'Unexpected test nested within another test.', | ||
line: 1, | ||
column: 27 | ||
} ] | ||
}, | ||
{ | ||
code: 'it.skip("", function () { it() });', | ||
errors: [ { | ||
message: 'Unexpected test nested within another test.', | ||
line: 1, | ||
column: 27 | ||
} ] | ||
}, | ||
{ | ||
code: 'test("", function () { it() });', | ||
errors: [ { | ||
message: 'Unexpected test nested within another test.', | ||
line: 1, | ||
column: 24 | ||
} ] | ||
}, | ||
{ | ||
code: 'specify("", function () { it() });', | ||
errors: [ { | ||
message: 'Unexpected test nested within another test.', | ||
line: 1, | ||
column: 27 | ||
} ] | ||
}, | ||
{ | ||
code: 'it("", function () { describe() });', | ||
errors: [ { | ||
message: 'Unexpected suite nested within a test.', | ||
line: 1, | ||
column: 22 | ||
} ] | ||
}, | ||
{ | ||
code: 'it("", function () { context() });', | ||
errors: [ { | ||
message: 'Unexpected suite nested within a test.', | ||
line: 1, | ||
column: 22 | ||
} ] | ||
}, | ||
{ | ||
code: 'it("", function () { suite() });', | ||
errors: [ { | ||
message: 'Unexpected suite nested within a test.', | ||
line: 1, | ||
column: 22 | ||
} ] | ||
}, | ||
{ | ||
code: 'it("", function () { describe.skip() });', | ||
errors: [ { | ||
message: 'Unexpected suite nested within a test.', | ||
line: 1, | ||
column: 22 | ||
} ] | ||
}, | ||
{ | ||
code: 'it("", function () { describe("", function () { it(); it(); }); });', | ||
errors: [ | ||
{ | ||
message: 'Unexpected suite nested within a test.', | ||
line: 1, | ||
column: 22 | ||
}, | ||
{ | ||
message: 'Unexpected test nested within another test.', | ||
line: 1, | ||
column: 49 | ||
}, | ||
{ | ||
message: 'Unexpected test nested within another test.', | ||
line: 1, | ||
column: 55 | ||
} | ||
] | ||
} | ||
] | ||
}); |