-
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.
- Loading branch information
Showing
3 changed files
with
117 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,35 @@ | ||
# Prevent specifying error type in `t.notThrows()` | ||
|
||
AVA will fail if error type is specified with `t.notThrows()`. | ||
|
||
|
||
## Fail | ||
|
||
```js | ||
const test = require('ava'); | ||
|
||
test('some test', t => { | ||
t.notThrows(() => { | ||
t.pass(); | ||
}, TypeError); | ||
}); | ||
``` | ||
|
||
|
||
## Pass | ||
|
||
```js | ||
const test = require('ava'); | ||
|
||
test('some test', t => { | ||
t.notThrows(() => { | ||
t.pass(); | ||
}); | ||
}); | ||
|
||
test('some test', t => { | ||
t.throws(() => { | ||
t.pass(); | ||
}, TypeError); | ||
}); | ||
``` |
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,50 @@ | ||
'use strict'; | ||
const {visitIf} = require('enhance-visitors'); | ||
const util = require('../util'); | ||
const createAvaRule = require('../create-ava-rule'); | ||
|
||
const nameRegexp = /^(?:[A-Z][a-z0-9]*)*Error$/; | ||
|
||
const create = context => { | ||
const ava = createAvaRule(); | ||
|
||
return ava.merge({ | ||
CallExpression: visitIf([ | ||
ava.isInTestFile, | ||
ava.isInTestNode | ||
])(node => { | ||
const functionArgIndex = node.arguments.length - 1; | ||
let functionArgName; | ||
|
||
if (typeof node.callee.property === 'undefined') { | ||
return; | ||
} | ||
|
||
const calleeProperty = node.callee.property.name; | ||
|
||
if (functionArgIndex === 1) { | ||
functionArgName = node.arguments[1].name; | ||
} else { | ||
return; | ||
} | ||
|
||
if (calleeProperty === 'notThrows') { | ||
if (nameRegexp.test(functionArgName)) { | ||
context.report({ | ||
node, | ||
message: 'Do not specify Error in t.notThrows()' | ||
}); | ||
} | ||
} | ||
}) | ||
}); | ||
}; | ||
|
||
module.exports = { | ||
create, | ||
meta: { | ||
docs: { | ||
url: util.getDocsUrl(__filename) | ||
} | ||
} | ||
}; |
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,32 @@ | ||
import test from 'ava'; | ||
import avaRuleTester from 'eslint-ava-rule-tester'; | ||
import rule from '../rules/prevent-errortype'; | ||
|
||
const ruleTester = avaRuleTester(test, { | ||
env: { | ||
es6: true | ||
} | ||
}); | ||
|
||
const errors = [{ruleId: 'prevent-errortype'}]; | ||
|
||
const header = 'const test = require(\'ava\');\n'; | ||
|
||
ruleTester.run('prevent-errortype', rule, { | ||
valid: [ | ||
header + 'test(\'some test\',t => {t.notThrows(() => {t.pass();});});', | ||
header + 'test(t => {t.notThrows(() => {t.pass();});});', | ||
header + 'test(t => {t.throws(() => {t.pass();}, TypeError);});', | ||
header + 'test(t => {t.end(); });' | ||
], | ||
invalid: [ | ||
{ | ||
code: header + 'test(t => {t.notThrows(() => {t.pass();}, TypeError);});', | ||
errors | ||
}, | ||
{ | ||
code: header + 'test(\'some test\',t => {t.notThrows(() => {t.pass();}, TypeError);});', | ||
errors | ||
} | ||
] | ||
}); |