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 17 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
69 changes: 69 additions & 0 deletions docs/rules/test-title-format.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# Enforce test titles have a certain format

This rule is useful when you want to make sure all test titles match a common pattern to increase readability when tests fail.

For example, titles like `'Should throw when invalid.'`, `'Should fail when called.'` or `'Should pass when using any number.'` could be enforced with the following pattern `'^Should (pass|fail|throw) when [\\w ]+\\.$'` (Note the escaped `\`).


## Fail

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

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

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

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


## Pass

```js
/* eslint ava/test-title: ["error", {format: "^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", {format: "\\.$"}] */
import test from 'ava';

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


## Options

This rule supports the following options:

`format`: A regular expression string to match against the test titles.

You can set the options like this:

```json
"ava/test-title-format": [
"error",
{
"format": "^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
2 changes: 2 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ Configure it in `package.json`.
"error",
"if-multiple"
],
"ava/test-title-format": "off",
"ava/use-t-well": "error",
"ava/use-t": "error",
"ava/use-test": "error",
Expand Down Expand Up @@ -95,6 +96,7 @@ The rules will only activate in test files.
- [prefer-power-assert](docs/rules/prefer-power-assert.md) - Allow only use of the asserts that have no [power-assert](https://github.com/power-assert-js/power-assert) alternative.
- [test-ended](docs/rules/test-ended.md) - Ensure callback tests are explicitly ended.
- [test-title](docs/rules/test-title.md) - Ensure tests have a title.
- [test-title-format](docs/rules/test-title-format.md) - Ensure tests have a correct title format.
- [use-t-well](docs/rules/use-t-well.md) - Prevent the incorrect use of `t`.
- [use-t](docs/rules/use-t.md) - Ensure test functions use `t` as their parameter.
- [use-test](docs/rules/use-test.md) - Ensure that AVA is imported with `test` as the variable name.
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;
if (context.options[0] && context.options[0].format) {
titleRegExp = new RegExp(context.options[0].format);
} else {
return {};
}

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) {
const title = node.arguments[0];
if (title.type === 'Literal' && !titleRegExp.test(title.value)) {
context.report({
node,
message: `The test title doesn't match the required format: ${titleRegExp}`
});
}
}
})
});
};

module.exports = {
create,
meta: {
type: 'suggestion',
docs: {
url: util.getDocsUrl(__filename)
}
}
};
59 changes: 59 additions & 0 deletions test/test-title-format.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
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: [{format: '.'}]
},
{
code: header + 'test("Should pass tests.", t => { t.pass(); });',
options: [{format: '^Should .+\\.$'}]
},
{
code: header + 'test.todo("Should pass tests.");',
options: [{format: '^Should .+\\.$'}]
},
{
code: header + 'test(t => { t.pass(); });',
options: [{format: '^Should'}]
},
{
code: header + 'notTest("Foo", t => { t.pass(); });',
options: [{format: '^Should'}]
},
{
code: header + 'test(macro, t => { t.pass(); });',
options: [{format: '^Should'}]
},
// Shouldn't be triggered since it's not a test file
{
code: 'test("Test", t => { t.pass(); });',
options: [{format: '^Should'}]
}
],
invalid: [
{
code: header + 'test("Test something", t => { t.pass(); });',
options: [{format: '^Should'}],
errors
},
{
code: header + 'test.todo("Test something");',
options: [{format: '^Should'}],
errors
}
]
});