-
Notifications
You must be signed in to change notification settings - Fork 49
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
Changes from 17 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
6ae2bd5
Add the test-title-format rule
GMartigny 57b8e3a
Fix linter warning
GMartigny 25e6c9a
Update test-title-format.md
sindresorhus 4dccddd
Update test-title-format.md
sindresorhus 67e7dfb
Add test-title-format rule to readme
8605a72
Rename the test-title-format option from "regexp" to "format"
9d2c4ce
Add meta.type to test-title-format
03d4c12
Better report message for test-title-format rule
e14fd7a
Early return if missing configuration
4d2b292
Add report on invalid regexp
01921c8
Reformat doc
1814128
Fix lint
4415d84
Let rule crash with invalid regexp
b450d80
Update test-title-format.js
sindresorhus 8d9a702
Unnecessary test
GMartigny ce8bc06
Improve test-title-format documentation with a description
GMartigny 75240da
Update test-title-format.md
sindresorhus 95c6ed9
Follow suggestion on documentation
GMartigny c298a7c
Merge branch 'master' into addTestTitleFormat
GMartigny 45f8487
Merge branch 'master' into addTestTitleFormat
GMartigny a09a28f
Tweak error message
GMartigny 32230f1
Merge branch 'master' into addTestTitleFormat
GMartigny fee3e15
Match readme with documentation title
GMartigny File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,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" | ||
} | ||
] | ||
``` |
This file contains hidden or 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 hidden or 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 hidden or 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,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}` | ||
GMartigny marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}); | ||
} | ||
} | ||
}) | ||
}); | ||
}; | ||
|
||
module.exports = { | ||
create, | ||
meta: { | ||
type: 'suggestion', | ||
docs: { | ||
GMartigny marked this conversation as resolved.
Show resolved
Hide resolved
|
||
url: util.getDocsUrl(__filename) | ||
} | ||
} | ||
}; |
This file contains hidden or 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,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 | ||
} | ||
] | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.