Skip to content

Add test-title-format rule #235

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

Merged
merged 23 commits into from
May 27, 2019
Merged
Show file tree
Hide file tree
Changes from 4 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
67 changes: 67 additions & 0 deletions docs/rules/test-title-format.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# Ensure tests have a correct title format

Tests should have a title matching the format option.


## Fail

```js
/* eslint ava/test-title: ["error", {regexp: "^Should"}] */
import test from 'ava';

test('Not starting with `Should`', t => {
t.pass();
});
```

```js
/* eslint ava/test-title: ["error", {regexp: "\\.$"}] */
import test from 'ava';

test('Doesn\'t end with a dot', t => {
t.pass();
});
```


## Pass

```js
/* eslint ava/test-title: ["error", {regexp: "^Should"}] */
import test from 'ava';

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

test('Should behave as expected', t => {
t.pass();
});
```

```js
/* eslint ava/test-title: ["error", {regexp: "\\.$"}] */
import test from 'ava';

test('End with a dot.', t => {
t.pass();
});
```


## Options

This rule supports the following options:

`regexp`: A regular expression string to match against the test titles. Overrides the default and the configuration found in the `package.json` or `ava.config.js` files.

You can set the options like this:

```json
"ava/test-title-format": [
"error",
{
"regexp": "^Should"
}
]
```
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ module.exports = {
'error',
'if-multiple'
],
'ava/test-title-format': 'off',
'ava/use-t-well': 'error',
'ava/use-t': 'error',
'ava/use-test': 'error',
Expand Down
46 changes: 46 additions & 0 deletions rules/test-title-format.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
'use strict';
const {visitIf} = require('enhance-visitors');
const createAvaRule = require('../create-ava-rule');
const util = require('../util');

const create = context => {
const ava = createAvaRule();
let titleRegExp;
try {
if (context.options[0] && context.options[0].regexp) {
titleRegExp = new RegExp(context.options[0].regexp);
}
} catch (error) {
console.warn(error);
}

return ava.merge({
CallExpression: visitIf([
ava.isInTestFile,
ava.isTestNode,
ava.hasNoHookModifier
])(node => {
const requiredLength = ava.hasTestModifier('todo') ? 1 : 2;
const hasTitle = node.arguments.length >= requiredLength;

if (hasTitle && titleRegExp) {
const title = node.arguments[0];
if (title.type === 'Literal' && !titleRegExp.test(title.value)) {
context.report({
node,
message: 'Title doesn\'t match required format.'
});
}
}
})
});
};

module.exports = {
create,
meta: {
docs: {
url: util.getDocsUrl(__filename)
}
}
};
63 changes: 63 additions & 0 deletions test/test-title-format.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import test from 'ava';
import avaRuleTester from 'eslint-ava-rule-tester';
import rule from '../rules/test-title-format';

const ruleTester = avaRuleTester(test, {
env: {
es6: true
}
});

const errors = [{ruleId: 'test-title-format'}];
const header = 'const test = require(\'ava\');\n';

ruleTester.run('test-title-format', rule, {
valid: [
header + 'test("Foo", t => { t.pass(); });',
{
code: header + 'test("Foo", t => { t.pass(); });',
options: [{regexp: '.'}]
},
{
code: header + 'test("Should pass tests.", t => { t.pass(); });',
options: [{regexp: '^Should .+\\.$'}]
},
{
code: header + 'test.todo("Should pass tests.");',
options: [{regexp: '^Should .+\\.$'}]
},
{
code: header + 'test(t => { t.pass(); });',
options: [{regexp: '^Should'}]
},
{
code: header + 'test("Foo", t => { t.pass(); });',
options: [{regexp: '(]'}] // Invalid regexp
},
{
code: header + 'notTest("Foo", t => { t.pass(); });',
options: [{regexp: '^Should'}]
},
{
code: header + 'test(macro, t => { t.pass(); });',
options: [{regexp: '^Should'}]
},
// Shouldn't be triggered since it's not a test file
{
code: 'test("Test", t => { t.pass(); });',
options: [{regexp: '^Should'}]
}
],
invalid: [
{
code: header + 'test("Test something", t => { t.pass(); });',
options: [{regexp: '^Should'}],
errors
},
{
code: header + 'test.todo("Test something");',
options: [{regexp: '^Should'}],
errors
}
]
});