Skip to content

Commit

Permalink
Remove options from the test-title rule (#224)
Browse files Browse the repository at this point in the history
  • Loading branch information
GMartigny authored and sindresorhus committed May 17, 2019
1 parent 87aeb8e commit 1218892
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 125 deletions.
36 changes: 1 addition & 35 deletions docs/rules/test-title.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,12 @@

Translations: [Français](https://github.com/avajs/ava-docs/blob/master/fr_FR/related/eslint-plugin-ava/docs/rules/test-title.md)

Tests should have a title.
Tests should have a title. AVA [v1.0.1](https://github.com/avajs/ava/releases/tag/v1.0.1) and later enforces this at runtime.


## Fail

```js
/*eslint ava/test-title: ["error", "if-multiple"]*/
import test from 'ava';

test(t => {
t.pass();
});

test(t => {
t.pass();
});

/*eslint ava/test-title: ["error", "always"]*/
import test from 'ava';

test(t => {
Expand All @@ -31,31 +19,9 @@ test(t => {
## Pass

```js
/*eslint ava/test-title: ["error", "if-multiple"]*/
import test from 'ava';

test(t => {
t.pass();
});

test('foo', t => {
t.pass();
});

/*eslint ava/test-title: ["error", "always"]*/
import test from 'ava';

test('foo', t => {
t.pass();
});
```

## Options

The rule takes one option, a string, which could be either `"always"` or `"if-multiple"`. The default is `"if-multiple"`. If the option is set to `"if-multiple"`, the rule will only trigger if there are multiple tests in a file.

You can set the option in configuration like this:

```js
"ava/test-title": ["error", "always"]
```
26 changes: 5 additions & 21 deletions rules/test-title.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,47 +5,31 @@ const util = require('../util');

const create = context => {
const ava = createAvaRule();
const ifMultiple = context.options[0] !== 'always';
let testCount = 0;

return ava.merge({
CallExpression: visitIf([
ava.isInTestFile,
ava.isTestNode,
ava.hasNoHookModifier
])(node => {
testCount++;
const firstArgumentIsFunction = node.arguments.length < 1 || util.isFunctionExpression(node.arguments[0]);

const requiredLength = ava.hasTestModifier('todo') ? 1 : 2;
const hasNoTitle = node.arguments.length < requiredLength;
const isOverThreshold = !ifMultiple || testCount > 1;

if (hasNoTitle && isOverThreshold) {
if (firstArgumentIsFunction) {
context.report({
node,
message: 'Test should have a title.'
});
}
}),
'Program:exit': () => {
testCount = 0;
}
})
});
};

const schema = [{
enum: [
'always',
'if-multiple'
]
}];

module.exports = {
create,
meta: {
type: 'problem',
docs: {
url: util.getDocsUrl(__filename)
},
schema
}
}
};
88 changes: 19 additions & 69 deletions test/test-title.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,99 +13,49 @@ const header = 'const test = require(\'ava\');\n';

ruleTester.run('test-title', rule, {
valid: [
// Default options should be `['if-multiple']`
header + 'test(t => { t.pass(); t.end(); });',
{
code: header + 'test("my test name", t => { t.pass(); t.end(); });',
options: ['always']
},
{
code: header + 'test(`my test name`, t => { t.pass(); t.end(); });',
options: ['always']
},
{
code: header + 'test(\'my test name\', t => { t.pass(); t.end(); });',
options: ['always']
},
{
code: header + 'test.cb("my test name", t => { t.pass(); t.end(); });',
options: ['always']
},
{
code: header + 'test.todo("my test name");',
options: ['always']
},
{
code: header + 'test.before(t => {});',
options: ['always']
},
{
code: header + 'test.after(t => {});',
options: ['always']
},
{
code: header + 'test.beforeEach(t => {});',
options: ['always']
},
{
code: header + 'test.afterEach(t => {});',
options: ['always']
},
{
code: header + 'test.cb.before(t => {}); test.before.cb(t => {});',
options: ['always']
},
{
code: header + 'test(t => { t.pass(); t.end(); });',
options: ['if-multiple']
},
{
code: header + 'notTest(t => { t.pass(); t.end(); });',
options: ['always']
},
{
code: header + 'test(macroFn, arg1, arg2);',
options: ['always']
},
header + 'test("my test name", t => { t.pass(); t.end(); });',
header + 'test(`my test name`, t => { t.pass(); t.end(); });',
header + 'test(\'my test name\', t => { t.pass(); t.end(); });',
header + 'test.cb("my test name", t => { t.pass(); t.end(); });',
header + 'test.todo("my test name");',
header + 'test.before(t => {});',
header + 'test.after(t => {});',
header + 'test.beforeEach(t => {});',
header + 'test.afterEach(t => {});',
header + 'test.cb.before(t => {}); test.before.cb(t => {});',
header + 'notTest(t => { t.pass(); t.end(); });',
header + 'test([], arg1, arg2);',
header + 'test({}, arg1, arg2);',
// Shouldn't be triggered since it's not a test file
{
code: 'test(t => {});',
options: ['always']
}
'test(t => {});'
],
invalid: [
{
code: header + 'test(t => {}); test(t => {});',
code: header + 'test(t => {});',
errors
},
{
code: header + 'test(t => {}, "my test name");',
errors
},
{
code: header + 'test(t => { t.pass(); t.end(); });',
options: ['always'],
errors
},
{
code: header + 'test.cb(t => { t.pass(); t.end(); });',
options: ['always'],
errors
},
{
code: header + 'test.cb.skip(t => { t.pass(); t.end(); });',
options: ['always'],
errors
},
{
code: header + 'test(t => { t.pass(); t.end(); });',
options: ['always'],
errors
},
{
code: header + 'test.todo();',
options: ['always'],
errors
},
{
code: header + 'test(t => {}); test(t => {});',
options: ['if-multiple'],
errors
}
]
Expand Down

0 comments on commit 1218892

Please sign in to comment.