From ae34aea85345dbe99fd612e7b8751c5d5f6098c2 Mon Sep 17 00:00:00 2001 From: Ed Brannin Date: Wed, 24 Mar 2021 09:31:58 -0400 Subject: [PATCH] Use suggestions API for no-only-test and no-skip-test (#325) --- docs/rules/no-only-test.md | 3 -- docs/rules/no-skip-test.md | 3 -- readme.md | 4 +-- rules/no-only-test.js | 17 +++++----- rules/no-skip-test.js | 17 +++++----- test/no-only-test.js | 63 +++++++++++++++++++++++++++----------- test/no-skip-test.js | 35 +++++++++++++++------ 7 files changed, 92 insertions(+), 50 deletions(-) diff --git a/docs/rules/no-only-test.md b/docs/rules/no-only-test.md index c026b137..92201c3b 100644 --- a/docs/rules/no-only-test.md +++ b/docs/rules/no-only-test.md @@ -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 diff --git a/docs/rules/no-skip-test.md b/docs/rules/no-skip-test.md index 57c41ee4..a7707526 100644 --- a/docs/rules/no-skip-test.md +++ b/docs/rules/no-skip-test.md @@ -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 diff --git a/readme.md b/readme.md index 97aeb7cd..cc80a4eb 100644 --- a/readme.md +++ b/readme.md @@ -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. diff --git a/rules/no-only-test.js b/rules/no-only-test.js index 57edd26d..f0a0dc67 100644 --- a/rules/no-only-test.js +++ b/rules/no-only-test.js @@ -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 + })); + } + }] }); } }) diff --git a/rules/no-skip-test.js b/rules/no-skip-test.js index b32e0a7b..ae9379bf 100644 --- a/rules/no-skip-test.js +++ b/rules/no-skip-test.js @@ -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 + })); + } + }] }); } }) diff --git a/test/no-only-test.js b/test/no-only-test.js index 2ec5c5b8..cecf9d38 100644 --- a/test/no-only-test.js +++ b/test/no-only-test.js @@ -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(); });' + }] }] } ] diff --git a/test/no-skip-test.js b/test/no-skip-test.js index f3de151c..cd6e60a3 100644 --- a/test/no-skip-test.js +++ b/test/no-skip-test.js @@ -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(); });' + }] }] } ]