-
Notifications
You must be signed in to change notification settings - Fork 92
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
feat: Add 'no-empty-tests' rule #99
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
047efae
feat: Add 'no-empty-tests' rule
davidkaufmann 1454e31
test: Implement tests for 'no-empty-tests' rule
davidkaufmann 37c17a0
docs: Update docs for 'no-empty-tests' rule
davidkaufmann ce3358f
refactor: Add node type check
davidkaufmann ddffe7a
refactor: Append `.skip` instead of replacement
davidkaufmann 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 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 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,38 @@ | ||
# Checks if tests are empty (no-empty-tests) | ||
|
||
Empty tests will always pass. | ||
In large testsuites tests might have been initialized but not yet written. | ||
Passing but nevertheless empty tests will suggest functionality has been tested, although it hasn't. | ||
Having empty tests might seem convenient for reasons, it isn't on execution though. | ||
Empty tests should always be deleted or skipped. | ||
|
||
## Rule Details | ||
|
||
This rule aims to prevent the execution of empty tests. | ||
|
||
Examples of **incorrect** code for this rule: | ||
|
||
```js | ||
it('an empty test', () => {} ) | ||
describe('nested', () => { it('empty test', () => { } ) }) | ||
context('nested', () => { it('empty test', () => { } ) }) | ||
test('an empty test', () => {} ) | ||
describe('nested', () => { test('empty test', () => { } ) }) | ||
context('nested', () => { test('empty test', () => { } ) }) | ||
``` | ||
|
||
Examples of **correct** code for this rule: | ||
|
||
```js | ||
foo.bar('random empty function', () => {}) | ||
it.skip('a skipped empty test', () => {} ) | ||
it('do something', () => { cy.dataCy('getter') } ) | ||
describe.skip('nested skip', () => { it('empty test', () => { } ) }) | ||
context.skip('nested skip', () => { it('empty test', () => { } ) }) | ||
describe.skip('nested skip', () => { context('nested', () => { it('empty test', () => { } ) }) }) | ||
test.skip('a skipped empty test', () => {} ) | ||
test('do something', () => { cy.dataCy('getter') } ) | ||
describe.skip('nested skip', () => { test('empty test', () => { } ) }) | ||
context.skip('nested skip', () => { test('empty test', () => { } ) }) | ||
describe.skip('nested skip', () => { context('nested', () => { test('empty test', () => { } ) }) }) | ||
``` |
This file contains 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 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 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,92 @@ | ||
'use strict' | ||
|
||
module.exports = { | ||
meta: { | ||
docs: { | ||
description: 'Checks for empty tests', | ||
category: 'Possibly Errors', | ||
recommended: 'error', | ||
}, | ||
fixable: 'code', | ||
hasSuggestions: true, | ||
messages: { | ||
unexpected: 'Do not keep empty tests', | ||
skipTest: 'Skip the test', | ||
removeTest: 'Remove the test', | ||
}, | ||
}, | ||
|
||
create (context) { | ||
|
||
function addSkip (node) { | ||
return (fixer) => fixer.insertTextAfter(node.callee, '.skip') | ||
} | ||
|
||
function removeTest (node) { | ||
return (fixer) => fixer.remove(node) | ||
} | ||
|
||
return { | ||
CallExpression (node) { | ||
if (isEmptyTest(node) && !areParentsSkipped(node)) { | ||
return context.report({ | ||
node, | ||
messageId: 'unexpected', | ||
fix: addSkip(node), | ||
suggest: [ | ||
{ | ||
messageId: 'skipTest', | ||
fix: addSkip(node), | ||
}, | ||
{ | ||
messageId: 'removeTest', | ||
fix: removeTest(node), | ||
}, | ||
], | ||
}) | ||
} | ||
}, | ||
} | ||
}, | ||
} | ||
|
||
const checkNode = (names) => { | ||
return (node) => { | ||
return node.type === 'CallExpression' && ( | ||
(node.callee.type === 'MemberExpression' && | ||
node.callee.object.type === 'Identifier' && | ||
names.includes(node.callee.object.name)) || | ||
(node.callee.type === 'Identifier' && names.includes(node.callee.name))) | ||
} | ||
} | ||
|
||
const isTest = checkNode(['test', 'it']) | ||
const isGroup = checkNode(['describe', 'context']) | ||
|
||
function isEmptyTest (node) { | ||
return isTest(node) && !isSkipped(node) && isFunctionEmpty(node) | ||
} | ||
|
||
function isFunctionEmpty (node) { | ||
return ['ArrowFunctionExpression', 'FunctionExpression', 'Identifier'] | ||
.includes(node.arguments[1].type) && | ||
node.arguments[1].body.body.length === 0 | ||
} | ||
|
||
function isSkipped (node) { | ||
return node.type === 'CallExpression' && | ||
node.callee.type === 'MemberExpression' && | ||
node.callee.property.type === 'Identifier' && | ||
node.callee.property.name === 'skip' | ||
} | ||
|
||
function areParentsSkipped (node) { | ||
while (node.parent) { | ||
node = node.parent | ||
if (isGroup(node) && isSkipped(node)) { | ||
return true | ||
} | ||
} | ||
|
||
return false | ||
} |
This file contains 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,34 @@ | ||
'use strict' | ||
|
||
const rule = require('../../../lib/rules/no-empty-tests') | ||
const RuleTester = require('eslint').RuleTester | ||
|
||
const ruleTester = new RuleTester() | ||
|
||
const errors = [{ messageId: 'unexpected' }] | ||
const parserOptions = { ecmaVersion: 6 } | ||
|
||
ruleTester.run('no-empty-tests', rule, { | ||
valid: [ | ||
{ code: 'foo.bar(\'random empty function\', () => {})', parserOptions }, | ||
{ code: 'it.skip(\'a skipped empty test\', () => {} )', parserOptions }, | ||
{ code: 'it(\'do something\', () => { cy.dataCy(\'getter\') } )', parserOptions }, | ||
{ code: 'describe.skip(\'nested skip\', () => { it(\'empty test\', () => { } ) })', parserOptions }, | ||
{ code: 'context.skip(\'nested skip\', () => { it(\'empty test\', () => { } ) })', parserOptions }, | ||
{ code: 'describe.skip(\'nested skip\', () => { context(\'nested\', () => { it(\'empty test\', () => { } ) }) })', parserOptions }, | ||
{ code: 'test.skip(\'a skipped empty test\', () => {} )', parserOptions }, | ||
{ code: 'test(\'do something\', () => { cy.dataCy(\'getter\') } )', parserOptions }, | ||
{ code: 'describe.skip(\'nested skip\', () => { test(\'empty test\', () => { } ) })', parserOptions }, | ||
{ code: 'context.skip(\'nested skip\', () => { test(\'empty test\', () => { } ) })', parserOptions }, | ||
{ code: 'describe.skip(\'nested skip\', () => { context(\'nested\', () => { test(\'empty test\', () => { } ) }) })', parserOptions }, | ||
], | ||
|
||
invalid: [ | ||
{ code: 'it(\'an empty test\', () => {} )', parserOptions, errors }, | ||
{ code: 'describe(\'nested\', () => { it(\'empty test\', () => { } ) })', parserOptions, errors }, | ||
{ code: 'context(\'nested\', () => { it(\'empty test\', () => { } ) })', parserOptions, errors }, | ||
{ code: 'test(\'an empty test\', () => {} )', parserOptions, errors }, | ||
{ code: 'describe(\'nested\', () => { test(\'empty test\', () => { } ) })', parserOptions, errors }, | ||
{ code: 'context(\'nested\', () => { test(\'empty test\', () => { } ) })', parserOptions, 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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.