From 016880debbfcceb75df36269a4571346360ba22f Mon Sep 17 00:00:00 2001 From: kangax Date: Wed, 15 May 2019 21:32:16 -0400 Subject: [PATCH 01/13] feat(rules): add no-commented-out rule --- README.md | 2 + docs/rules/no-commented-tests.md | 53 ++++++ src/__tests__/rules.test.js | 2 +- src/index.js | 1 + .../__tests__/no-commented-tests.test.js | 155 ++++++++++++++++++ src/rules/no-commented-tests.js | 37 +++++ 6 files changed, 249 insertions(+), 1 deletion(-) create mode 100644 docs/rules/no-commented-tests.md create mode 100644 src/rules/__tests__/no-commented-tests.test.js create mode 100644 src/rules/no-commented-tests.js diff --git a/README.md b/README.md index 33ccde9b3..d0b870387 100644 --- a/README.md +++ b/README.md @@ -96,6 +96,7 @@ for more information about extending configuration files. | [lowercase-name][] | Disallow capitalized test names | | ![fixable-green][] | | [no-alias-methods][] | Disallow alias methods | ![recommended][] | ![fixable-green][] | | [no-disabled-tests][] | Disallow disabled tests | ![recommended][] | | +| [no-commented-tests][] | Disallow commented tests | | | s | | [no-empty-title][] | Disallow empty titles | | | | [no-focused-tests][] | Disallow focused tests | ![recommended][] | | | [no-hooks][] | Disallow setup and teardown hooks | | | @@ -142,6 +143,7 @@ https://github.com/dangreenisrael/eslint-plugin-jest-formatting [lowercase-name]: docs/rules/lowercase-name.md [no-alias-methods]: docs/rules/no-alias-methods.md [no-disabled-tests]: docs/rules/no-disabled-tests.md +[no-commented-tests]: docs/rules/no-commented-tests.md [no-empty-title]: docs/rules/no-empty-title.md [no-focused-tests]: docs/rules/no-focused-tests.md [no-hooks]: docs/rules/no-hooks.md diff --git a/docs/rules/no-commented-tests.md b/docs/rules/no-commented-tests.md new file mode 100644 index 000000000..3d78aa7cb --- /dev/null +++ b/docs/rules/no-commented-tests.md @@ -0,0 +1,53 @@ +# Disallow commented tests (no-commented-tests) + +This rule raises a warning about commented tests. + +## Rule Details + + diff --git a/src/__tests__/rules.test.js b/src/__tests__/rules.test.js index 687352c36..defda6016 100644 --- a/src/__tests__/rules.test.js +++ b/src/__tests__/rules.test.js @@ -5,7 +5,7 @@ const path = require('path'); const { rules } = require('../'); const ruleNames = Object.keys(rules); -const numberOfRules = 31; +const numberOfRules = 32; describe('rules', () => { it('should have a corresponding doc for each rule', () => { diff --git a/src/index.js b/src/index.js index 400651b77..6ac704f22 100644 --- a/src/index.js +++ b/src/index.js @@ -38,6 +38,7 @@ module.exports = { rules: { 'jest/no-alias-methods': 'warn', 'jest/no-disabled-tests': 'warn', + 'jest/no-commented-tests': 'warn', 'jest/no-focused-tests': 'error', 'jest/no-identical-title': 'error', 'jest/no-jest-import': 'error', diff --git a/src/rules/__tests__/no-commented-tests.test.js b/src/rules/__tests__/no-commented-tests.test.js new file mode 100644 index 000000000..aad8b256f --- /dev/null +++ b/src/rules/__tests__/no-commented-tests.test.js @@ -0,0 +1,155 @@ +'use strict'; + +const { RuleTester } = require('eslint'); +const rule = require('../no-commented-tests'); + +const ruleTester = new RuleTester({ + parserOptions: { + sourceType: 'module', + }, +}); + +ruleTester.run('no-commented-tests', rule, { + valid: [ + '// foo("bar", function () {})', + 'describe("foo", function () {})', + 'it("foo", function () {})', + 'describe.only("foo", function () {})', + 'it.only("foo", function () {})', + 'test("foo", function () {})', + 'test.only("foo", function () {})', + 'var appliedSkip = describe.skip; appliedSkip.apply(describe)', + 'var calledSkip = it.skip; calledSkip.call(it)', + '({ f: function () {} }).f()', + '(a || b).f()', + 'itHappensToStartWithIt()', + 'testSomething()', + [ + 'import { pending } from "actions"', + '', + 'test("foo", () => {', + ' expect(pending()).toEqual({})', + '})', + ].join('\n'), + [ + 'const { pending } = require("actions")', + '', + 'test("foo", () => {', + ' expect(pending()).toEqual({})', + '})', + ].join('\n'), + [ + 'test("foo", () => {', + ' const pending = getPending()', + ' expect(pending()).toEqual({})', + '})', + ].join('\n'), + [ + 'test("foo", () => {', + ' expect(pending()).toEqual({})', + '})', + '', + 'function pending() {', + ' return {}', + '}', + ].join('\n'), + ], + + invalid: [ + { + code: '// describe("foo", function () {})', + errors: [ + { message: 'Some tests seem to be commented', column: 1, line: 1 }, + ], + }, + { + code: '// describe["skip"]("foo", function () {})', + errors: [ + { message: 'Some tests seem to be commented', column: 1, line: 1 }, + ], + }, + { + code: '// describe[\'skip\']("foo", function () {})', + errors: [ + { message: 'Some tests seem to be commented', column: 1, line: 1 }, + ], + }, + { + code: '// it.skip("foo", function () {})', + errors: [ + { message: 'Some tests seem to be commented', column: 1, line: 1 }, + ], + }, + { + code: '// it["skip"]("foo", function () {})', + errors: [ + { message: 'Some tests seem to be commented', column: 1, line: 1 }, + ], + }, + { + code: '// test.skip("foo", function () {})', + errors: [ + { message: 'Some tests seem to be commented', column: 1, line: 1 }, + ], + }, + { + code: '// test["skip"]("foo", function () {})', + errors: [ + { message: 'Some tests seem to be commented', column: 1, line: 1 }, + ], + }, + { + code: '// xdescribe("foo", function () {})', + errors: [ + { message: 'Some tests seem to be commented', column: 1, line: 1 }, + ], + }, + { + code: '// xit("foo", function () {})', + errors: [ + { message: 'Some tests seem to be commented', column: 1, line: 1 }, + ], + }, + { + code: '// xtest("foo", function () {})', + errors: [ + { message: 'Some tests seem to be commented', column: 1, line: 1 }, + ], + }, + { + code: '// it("has title but no callback")', + errors: [ + { + message: 'Some tests seem to be commented', + column: 1, + line: 1, + }, + ], + }, + { + code: '// test("has title but no callback")', + errors: [ + { + message: 'Some tests seem to be commented', + column: 1, + line: 1, + }, + ], + }, + { + code: ` + foo() + /* + describe("has title but no callback", () => {}) + */ + bar()`, + errors: [ + { + message: 'Some tests seem to be commented', + column: 7, + line: 3, + }, + ], + }, + ], +}); diff --git a/src/rules/no-commented-tests.js b/src/rules/no-commented-tests.js new file mode 100644 index 000000000..9d622e287 --- /dev/null +++ b/src/rules/no-commented-tests.js @@ -0,0 +1,37 @@ +'use strict'; + +const { getDocsUrl } = require('./util'); + +const message = 'Some tests seem to be commented'; + +function hasAssertions(node) { + return /x?(test|it|describe)((\.skip|\[['"]skip['"]\]))?\(/.test(node.value); +} + +module.exports = { + meta: { + docs: { + url: getDocsUrl(__filename), + }, + }, + create(context) { + const sourceCode = context.getSourceCode(); + + function checkNode(node) { + if (!hasAssertions(node)) return; + + context.report({ + message, + node, + }); + } + + return { + Program() { + const comments = sourceCode.getAllComments(); + + comments.filter(token => token.type !== 'Shebang').forEach(checkNode); + }, + }; + }, +}; From f7e970d9b12a5f5e145ec8ff08e8b07ac485e371 Mon Sep 17 00:00:00 2001 From: kangax Date: Wed, 15 May 2019 21:38:19 -0400 Subject: [PATCH 02/13] squash! update docs --- docs/rules/no-commented-tests.md | 48 +++++++++++++++++++------------- 1 file changed, 28 insertions(+), 20 deletions(-) diff --git a/docs/rules/no-commented-tests.md b/docs/rules/no-commented-tests.md index 3d78aa7cb..b2e05231b 100644 --- a/docs/rules/no-commented-tests.md +++ b/docs/rules/no-commented-tests.md @@ -1,30 +1,36 @@ # Disallow commented tests (no-commented-tests) -This rule raises a warning about commented tests. +This rule raises a warning about commented out tests. It's similar to +no-disabled-tests rule. ## Rule Details +The rule uses fuzzy matching to do its best to determine what constitutes a +commented out test, checking for a presence of `it(`, `describe(`, `it.skip(`, +etc. in code comments. + From da6fe04f2d34a6bd8ebe4430bd3d65a46d6d0aab Mon Sep 17 00:00:00 2001 From: kangax Date: Wed, 15 May 2019 21:49:29 -0400 Subject: [PATCH 03/13] squash! add support for only --- src/rules/__tests__/no-commented-tests.test.js | 6 ++++++ src/rules/no-commented-tests.js | 4 +++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/rules/__tests__/no-commented-tests.test.js b/src/rules/__tests__/no-commented-tests.test.js index aad8b256f..0bedc5f7a 100644 --- a/src/rules/__tests__/no-commented-tests.test.js +++ b/src/rules/__tests__/no-commented-tests.test.js @@ -80,6 +80,12 @@ ruleTester.run('no-commented-tests', rule, { { message: 'Some tests seem to be commented', column: 1, line: 1 }, ], }, + { + code: '// it.only("foo", function () {})', + errors: [ + { message: 'Some tests seem to be commented', column: 1, line: 1 }, + ], + }, { code: '// it["skip"]("foo", function () {})', errors: [ diff --git a/src/rules/no-commented-tests.js b/src/rules/no-commented-tests.js index 9d622e287..134ba767f 100644 --- a/src/rules/no-commented-tests.js +++ b/src/rules/no-commented-tests.js @@ -5,7 +5,9 @@ const { getDocsUrl } = require('./util'); const message = 'Some tests seem to be commented'; function hasAssertions(node) { - return /x?(test|it|describe)((\.skip|\[['"]skip['"]\]))?\(/.test(node.value); + return /x?(test|it|describe)((\.only|\.skip|\[['"]skip['"]\]))?\(/.test( + node.value, + ); } module.exports = { From 3dd7264859636905c26015daa56229abb96c0cd5 Mon Sep 17 00:00:00 2001 From: kangax Date: Fri, 17 May 2019 13:52:25 -0400 Subject: [PATCH 04/13] squash! Rename method --- src/rules/__tests__/no-commented-tests.test.js | 10 ++++++++++ src/rules/no-commented-tests.js | 6 +++--- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/rules/__tests__/no-commented-tests.test.js b/src/rules/__tests__/no-commented-tests.test.js index 0bedc5f7a..4c2b857e7 100644 --- a/src/rules/__tests__/no-commented-tests.test.js +++ b/src/rules/__tests__/no-commented-tests.test.js @@ -132,6 +132,16 @@ ruleTester.run('no-commented-tests', rule, { }, ], }, + { + code: '// it()', + errors: [ + { + message: 'Some tests seem to be commented', + column: 1, + line: 1, + }, + ], + }, { code: '// test("has title but no callback")', errors: [ diff --git a/src/rules/no-commented-tests.js b/src/rules/no-commented-tests.js index 134ba767f..bdb497686 100644 --- a/src/rules/no-commented-tests.js +++ b/src/rules/no-commented-tests.js @@ -4,8 +4,8 @@ const { getDocsUrl } = require('./util'); const message = 'Some tests seem to be commented'; -function hasAssertions(node) { - return /x?(test|it|describe)((\.only|\.skip|\[['"]skip['"]\]))?\(/.test( +function hasTests(node) { + return /x?(test|it|describe)((\.only|\.skip|\[['"]skip['"]\]))?\(.*?\)/.test( node.value, ); } @@ -20,7 +20,7 @@ module.exports = { const sourceCode = context.getSourceCode(); function checkNode(node) { - if (!hasAssertions(node)) return; + if (!hasTests(node)) return; context.report({ message, From 9cbe4093cb9dd287755104efa9f076f9c5d2661a Mon Sep 17 00:00:00 2001 From: kangax Date: Fri, 17 May 2019 13:54:14 -0400 Subject: [PATCH 05/13] squash! Rename the rule --- README.md | 4 +- docs/rules/no-commented-tests.md | 61 ------- src/index.js | 2 +- .../__tests__/no-commented-tests.test.js | 171 ------------------ src/rules/no-commented-tests.js | 39 ---- 5 files changed, 3 insertions(+), 274 deletions(-) delete mode 100644 docs/rules/no-commented-tests.md delete mode 100644 src/rules/__tests__/no-commented-tests.test.js delete mode 100644 src/rules/no-commented-tests.js diff --git a/README.md b/README.md index d0b870387..9685dcba9 100644 --- a/README.md +++ b/README.md @@ -96,7 +96,7 @@ for more information about extending configuration files. | [lowercase-name][] | Disallow capitalized test names | | ![fixable-green][] | | [no-alias-methods][] | Disallow alias methods | ![recommended][] | ![fixable-green][] | | [no-disabled-tests][] | Disallow disabled tests | ![recommended][] | | -| [no-commented-tests][] | Disallow commented tests | | | s | +| [no-commented-out-tests][] | Disallow commented out tests | | | s | | [no-empty-title][] | Disallow empty titles | | | | [no-focused-tests][] | Disallow focused tests | ![recommended][] | | | [no-hooks][] | Disallow setup and teardown hooks | | | @@ -143,7 +143,7 @@ https://github.com/dangreenisrael/eslint-plugin-jest-formatting [lowercase-name]: docs/rules/lowercase-name.md [no-alias-methods]: docs/rules/no-alias-methods.md [no-disabled-tests]: docs/rules/no-disabled-tests.md -[no-commented-tests]: docs/rules/no-commented-tests.md +[no-commented-out-tests]: docs/rules/no-commented-out-tests.md [no-empty-title]: docs/rules/no-empty-title.md [no-focused-tests]: docs/rules/no-focused-tests.md [no-hooks]: docs/rules/no-hooks.md diff --git a/docs/rules/no-commented-tests.md b/docs/rules/no-commented-tests.md deleted file mode 100644 index b2e05231b..000000000 --- a/docs/rules/no-commented-tests.md +++ /dev/null @@ -1,61 +0,0 @@ -# Disallow commented tests (no-commented-tests) - -This rule raises a warning about commented out tests. It's similar to -no-disabled-tests rule. - -## Rule Details - -The rule uses fuzzy matching to do its best to determine what constitutes a -commented out test, checking for a presence of `it(`, `describe(`, `it.skip(`, -etc. in code comments. - - diff --git a/src/index.js b/src/index.js index 6ac704f22..0a77aefd4 100644 --- a/src/index.js +++ b/src/index.js @@ -38,7 +38,7 @@ module.exports = { rules: { 'jest/no-alias-methods': 'warn', 'jest/no-disabled-tests': 'warn', - 'jest/no-commented-tests': 'warn', + 'jest/no-commented-out-tests': 'warn', 'jest/no-focused-tests': 'error', 'jest/no-identical-title': 'error', 'jest/no-jest-import': 'error', diff --git a/src/rules/__tests__/no-commented-tests.test.js b/src/rules/__tests__/no-commented-tests.test.js deleted file mode 100644 index 4c2b857e7..000000000 --- a/src/rules/__tests__/no-commented-tests.test.js +++ /dev/null @@ -1,171 +0,0 @@ -'use strict'; - -const { RuleTester } = require('eslint'); -const rule = require('../no-commented-tests'); - -const ruleTester = new RuleTester({ - parserOptions: { - sourceType: 'module', - }, -}); - -ruleTester.run('no-commented-tests', rule, { - valid: [ - '// foo("bar", function () {})', - 'describe("foo", function () {})', - 'it("foo", function () {})', - 'describe.only("foo", function () {})', - 'it.only("foo", function () {})', - 'test("foo", function () {})', - 'test.only("foo", function () {})', - 'var appliedSkip = describe.skip; appliedSkip.apply(describe)', - 'var calledSkip = it.skip; calledSkip.call(it)', - '({ f: function () {} }).f()', - '(a || b).f()', - 'itHappensToStartWithIt()', - 'testSomething()', - [ - 'import { pending } from "actions"', - '', - 'test("foo", () => {', - ' expect(pending()).toEqual({})', - '})', - ].join('\n'), - [ - 'const { pending } = require("actions")', - '', - 'test("foo", () => {', - ' expect(pending()).toEqual({})', - '})', - ].join('\n'), - [ - 'test("foo", () => {', - ' const pending = getPending()', - ' expect(pending()).toEqual({})', - '})', - ].join('\n'), - [ - 'test("foo", () => {', - ' expect(pending()).toEqual({})', - '})', - '', - 'function pending() {', - ' return {}', - '}', - ].join('\n'), - ], - - invalid: [ - { - code: '// describe("foo", function () {})', - errors: [ - { message: 'Some tests seem to be commented', column: 1, line: 1 }, - ], - }, - { - code: '// describe["skip"]("foo", function () {})', - errors: [ - { message: 'Some tests seem to be commented', column: 1, line: 1 }, - ], - }, - { - code: '// describe[\'skip\']("foo", function () {})', - errors: [ - { message: 'Some tests seem to be commented', column: 1, line: 1 }, - ], - }, - { - code: '// it.skip("foo", function () {})', - errors: [ - { message: 'Some tests seem to be commented', column: 1, line: 1 }, - ], - }, - { - code: '// it.only("foo", function () {})', - errors: [ - { message: 'Some tests seem to be commented', column: 1, line: 1 }, - ], - }, - { - code: '// it["skip"]("foo", function () {})', - errors: [ - { message: 'Some tests seem to be commented', column: 1, line: 1 }, - ], - }, - { - code: '// test.skip("foo", function () {})', - errors: [ - { message: 'Some tests seem to be commented', column: 1, line: 1 }, - ], - }, - { - code: '// test["skip"]("foo", function () {})', - errors: [ - { message: 'Some tests seem to be commented', column: 1, line: 1 }, - ], - }, - { - code: '// xdescribe("foo", function () {})', - errors: [ - { message: 'Some tests seem to be commented', column: 1, line: 1 }, - ], - }, - { - code: '// xit("foo", function () {})', - errors: [ - { message: 'Some tests seem to be commented', column: 1, line: 1 }, - ], - }, - { - code: '// xtest("foo", function () {})', - errors: [ - { message: 'Some tests seem to be commented', column: 1, line: 1 }, - ], - }, - { - code: '// it("has title but no callback")', - errors: [ - { - message: 'Some tests seem to be commented', - column: 1, - line: 1, - }, - ], - }, - { - code: '// it()', - errors: [ - { - message: 'Some tests seem to be commented', - column: 1, - line: 1, - }, - ], - }, - { - code: '// test("has title but no callback")', - errors: [ - { - message: 'Some tests seem to be commented', - column: 1, - line: 1, - }, - ], - }, - { - code: ` - foo() - /* - describe("has title but no callback", () => {}) - */ - bar()`, - errors: [ - { - message: 'Some tests seem to be commented', - column: 7, - line: 3, - }, - ], - }, - ], -}); diff --git a/src/rules/no-commented-tests.js b/src/rules/no-commented-tests.js deleted file mode 100644 index bdb497686..000000000 --- a/src/rules/no-commented-tests.js +++ /dev/null @@ -1,39 +0,0 @@ -'use strict'; - -const { getDocsUrl } = require('./util'); - -const message = 'Some tests seem to be commented'; - -function hasTests(node) { - return /x?(test|it|describe)((\.only|\.skip|\[['"]skip['"]\]))?\(.*?\)/.test( - node.value, - ); -} - -module.exports = { - meta: { - docs: { - url: getDocsUrl(__filename), - }, - }, - create(context) { - const sourceCode = context.getSourceCode(); - - function checkNode(node) { - if (!hasTests(node)) return; - - context.report({ - message, - node, - }); - } - - return { - Program() { - const comments = sourceCode.getAllComments(); - - comments.filter(token => token.type !== 'Shebang').forEach(checkNode); - }, - }; - }, -}; From 318f49d7f6d00cd09c0ab1e3e1492a88b9d662aa Mon Sep 17 00:00:00 2001 From: kangax Date: Fri, 17 May 2019 13:55:13 -0400 Subject: [PATCH 06/13] squash! Rename the rule --- docs/rules/no-commented-out-tests.md | 61 +++++++ .../__tests__/no-commented-out-tests.test.js | 171 ++++++++++++++++++ src/rules/no-commented-out-tests.js | 39 ++++ 3 files changed, 271 insertions(+) create mode 100644 docs/rules/no-commented-out-tests.md create mode 100644 src/rules/__tests__/no-commented-out-tests.test.js create mode 100644 src/rules/no-commented-out-tests.js diff --git a/docs/rules/no-commented-out-tests.md b/docs/rules/no-commented-out-tests.md new file mode 100644 index 000000000..9792cb9fd --- /dev/null +++ b/docs/rules/no-commented-out-tests.md @@ -0,0 +1,61 @@ +# Disallow commented tests (no-commented-out-tests) + +This rule raises a warning about commented out tests. It's similar to +no-disabled-tests rule. + +## Rule Details + +The rule uses fuzzy matching to do its best to determine what constitutes a +commented out test, checking for a presence of `it(`, `describe(`, `it.skip(`, +etc. in code comments. + + diff --git a/src/rules/__tests__/no-commented-out-tests.test.js b/src/rules/__tests__/no-commented-out-tests.test.js new file mode 100644 index 000000000..5b5fccd1e --- /dev/null +++ b/src/rules/__tests__/no-commented-out-tests.test.js @@ -0,0 +1,171 @@ +'use strict'; + +const { RuleTester } = require('eslint'); +const rule = require('../no-commented-out-tests'); + +const ruleTester = new RuleTester({ + parserOptions: { + sourceType: 'module', + }, +}); + +ruleTester.run('no-commented-out-tests', rule, { + valid: [ + '// foo("bar", function () {})', + 'describe("foo", function () {})', + 'it("foo", function () {})', + 'describe.only("foo", function () {})', + 'it.only("foo", function () {})', + 'test("foo", function () {})', + 'test.only("foo", function () {})', + 'var appliedSkip = describe.skip; appliedSkip.apply(describe)', + 'var calledSkip = it.skip; calledSkip.call(it)', + '({ f: function () {} }).f()', + '(a || b).f()', + 'itHappensToStartWithIt()', + 'testSomething()', + [ + 'import { pending } from "actions"', + '', + 'test("foo", () => {', + ' expect(pending()).toEqual({})', + '})', + ].join('\n'), + [ + 'const { pending } = require("actions")', + '', + 'test("foo", () => {', + ' expect(pending()).toEqual({})', + '})', + ].join('\n'), + [ + 'test("foo", () => {', + ' const pending = getPending()', + ' expect(pending()).toEqual({})', + '})', + ].join('\n'), + [ + 'test("foo", () => {', + ' expect(pending()).toEqual({})', + '})', + '', + 'function pending() {', + ' return {}', + '}', + ].join('\n'), + ], + + invalid: [ + { + code: '// describe("foo", function () {})', + errors: [ + { message: 'Some tests seem to be commented', column: 1, line: 1 }, + ], + }, + { + code: '// describe["skip"]("foo", function () {})', + errors: [ + { message: 'Some tests seem to be commented', column: 1, line: 1 }, + ], + }, + { + code: '// describe[\'skip\']("foo", function () {})', + errors: [ + { message: 'Some tests seem to be commented', column: 1, line: 1 }, + ], + }, + { + code: '// it.skip("foo", function () {})', + errors: [ + { message: 'Some tests seem to be commented', column: 1, line: 1 }, + ], + }, + { + code: '// it.only("foo", function () {})', + errors: [ + { message: 'Some tests seem to be commented', column: 1, line: 1 }, + ], + }, + { + code: '// it["skip"]("foo", function () {})', + errors: [ + { message: 'Some tests seem to be commented', column: 1, line: 1 }, + ], + }, + { + code: '// test.skip("foo", function () {})', + errors: [ + { message: 'Some tests seem to be commented', column: 1, line: 1 }, + ], + }, + { + code: '// test["skip"]("foo", function () {})', + errors: [ + { message: 'Some tests seem to be commented', column: 1, line: 1 }, + ], + }, + { + code: '// xdescribe("foo", function () {})', + errors: [ + { message: 'Some tests seem to be commented', column: 1, line: 1 }, + ], + }, + { + code: '// xit("foo", function () {})', + errors: [ + { message: 'Some tests seem to be commented', column: 1, line: 1 }, + ], + }, + { + code: '// xtest("foo", function () {})', + errors: [ + { message: 'Some tests seem to be commented', column: 1, line: 1 }, + ], + }, + { + code: '// it("has title but no callback")', + errors: [ + { + message: 'Some tests seem to be commented', + column: 1, + line: 1, + }, + ], + }, + { + code: '// it()', + errors: [ + { + message: 'Some tests seem to be commented', + column: 1, + line: 1, + }, + ], + }, + { + code: '// test("has title but no callback")', + errors: [ + { + message: 'Some tests seem to be commented', + column: 1, + line: 1, + }, + ], + }, + { + code: ` + foo() + /* + describe("has title but no callback", () => {}) + */ + bar()`, + errors: [ + { + message: 'Some tests seem to be commented', + column: 7, + line: 3, + }, + ], + }, + ], +}); diff --git a/src/rules/no-commented-out-tests.js b/src/rules/no-commented-out-tests.js new file mode 100644 index 000000000..bdb497686 --- /dev/null +++ b/src/rules/no-commented-out-tests.js @@ -0,0 +1,39 @@ +'use strict'; + +const { getDocsUrl } = require('./util'); + +const message = 'Some tests seem to be commented'; + +function hasTests(node) { + return /x?(test|it|describe)((\.only|\.skip|\[['"]skip['"]\]))?\(.*?\)/.test( + node.value, + ); +} + +module.exports = { + meta: { + docs: { + url: getDocsUrl(__filename), + }, + }, + create(context) { + const sourceCode = context.getSourceCode(); + + function checkNode(node) { + if (!hasTests(node)) return; + + context.report({ + message, + node, + }); + } + + return { + Program() { + const comments = sourceCode.getAllComments(); + + comments.filter(token => token.type !== 'Shebang').forEach(checkNode); + }, + }; + }, +}; From ab23599a8af2601a0add04092cce2121804e2652 Mon Sep 17 00:00:00 2001 From: kangax Date: Fri, 17 May 2019 13:56:16 -0400 Subject: [PATCH 07/13] squash! Update docs --- docs/rules/no-commented-out-tests.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/rules/no-commented-out-tests.md b/docs/rules/no-commented-out-tests.md index 9792cb9fd..bc671c193 100644 --- a/docs/rules/no-commented-out-tests.md +++ b/docs/rules/no-commented-out-tests.md @@ -1,4 +1,4 @@ -# Disallow commented tests (no-commented-out-tests) +# Disallow commented out tests (no-commented-out-tests) This rule raises a warning about commented out tests. It's similar to no-disabled-tests rule. From f5968b4cf606d9d0ef69306ec4f59d019cf543d0 Mon Sep 17 00:00:00 2001 From: kangax Date: Fri, 17 May 2019 14:02:18 -0400 Subject: [PATCH 08/13] squash! Update rule and tests --- .../__tests__/no-commented-out-tests.test.js | 20 +++++++++++++++++++ src/rules/no-commented-out-tests.js | 4 +--- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/src/rules/__tests__/no-commented-out-tests.test.js b/src/rules/__tests__/no-commented-out-tests.test.js index 5b5fccd1e..43b80aaa2 100644 --- a/src/rules/__tests__/no-commented-out-tests.test.js +++ b/src/rules/__tests__/no-commented-out-tests.test.js @@ -142,6 +142,26 @@ ruleTester.run('no-commented-out-tests', rule, { }, ], }, + { + code: '// test.someNewMethodThatMightBeAddedInTheFuture()', + errors: [ + { + message: 'Some tests seem to be commented', + column: 1, + line: 1, + }, + ], + }, + { + code: '// test["someNewMethodThatMightBeAddedInTheFuture"]()', + errors: [ + { + message: 'Some tests seem to be commented', + column: 1, + line: 1, + }, + ], + }, { code: '// test("has title but no callback")', errors: [ diff --git a/src/rules/no-commented-out-tests.js b/src/rules/no-commented-out-tests.js index bdb497686..19a9590ce 100644 --- a/src/rules/no-commented-out-tests.js +++ b/src/rules/no-commented-out-tests.js @@ -5,9 +5,7 @@ const { getDocsUrl } = require('./util'); const message = 'Some tests seem to be commented'; function hasTests(node) { - return /x?(test|it|describe)((\.only|\.skip|\[['"]skip['"]\]))?\(.*?\)/.test( - node.value, - ); + return /x?(test|it|describe)(\.\w+|\[['"]\w+['"]\])?\(.*?\)/.test(node.value); } module.exports = { From 0a64095605755f5bdda33a4afb2539f5eeb9fb28 Mon Sep 17 00:00:00 2001 From: kangax Date: Mon, 20 May 2019 11:01:16 -0400 Subject: [PATCH 09/13] squash! Address multiline comment and fit --- src/rules/__tests__/no-commented-out-tests.test.js | 14 ++++++++++++++ src/rules/no-commented-out-tests.js | 4 +++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/rules/__tests__/no-commented-out-tests.test.js b/src/rules/__tests__/no-commented-out-tests.test.js index 43b80aaa2..3e74caee4 100644 --- a/src/rules/__tests__/no-commented-out-tests.test.js +++ b/src/rules/__tests__/no-commented-out-tests.test.js @@ -116,12 +116,26 @@ ruleTester.run('no-commented-out-tests', rule, { { message: 'Some tests seem to be commented', column: 1, line: 1 }, ], }, + { + code: '// fit("foo", function () {})', + errors: [ + { message: 'Some tests seem to be commented', column: 1, line: 1 }, + ], + }, { code: '// xtest("foo", function () {})', errors: [ { message: 'Some tests seem to be commented', column: 1, line: 1 }, ], }, + { + code: `// test( + // "foo", function () {} + // )`, + errors: [ + { message: 'Some tests seem to be commented', column: 1, line: 1 }, + ], + }, { code: '// it("has title but no callback")', errors: [ diff --git a/src/rules/no-commented-out-tests.js b/src/rules/no-commented-out-tests.js index 19a9590ce..013dc08cf 100644 --- a/src/rules/no-commented-out-tests.js +++ b/src/rules/no-commented-out-tests.js @@ -5,7 +5,9 @@ const { getDocsUrl } = require('./util'); const message = 'Some tests seem to be commented'; function hasTests(node) { - return /x?(test|it|describe)(\.\w+|\[['"]\w+['"]\])?\(.*?\)/.test(node.value); + return /(x|f)?(test|it|describe)(\.\w+|\[['"]\w+['"]\])?\s*\(/m.test( + node.value, + ); } module.exports = { From f29d37385ebd390a95714a4ce044b46f5fe5e916 Mon Sep 17 00:00:00 2001 From: kangax Date: Mon, 20 May 2019 11:25:33 -0400 Subject: [PATCH 10/13] squash! Add a multiline test --- src/rules/__tests__/no-commented-out-tests.test.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/rules/__tests__/no-commented-out-tests.test.js b/src/rules/__tests__/no-commented-out-tests.test.js index 3e74caee4..6d9a41e6f 100644 --- a/src/rules/__tests__/no-commented-out-tests.test.js +++ b/src/rules/__tests__/no-commented-out-tests.test.js @@ -136,6 +136,16 @@ ruleTester.run('no-commented-out-tests', rule, { { message: 'Some tests seem to be commented', column: 1, line: 1 }, ], }, + { + code: `/* test + ( + "foo", function () {} + ) + */`, + errors: [ + { message: 'Some tests seem to be commented', column: 1, line: 1 }, + ], + }, { code: '// it("has title but no callback")', errors: [ From fc5b73f21e5edb878317d59014c1a564cfc28838 Mon Sep 17 00:00:00 2001 From: kangax Date: Tue, 21 May 2019 11:58:22 -0400 Subject: [PATCH 11/13] squash! Remove rule from default --- src/index.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/index.js b/src/index.js index 0a77aefd4..400651b77 100644 --- a/src/index.js +++ b/src/index.js @@ -38,7 +38,6 @@ module.exports = { rules: { 'jest/no-alias-methods': 'warn', 'jest/no-disabled-tests': 'warn', - 'jest/no-commented-out-tests': 'warn', 'jest/no-focused-tests': 'error', 'jest/no-identical-title': 'error', 'jest/no-jest-import': 'error', From 83a4c8febc1a7d0c41e615fdbd4578b855da2dc9 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Wed, 22 May 2019 14:48:23 +0200 Subject: [PATCH 12/13] chore: remove random char from readme --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 9685dcba9..351b7674c 100644 --- a/README.md +++ b/README.md @@ -96,7 +96,7 @@ for more information about extending configuration files. | [lowercase-name][] | Disallow capitalized test names | | ![fixable-green][] | | [no-alias-methods][] | Disallow alias methods | ![recommended][] | ![fixable-green][] | | [no-disabled-tests][] | Disallow disabled tests | ![recommended][] | | -| [no-commented-out-tests][] | Disallow commented out tests | | | s | +| [no-commented-out-tests][] | Disallow commented out tests | | | | [no-empty-title][] | Disallow empty titles | | | | [no-focused-tests][] | Disallow focused tests | ![recommended][] | | | [no-hooks][] | Disallow setup and teardown hooks | | | From 6bcf16f9750c3b9759a07acae0414e54dc110ea0 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Wed, 22 May 2019 14:49:21 +0200 Subject: [PATCH 13/13] chore: remove html comment from docs --- docs/rules/no-commented-out-tests.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/rules/no-commented-out-tests.md b/docs/rules/no-commented-out-tests.md index bc671c193..829c44291 100644 --- a/docs/rules/no-commented-out-tests.md +++ b/docs/rules/no-commented-out-tests.md @@ -9,7 +9,7 @@ The rule uses fuzzy matching to do its best to determine what constitutes a commented out test, checking for a presence of `it(`, `describe(`, `it.skip(`, etc. in code comments. - +```