Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove options from the test-title rule #224

Merged
merged 4 commits into from
May 17, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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, because test without title would be rejected by AVA since [v1.0.1](https://github.com/avajs/ava/releases/tag/v1.0.1).
GMartigny marked this conversation as resolved.
Show resolved Hide resolved


## 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