-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
a4e4ffb
commit a0908d8
Showing
6 changed files
with
123 additions
and
5 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,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); | ||
}); | ||
``` |
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
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,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: {} | ||
}; |
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,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] | ||
} | ||
] | ||
}); |