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
7 changed files
with
187 additions
and
10 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,48 @@ | ||
# Limit the number of assertions in a test | ||
|
||
Limit the amount of assertions in a test to enforce splitting up large tests into smaller ones. | ||
|
||
Skipped assertions are counted. | ||
|
||
|
||
## Fail | ||
|
||
```js | ||
/*eslint max-asserts: [2, 5]*/ | ||
const test = require('ava'); | ||
|
||
test('tests with too may asserts', (t) => { | ||
const array = getArrayUpTo(6); | ||
|
||
t.is(array.length, 6); | ||
t.is(array[0], 1); | ||
t.is(array[1], 2); | ||
t.is(array[2], 3); | ||
t.is(array[3], 4); | ||
t.is(array[4], 5); | ||
t.is(array[5], 6); | ||
}); | ||
``` | ||
|
||
|
||
## Pass | ||
|
||
```js | ||
const test = require('ava'); | ||
|
||
test('my test name', (t) => { | ||
|
||
test('tests with too may asserts', (t) => { | ||
const array = getArrayUpTo(6); | ||
|
||
t.same(array, [1, 2, 3, 4, 5, 6]); | ||
}); | ||
``` | ||
## Options | ||
The rule takes one option, a number, which is the maximum number of assertions for each test. The default is 5. | ||
You can set the option in configuration like this: | ||
"max-asserts": [2, 5] |
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'; | ||
var util = require('../util'); | ||
|
||
module.exports = function (context) { | ||
var maxAssertions = context.options[0] || 5; | ||
var isTestFile = false; | ||
var assertionCount = 0; | ||
|
||
return { | ||
CallExpression: function (node) { | ||
if (util.isTestFile(node)) { | ||
isTestFile = true; | ||
return; | ||
} | ||
|
||
var callee = node.callee; | ||
var name = callee.type === 'MemberExpression' ? callee.object.name : callee.name; | ||
if (name === 'test') { | ||
assertionCount = 0; | ||
} | ||
}, | ||
|
||
MemberExpression: function (node) { | ||
if (isTestFile) { | ||
// node.parent.type !== 'MemberExpression' --> don't count `t.skip.is(...)` twice | ||
if (node.parent.type !== 'MemberExpression' && util.isAssertion(node)) { | ||
assertionCount++; | ||
if (assertionCount > maxAssertions) { | ||
context.report(node, 'Too many assertions.'); | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
}; |
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,66 @@ | ||
import test from 'ava'; | ||
import {RuleTester} from 'eslint'; | ||
import rule from '../rules/max-asserts'; | ||
|
||
const ruleTester = new RuleTester({ | ||
env: { | ||
es6: true | ||
} | ||
}); | ||
|
||
const errors = [{ruleId: 'max-asserts'}]; | ||
const header = `const test = require('ava');\n`; | ||
function nbAssertions(n) { | ||
return Array.from({length: n}).map(() => 't.is(1, 1);').join('\n'); | ||
} | ||
|
||
test(() => { | ||
ruleTester.run('max-asserts', rule, { | ||
valid: [ | ||
`${header} test(function (t) { ${nbAssertions(3)} });`, | ||
` ${header} | ||
test(function (t) { ${nbAssertions(3)} }); | ||
test(function (t) { ${nbAssertions(3)} }); | ||
`, | ||
`${header} test(function (t) { t.plan(5); ${nbAssertions(5)} });`, | ||
`${header} test(function (t) { t.skip.is(1, 1); ${nbAssertions(4)} });`, | ||
`${header} test.cb(function (t) { ${nbAssertions(5)} t.end(); });`, | ||
{ | ||
code: `${header} test(function (t) { ${nbAssertions(3)} });`, | ||
options: [3] | ||
}, | ||
// shouldn't be triggered since it's not a test file | ||
`test(function (t) { ${nbAssertions(10)} });` | ||
], | ||
invalid: [ | ||
{ | ||
code: `${header} test(function (t) { ${nbAssertions(6)} });`, | ||
errors | ||
}, | ||
{ | ||
code: ` ${header} | ||
test(function (t) { ${nbAssertions(3)} }); | ||
test(function (t) { ${nbAssertions(6)} }); | ||
`, | ||
errors | ||
}, | ||
{ | ||
code: `${header} test(function (t) { t.plan(5); ${nbAssertions(6)} });`, | ||
errors | ||
}, | ||
{ | ||
code: `${header} test(function (t) { t.skip.is(1, 1); ${nbAssertions(5)} });`, | ||
errors | ||
}, | ||
{ | ||
code: `${header} test.cb(function (t) { ${nbAssertions(6)} t.end(); });`, | ||
errors | ||
}, | ||
{ | ||
code: `${header} test(function (t) { ${nbAssertions(4)} });`, | ||
options: [3], | ||
errors | ||
} | ||
] | ||
}); | ||
}); |
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