Skip to content

Commit

Permalink
Add rule no-pending-tests
Browse files Browse the repository at this point in the history
  • Loading branch information
treythomas123 committed Apr 13, 2016
1 parent df50de1 commit a315b00
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/rules/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

* [no-exclusive-tests](no-exclusive-tests.md) - disallow exclusive mocha tests (fixable)
* [no-skipped-tests](no-skipped-tests.md) - disallow skipped mocha tests (fixable)
* [no-pending-tests](no-pending-tests.md) - disallow pending/unimplemented mocha tests
* [handle-done-callback](handle-done-callback.md) - enforces handling of callbacks for async tests
* [no-synchronous-tests](no-synchronous-tests.md) - disallow synchronous tests
* [no-global-tests](no-global-tests.md) - disallow global tests
43 changes: 43 additions & 0 deletions docs/rules/no-pending-tests.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Disallow Pending Tests (no-pending-tests)

Mocha allows specification of pending tests, which represent tests that aren't yet implemented, but are intended to be implemented eventually. These are designated like a normal mocha test or suite, but with only the first argument provided (no callback for the actual implementation). For example: `describe('unimplemented bdd suite');`

This rule allows you to raise ESLint warnings or errors on pending tests. This can be useful, for example, for reminding developers that pending tests exist in the repository, so they're more likely to get implemented.

## Rule Details

This rule looks for `describe`, `it`, `suite`, `test`, `context`, and `specify` function calls with only one argument, where the argument is a string literal.

The following patterns are considered warnings:

```js
// bdd
describe("foo");
it("foo");

// tdd
suite("foo");
test("foo");
suite("bar");
test("bar");
```

These patterns are not considered warnings:

```js
// bdd
describe("foo", function () {});
it("foo", function () {});

// tdd
suite("foo", function () {});
test("foo", function () {});
```

## When Not To Use It

* If the existence of pending/unimplemented tests isn't considered important enough to warrant raising lint warnings/errors.

## Further Reading

* [Pending Tests](http://mochajs.org/#pending-tests)
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
module.exports = {
rules: {
'no-exclusive-tests': require('./lib/rules/no-exclusive-tests'),
'no-pending-tests': require('./lib/rules/no-pending-tests'),
'no-skipped-tests': require('./lib/rules/no-skipped-tests'),
'handle-done-callback': require('./lib/rules/handle-done-callback'),
'no-synchronous-tests': require('./lib/rules/no-synchronous-tests'),
Expand Down
34 changes: 34 additions & 0 deletions lib/rules/no-pending-tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
'use strict';

module.exports = function (context) {
var mochaTestFunctionNames = [
'it',
'describe',
'suite',
'test',
'context',
'specify'
];

function isMochaTest(callee) {
return callee.type === 'Identifier' &&
mochaTestFunctionNames.indexOf(callee.name) !== -1;
}

function isPendingMochaTest(node) {
return isMochaTest(node.callee) &&
node.arguments.length === 1 &&
node.arguments[0].type === 'Literal';
}

return {
CallExpression: function (node) {
if (node.callee && isPendingMochaTest(node)) {
context.report({
node: node,
message: 'Unexpected pending mocha test.'
});
}
}
};
};
46 changes: 46 additions & 0 deletions test/rules/no-pending-tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
'use strict';

var RuleTester = require('eslint').RuleTester,
rules = require('../../').rules,
ruleTester = new RuleTester(),
expectedErrorMessage = 'Unexpected pending mocha test.';

ruleTester.run('no-pending-tests', rules['no-pending-tests'], {

valid: [
'describe("test", function() { return true; })',
'it("should be false", function() { assert(something, false); })',
'something.describe()',
'something.describe("test")',
'something.it()',
'something.it("test")'
],

invalid: [
{
code: 'describe("this is pending")',
errors: [ { message: expectedErrorMessage, column: 1, line: 1 } ]
},
{
code: 'it("is pending")',
errors: [ { message: expectedErrorMessage, column: 1, line: 1 } ]
},
{
code: 'suite("this is pending")',
errors: [ { message: expectedErrorMessage, column: 1, line: 1 } ]
},
{
code: 'test("is pending")',
errors: [ { message: expectedErrorMessage, column: 1, line: 1 } ]
},
{
code: 'context("this is pending")',
errors: [ { message: expectedErrorMessage, column: 1, line: 1 } ]
},
{
code: 'specify("is pending")',
errors: [ { message: expectedErrorMessage, column: 1, line: 1 } ]
}
]

});

0 comments on commit a315b00

Please sign in to comment.