-
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
7cfdf34
commit a443d7a
Showing
6 changed files
with
132 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# Ensure that async tests use `await` | ||
|
||
Translations: [Français](https://github.com/avajs/ava-docs/blob/master/fr_FR/related/eslint-plugin-ava/docs/rules/no-async-fn-without-await.md) | ||
|
||
AVA comes with built-in support for [async functions](http://www.2ality.com/2016/02/async-functions.html) (async/await). This allows you to write shorter and clearer tests. | ||
|
||
Declaring an async test without using the `await` keyword means that either a Promise is not awaited on as intended, or that the function could have been declared as a regular function, which is confusing and slower. | ||
|
||
This rule will report an error when it finds an async test which does not use the `await` keyword. | ||
|
||
|
||
## Fail | ||
|
||
```js | ||
import test from 'ava'; | ||
|
||
test(async t => { | ||
return foo().then(res => { | ||
t.is(res, 1); | ||
}); | ||
}); | ||
``` | ||
|
||
|
||
## Pass | ||
|
||
```js | ||
import test from 'ava'; | ||
|
||
test(async t => { | ||
t.is(await foo(), 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
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,42 @@ | ||
'use strict'; | ||
const visitIf = require('enhance-visitors').visitIf; | ||
const createAvaRule = require('../create-ava-rule'); | ||
|
||
const create = context => { | ||
const ava = createAvaRule(); | ||
let testIsAsync = false; | ||
let testUsed = false; | ||
|
||
return ava.merge({ | ||
'CallExpression': visitIf([ | ||
ava.isInTestFile, | ||
ava.isTestNode | ||
])(node => { | ||
const implementationFn = node.arguments[0]; | ||
testIsAsync = implementationFn && implementationFn.async; | ||
}), | ||
'YieldExpression': () => { | ||
if (testIsAsync) { | ||
testUsed = true; | ||
} | ||
}, | ||
'CallExpression:exit': visitIf([ | ||
ava.isInTestFile, | ||
ava.isTestNode | ||
])(node => { | ||
if (testIsAsync && !testUsed) { | ||
context.report({ | ||
node, | ||
message: 'Function was declared as `async` but doesn\'t use `await`' | ||
}); | ||
} | ||
testIsAsync = false; | ||
testUsed = false; | ||
}) | ||
}); | ||
}; | ||
|
||
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,53 @@ | ||
import test from 'ava'; | ||
import avaRuleTester from 'eslint-ava-rule-tester'; | ||
import rule from '../rules/no-async-fn-without-await'; | ||
|
||
const ruleTester = avaRuleTester(test, { | ||
parser: 'babel-eslint', | ||
env: { | ||
es6: true | ||
} | ||
}); | ||
|
||
const error = { | ||
ruleId: 'no-async-fn-without-await', | ||
message: 'Function was declared as `async` but doesn\'t use `await`' | ||
}; | ||
const header = `const test = require('ava');\n`; | ||
|
||
ruleTester.run('no-async-fn-without-await', rule, { | ||
valid: [ | ||
`${header} test(fn);`, | ||
`${header} test(t => {});`, | ||
`${header} test(function(t) {});`, | ||
`${header} test(async t => { await foo(); });`, | ||
`${header} test(async t => { t.is(await foo(), 1); });`, | ||
`${header} test(async function(t) { await foo(); });`, | ||
`${header} test(async t => { if (bar) { await foo(); } });`, | ||
`${header} test(async t => { if (bar) {} else { await foo(); } });`, | ||
// shouldn't be triggered since it's not a test file | ||
'test(async t => {});' | ||
], | ||
invalid: [ | ||
{ | ||
code: `${header} test(async t => {});`, | ||
errors: [error] | ||
}, | ||
{ | ||
code: `${header} test(async function(t) {});`, | ||
errors: [error] | ||
}, | ||
{ | ||
code: `${header} test(async t => {}); test(async t => {});`, | ||
errors: [error, error] | ||
}, | ||
{ | ||
code: `${header} test(async t => {}); test(async t => { await foo(); });`, | ||
errors: [error] | ||
}, | ||
{ | ||
code: `${header} test(async t => { await foo(); }); test(async t => {});`, | ||
errors: [error] | ||
} | ||
] | ||
}); |