Skip to content

Commit

Permalink
Add no-nested-tests rule (fixes #131) (#135)
Browse files Browse the repository at this point in the history
  • Loading branch information
jfmengels authored and sindresorhus committed Aug 7, 2016
1 parent a4e4ffb commit a0908d8
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 5 deletions.
8 changes: 3 additions & 5 deletions create-ava-rule.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,9 @@ module.exports = () => {
}
},
CallExpression: node => {
if (!currentTestNode) {
if (isTestFunctionCall(node.callee)) {
// entering test function
currentTestNode = node;
}
if (isTestFunctionCall(node.callee)) {
// entering test function
currentTestNode = node;
}
},
'CallExpression:exit': node => {
Expand Down
38 changes: 38 additions & 0 deletions docs/rules/no-nested-tests.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Ensure no tests are nested

Translations: [Français](https://github.com/avajs/ava-docs/blob/master/fr_FR/related/eslint-plugin-ava/docs/rules/no-nested-tests.md)

In AVA, you cannot nest tests, for example, create tests inside of other tests. Doing so will lead to odd behavior.


## Fail

```js
import test from 'ava';

test('foo', t => {
const result = foo();
t.true(result.foo);

test('bar', t => {
t.true(result.bar);
});
});
```


## Pass

```js
import test from 'ava';

test('foo', t => {
const result = foo();
t.true(result.foo);
});

test('bar', t => {
const result = foo();
t.true(result.bar);
});
```
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ module.exports = {
'ava/no-identical-title': 'error',
'ava/no-ignored-test-files': 'error',
'ava/no-invalid-end': 'error',
'ava/no-nested-tests': 'error',
'ava/no-only-test': 'error',
'ava/no-skip-assert': 'error',
'ava/no-skip-test': 'error',
Expand Down
2 changes: 2 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ Configure it in `package.json`.
"ava/no-identical-title": "error",
"ava/no-ignored-test-files": "error",
"ava/no-invalid-end": "error",
"ava/no-nested-tests": "error",
"ava/no-only-test": "error",
"ava/no-skip-assert": "error",
"ava/no-skip-test": "error",
Expand Down Expand Up @@ -71,6 +72,7 @@ The rules will only activate in test files.
- [no-ignored-test-files](docs/rules/no-ignored-test-files.md) - Ensure no tests are written in ignored files.
- [no-invalid-end](docs/rules/no-invalid-end.md) - Ensure `t.end()` is only called inside `test.cb()`.
- [no-only-test](docs/rules/no-only-test.md) - Ensure no `test.only()` are present.
- [no-nested-tests](docs/rules/no-nested-tests.md) - Ensure no tests are nested.
- [no-skip-assert](docs/rules/no-skip-assert.md) - Ensure no assertions are skipped.
- [no-skip-test](docs/rules/no-skip-test.md) - Ensure no tests are skipped.
- [no-statement-after-end](docs/rules/no-statement-after-end.md) - Ensure `t.end()` is the last statement executed.
Expand Down
35 changes: 35 additions & 0 deletions rules/no-nested-tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
'use strict';
const visitIf = require('enhance-visitors').visitIf;
const createAvaRule = require('../create-ava-rule');

const create = context => {
const ava = createAvaRule();
let nestedCount = 0;

return ava.merge({
'CallExpression': visitIf([
ava.isInTestFile,
ava.isTestNode
])(node => {
nestedCount++;
if (nestedCount >= 2) {
context.report({
node,
message: 'Tests should not be nested'
});
}
}),

'CallExpression:exit': visitIf([
ava.isInTestFile,
ava.isTestNode
])(() => {
nestedCount--;
})
});
};

module.exports = {
create,
meta: {}
};
44 changes: 44 additions & 0 deletions test/no-nested-tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import test from 'ava';
import avaRuleTester from 'eslint-ava-rule-tester';
import rule from '../rules/no-nested-tests';

const ruleTester = avaRuleTester(test, {
env: {
es6: true
}
});

const header = `const test = require('ava');\n`;
const error = {
ruleId: 'no-nested-tests',
message: 'Tests should not be nested'
};

ruleTester.run('no-nested-tests', rule, {
valid: [
header + 'test(t => {});',
header + 'test("title", t => {});',
header + 'test(t => {}); test(t => {});',
header + 'test("title", t => {}); test("title", t => {});',
header + 'test.skip(t => {});',
header + 'test.skip(t => {}); test.skip(t => {});',
header + 'test.only(t => {});',
header + 'test.only(t => {}); test.only(t => {});',
// shouldn't be triggered since it's not a test file
'test(t => { test(t => {}); });'
],
invalid: [
{
code: header + 'test("2", t => { test(t => {}); });',
errors: [error]
},
{
code: header + 'test(t => { test(t => {}); test(t => {}); });',
errors: [error, error]
},
{
code: header + 'test(t => { test(t => { test(t => {}); }); });',
errors: [error, error]
}
]
});

0 comments on commit a0908d8

Please sign in to comment.