forked from avajs/eslint-plugin-ava
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes: avajs#156
- Loading branch information
Showing
5 changed files
with
137 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,60 @@ | ||
# Prefer using `t.throws()` over try/catch | ||
|
||
This rule will enforce the use of `t.throws()` when possible. | ||
|
||
## Fail | ||
|
||
```js | ||
const test = require('ava'); | ||
|
||
test('some test', async t => { | ||
try { | ||
await Promise.reject('error'); | ||
t.fail(); | ||
} catch (error) { | ||
t.is(error, 'error'); | ||
} | ||
}); | ||
``` | ||
|
||
## Pass | ||
|
||
```js | ||
const test = require('ava'); | ||
|
||
test('some test', async t => { | ||
const error = await t.throws(Promise.reject('error')); | ||
t.is(error, 'error'); | ||
}); | ||
``` | ||
|
||
```js | ||
const test = require('ava'); | ||
|
||
test('some test', async t => { | ||
try { | ||
const result = await promiseThatMaybeFails(); | ||
// This passes because using a try-catch can be intentional, for example, to test both the result and the error: | ||
// t.is(result, 'good result'); | ||
} catch (error) { | ||
t.is(error, 'good error message'); | ||
} | ||
}); | ||
``` | ||
|
||
```js | ||
const test = require('ava'); | ||
|
||
test('some test', async t => { | ||
try { | ||
// This is also because handling multiple rejection promises with try/catch may be easier or may be a deliberate choice. | ||
await promiseThatMaybeFails(); | ||
await anotherPromise; | ||
await timeout(100, 'good error message'); | ||
t.fail(); | ||
} catch (error) { | ||
t.is(error, 'good error message'); | ||
} | ||
}); | ||
``` | ||
|
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,47 @@ | ||
'use strict'; | ||
|
||
const {visitIf} = require('enhance-visitors'); | ||
const createAvaRule = require('../create-ava-rule'); | ||
const util = require('../util'); | ||
|
||
const create = context => { | ||
const ava = createAvaRule(); | ||
|
||
return ava.merge({ | ||
TryStatement: visitIf([ | ||
ava.isInTestFile, | ||
ava.isInTestNode, | ||
])(node => { | ||
const expressions = node.block.body.filter(node => node.type === 'ExpressionStatement'); | ||
if (expressions.length < 2) { | ||
return; | ||
} | ||
|
||
const lastExpression = expressions[expressions.length - 1].expression; | ||
if (lastExpression.type !== 'CallExpression' | ||
|| lastExpression.callee.object.name !== 't' | ||
|| lastExpression.callee.property.name !== 'fail' | ||
) { | ||
return; | ||
} | ||
|
||
if (expressions.filter(node => node.expression.type === 'AwaitExpression').length === 1) { | ||
context.report({ | ||
node, | ||
message: 'Prefer using the `t.throws()` assertion.', | ||
}); | ||
} | ||
}), | ||
}); | ||
}; | ||
|
||
module.exports = { | ||
create, | ||
meta: { | ||
type: 'suggestion', | ||
docs: { | ||
url: util.getDocsUrl(__filename), | ||
}, | ||
schema: [], | ||
}, | ||
}; |
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,27 @@ | ||
'use strict'; | ||
|
||
const test = require('ava'); | ||
const avaRuleTester = require('eslint-ava-rule-tester'); | ||
const rule = require('../rules/prefer-t-throws'); | ||
|
||
const ruleTester = avaRuleTester(test, { | ||
parserOptions: { | ||
ecmaVersion: 'latest', | ||
}, | ||
}); | ||
|
||
const header = 'const test = require(\'ava\');\n'; | ||
|
||
ruleTester.run('prefer-t-throws', rule, { | ||
valid: [ | ||
`${header}test(async t => { const error = await t.throws(promise); t.is(error, 'error'); });`, | ||
`${header}test(async t => { try { await promise; } catch (error) { t.is(error, 'error'); } });`, | ||
`${header}test(async t => { try { await promise; await anotherPromise; t.fail(); } catch (error) { t.is(error, 'error'); } });`, | ||
], | ||
invalid: [ | ||
{ | ||
code: `${header}test(async t => { try { await Promise.reject('error'); t.fail(); } catch (error) { t.is(error, 'error'); } });`, | ||
errors: [{message: 'Prefer using the `t.throws()` assertion.'}], | ||
}, | ||
], | ||
}); |