Skip to content

Commit

Permalink
Use suggestions API for no-only-test and no-skip-test (#325)
Browse files Browse the repository at this point in the history
  • Loading branch information
edbrannin committed Mar 24, 2021
1 parent f175fd0 commit ae34aea
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 50 deletions.
3 changes: 0 additions & 3 deletions docs/rules/no-only-test.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ Translations: [Français](https://github.com/avajs/ava-docs/blob/main/fr_FR/rela

It's easy to run only one test with `test.only()` and then forget about it. It's visible in the results, but still easily missed. Forgetting to remove `.only`, means only this one test in the whole file will run, and if not caught, can let serious bugs slip into your codebase.

This rule is fixable. It will remove the `.only` test modifier.


## Fail

```js
Expand Down
3 changes: 0 additions & 3 deletions docs/rules/no-skip-test.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ Translations: [Français](https://github.com/avajs/ava-docs/blob/main/fr_FR/rela

It's easy to make a test skipped with `test.skip()` and then forget about it. It's visible in the results, but still easily missed.

This rule is fixable. It will remove the `.skip` test modifier.


## Fail

```js
Expand Down
4 changes: 2 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,9 @@ The rules will only activate in test files.
- [no-inline-assertions](docs/rules/no-inline-assertions.md) - Ensure assertions are not called from inline arrow functions. *(fixable)*
- [no-invalid-end](docs/rules/no-invalid-end.md) - Ensure `t.end()` is only called inside `test.cb()`.
- [no-nested-tests](docs/rules/no-nested-tests.md) - Ensure no tests are nested.
- [no-only-test](docs/rules/no-only-test.md) - Ensure no `test.only()` are present. *(fixable)*
- [no-only-test](docs/rules/no-only-test.md) - Ensure no `test.only()` are present.
- [no-skip-assert](docs/rules/no-skip-assert.md) - Ensure no assertions are skipped.
- [no-skip-test](docs/rules/no-skip-test.md) - Ensure no tests are skipped. *(fixable)*
- [no-skip-test](docs/rules/no-skip-test.md) - Ensure no tests are skipped.
- [no-statement-after-end](docs/rules/no-statement-after-end.md) - Ensure `t.end()` is the last statement executed.
- [no-todo-implementation](docs/rules/no-todo-implementation.md) - Ensure `test.todo()` is not given an implementation function.
- [no-todo-test](docs/rules/no-todo-test.md) - Ensure no `test.todo()` is used.
Expand Down
17 changes: 10 additions & 7 deletions rules/no-only-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,16 @@ const create = context => {
context.report({
node: propertyNode,
message: '`test.only()` should not be used.',
fix: fixer => {
return fixer.replaceTextRange.apply(null, util.removeTestModifier({
modifier: 'only',
node,
context
}));
}
suggest: [{
desc: 'Remove the `.only`',
fix: fixer => {
return fixer.replaceTextRange.apply(null, util.removeTestModifier({
modifier: 'only',
node,
context
}));
}
}]
});
}
})
Expand Down
17 changes: 10 additions & 7 deletions rules/no-skip-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,16 @@ const create = context => {
context.report({
node: propertyNode,
message: 'No tests should be skipped.',
fix: fixer => {
return fixer.replaceTextRange.apply(null, util.removeTestModifier({
modifier: 'skip',
node,
context
}));
}
suggest: [{
desc: 'Remove the `.skip`',
fix: fixer => {
return fixer.replaceTextRange.apply(null, util.removeTestModifier({
modifier: 'skip',
node,
context
}));
}
}]
});
}
})
Expand Down
63 changes: 45 additions & 18 deletions test/no-only-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,92 +26,119 @@ ruleTester.run('no-only-test', rule, {
invalid: [
{
code: header + 'test\n\t.only(t => { t.pass(); });',
output: header + 'test\n\t(t => { t.pass(); });',
errors: [{
message,
type: 'Identifier',
line: 3,
column: 3
column: 3,
suggestions: [{
desc: 'Remove the `.only`',
output: header + 'test\n\t(t => { t.pass(); });'
}]
}]
},
{
code: header + 'test\n .only(t => { t.pass(); });',
output: header + 'test\n (t => { t.pass(); });',
errors: [{
message,
type: 'Identifier',
line: 3,
column: 4
column: 4,
suggestions: [{
desc: 'Remove the `.only`',
output: header + 'test\n (t => { t.pass(); });'
}]
}]
},
{
code: header + 'test\t.only(t => { t.pass(); });',
output: header + 'test\t(t => { t.pass(); });',
errors: [{
message,
type: 'Identifier',
line: 2,
column: 7
column: 7,
suggestions: [{
desc: 'Remove the `.only`',
output: header + 'test\t(t => { t.pass(); });'
}]
}]
},
{
code: header + 'test .only(t => { t.pass(); });',
output: header + 'test (t => { t.pass(); });',
errors: [{
message,
type: 'Identifier',
line: 2,
column: 8
column: 8,
suggestions: [{
desc: 'Remove the `.only`',
output: header + 'test (t => { t.pass(); });'
}]
}]
},
{
code: header + 'test.\n\tonly(t => { t.pass(); });',
output: header + 'test\n\t(t => { t.pass(); });',
errors: [{
message,
type: 'Identifier',
line: 3,
column: 2
column: 2,
suggestions: [{
desc: 'Remove the `.only`',
output: header + 'test\n\t(t => { t.pass(); });'
}]
}]
},
{
code: header + 'test.\n only(t => { t.pass(); });',
output: header + 'test\n (t => { t.pass(); });',
errors: [{
message,
type: 'Identifier',
line: 3,
column: 3
column: 3,
suggestions: [{
desc: 'Remove the `.only`',
output: header + 'test\n (t => { t.pass(); });'
}]
}]
},
{
code: header + 'test.only(t => { t.pass(); });',
output: header + 'test(t => { t.pass(); });',
errors: [{
message,
type: 'Identifier',
line: 2,
column: 6
column: 6,
suggestions: [{
desc: 'Remove the `.only`',
output: header + 'test(t => { t.pass(); });'
}]
}]
},
{
code: header + 'test.cb.only(t => { t.pass(); t.end(); });',
output: header + 'test.cb(t => { t.pass(); t.end(); });',
errors: [{
message,
type: 'Identifier',
line: 2,
column: 9
column: 9,
suggestions: [{
desc: 'Remove the `.only`',
output: header + 'test.cb(t => { t.pass(); t.end(); });'
}]
}]
},
{
code: header + 'test.only.cb(t => { t.pass(); t.end(); });',
output: header + 'test.cb(t => { t.pass(); t.end(); });',
errors: [{
message,
type: 'Identifier',
line: 2,
column: 6
column: 6,
suggestions: [{
desc: 'Remove the `.only`',
output: header + 'test.cb(t => { t.pass(); t.end(); });'
}]
}]
}
]
Expand Down
35 changes: 25 additions & 10 deletions test/no-skip-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,52 +24,67 @@ ruleTester.run('no-skip-test', rule, {
invalid: [
{
code: header + 'test.skip(t => { t.pass(); });',
output: header + 'test(t => { t.pass(); });',
errors: [{
message,
type: 'Identifier',
line: 2,
column: 6
column: 6,
suggestions: [{
desc: 'Remove the `.skip`',
output: header + 'test(t => { t.pass(); });'
}]
}]
},
{
code: header + 'test.cb.skip(t => { t.pass(); t.end(); });',
output: header + 'test.cb(t => { t.pass(); t.end(); });',
errors: [{
message,
type: 'Identifier',
line: 2,
column: 9
column: 9,
suggestions: [{
desc: 'Remove the `.skip`',
output: header + 'test.cb(t => { t.pass(); t.end(); });'
}]
}]
},
{
code: header + 'test.skip.cb(t => { t.pass(); t.end(); });',
output: header + 'test.cb(t => { t.pass(); t.end(); });',
errors: [{
message,
type: 'Identifier',
line: 2,
column: 6
column: 6,
suggestions: [{
desc: 'Remove the `.skip`',
output: header + 'test.cb(t => { t.pass(); t.end(); });'
}]
}]
},
{
code: header + 'test.\n\tskip.cb(t => { t.pass(); t.end(); });',
output: header + 'test\n\t.cb(t => { t.pass(); t.end(); });',
errors: [{
message,
type: 'Identifier',
line: 3,
column: 2
column: 2,
suggestions: [{
desc: 'Remove the `.skip`',
output: header + 'test\n\t.cb(t => { t.pass(); t.end(); });'
}]
}]
},
{
code: header + 'test .skip .cb(t => { t.pass(); t.end(); });',
output: header + 'test .cb(t => { t.pass(); t.end(); });',
errors: [{
message,
type: 'Identifier',
line: 2,
column: 8
column: 8,
suggestions: [{
desc: 'Remove the `.skip`',
output: header + 'test .cb(t => { t.pass(); t.end(); });'
}]
}]
}
]
Expand Down

0 comments on commit ae34aea

Please sign in to comment.