-
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 4 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,67 @@ | ||
# Ensure tests have a correct title format | ||
|
||
Tests should have a title matching the format option. | ||
GMartigny marked this conversation as resolved.
Show resolved
Hide resolved
GMartigny marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
## 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. | ||
GMartigny marked this conversation as resolved.
Show resolved
Hide resolved
GMartigny marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
You can set the options like this: | ||
|
||
```json | ||
"ava/test-title-format": [ | ||
"error", | ||
{ | ||
"regexp": "^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
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); | ||
GMartigny marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
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) { | ||
GMartigny marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const title = node.arguments[0]; | ||
if (title.type === 'Literal' && !titleRegExp.test(title.value)) { | ||
context.report({ | ||
node, | ||
message: 'Title doesn\'t match required format.' | ||
GMartigny marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}); | ||
} | ||
} | ||
}) | ||
}); | ||
}; | ||
|
||
module.exports = { | ||
create, | ||
meta: { | ||
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,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 | ||
} | ||
] | ||
}); |
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.