Skip to content

Commit

Permalink
Support mochas tdd interface (fixes #4)
Browse files Browse the repository at this point in the history
  • Loading branch information
lo1tuma committed Sep 20, 2014
1 parent cdbdcf9 commit 9991669
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 14 deletions.
26 changes: 17 additions & 9 deletions docs/rules/no-exclusive-tests.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ This rule reminds you to remove `.only` from your tests by raising a warning whe

## Rule Details

This rule looks for every `describe.only` and `it.only` occurrences within the source code.
This rule looks for every `describe.only`, `it.only`, `suite.only` and `test.only` occurrences within the source code.
Of course there are some edge-cases which can’t be detected by this rule e.g.:

```js
Expand All @@ -18,33 +18,41 @@ describeOnly.apply(describe);

The following patterns are considered warnings:

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

it.only("foo", function () {});

describe["only"]("bar", function () {});

it["only"]("bar", function () {});

// tdd
suite.only("foo", function () {});
test.only("foo", function () {});
suite["only"]("bar", function () {});
test["only"]("bar", function () {});

```
These patterns would not be considered warnings:
```js
// bdd
describe("foo", function () {});

it("foo", function () {});

describe.skip("bar", function () {});

it.skip("bar", function () {});
// tdd
suite("foo", function () {});
test("foo", function () {});
suite.skip("bar", function () {});
test.skip("bar", function () {});
```

## When Not To Use It

* If you really want to execute only one test-suite or test-case because all other tests should not be executed, turn this rule off.
* If you use another library which exposes a similar API as mocha (`describe.only` or `it.only`), you should turn this rule off, because it would raise warnings.
* If you use another library which exposes a similar API as mocha (e.g. `describe.only`), you should turn this rule off, because it would raise warnings.

## Further Reading

Expand Down
15 changes: 11 additions & 4 deletions lib/rules/no-exclusive-tests.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
module.exports = function (context) {
'use strict';

var mochaTestFunctions = [
'it',
'describe',
'suite',
'test'
];

function isCallToMochasOnlyFunction(callee) {
return callee.type === 'MemberExpression' &&
isObjectNamedItOrDescribe(callee.object) &&
isPropertyNamedOnly(callee.property);
matchesMochaTestFunction(callee.object) &&
isPropertyNamedOnly(callee.property);
}

function isObjectNamedItOrDescribe(object) {
return object && (object.name === 'it' || object.name === 'describe');
function matchesMochaTestFunction(object) {
return object && mochaTestFunctions.indexOf(object.name) !== -1;
}

function isPropertyNamedOnly(property) {
Expand Down
22 changes: 21 additions & 1 deletion test/rules/no-exclusive-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@ eslintTester.addRuleTest('lib/rules/no-exclusive-tests', {
'it()',
'describe.skip()',
'it.skip()',
'suite()',
'test()',
'suite.skip()',
'test.skip()',
'var appliedOnly = describe.only; appliedOnly.apply(describe)',
'var calledOnly = it.only; calledOnly.call(it)',
'var computedOnly = "only"; describe[computedOnly]()'
'var dynamicOnly = "only"; suite[dynamicOnly]()'
],

invalid: [
Expand All @@ -33,6 +37,22 @@ eslintTester.addRuleTest('lib/rules/no-exclusive-tests', {
{
code: 'it["only"]()',
errors: [ { message: expectedErrorMessage, column: 3, line: 1 } ]
},
{
code: 'suite.only()',
errors: [ { message: expectedErrorMessage, column: 6, line: 1 } ]
},
{
code: 'suite["only"]()',
errors: [ { message: expectedErrorMessage, column: 6, line: 1 } ]
},
{
code: 'test.only()',
errors: [ { message: expectedErrorMessage, column: 5, line: 1 } ]
},
{
code: 'test["only"]()',
errors: [ { message: expectedErrorMessage, column: 5, line: 1 } ]
}
]

Expand Down

0 comments on commit 9991669

Please sign in to comment.