Skip to content

Commit

Permalink
New: Adding rule no-only (fixes #11)
Browse files Browse the repository at this point in the history
  • Loading branch information
platinumazure committed Nov 27, 2015
1 parent 7b6ca3d commit 8fbdc11
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 0 deletions.
29 changes: 29 additions & 0 deletions docs/rules/no-only.md
@@ -0,0 +1,29 @@
# Forbid the use of QUnit.only (no-only)

`QUnit.only` is useful for restricting a test run to just one test while developing, but committing a test file using this function to a repository is dangerous because it will ensure that the rest of the test suite is not run.

## Rule Details

The following patterns are considered warnings:

```js

QUnit.only('Name', function () { });

```

The following patterns are not considered warnings:

```js

QUnit.test('Name', function () { });

```

## When Not to Use It

If your development pipeline would make running this rule annoying, it could be safely disabled. However, it would be a good idea to ensure that this rule is run in continuous integration at the very least.

## Further Reading

* [QUnit.only](https://api.qunitjs.com/QUnit.only/)
2 changes: 2 additions & 0 deletions index.js
Expand Up @@ -6,13 +6,15 @@ module.exports = {
"no-async-in-loops": require("./lib/rules/no-async-in-loops"),
"no-commented-tests": require("./lib/rules/no-commented-tests"),
"no-ok-equality": require("./lib/rules/no-ok-equality"),
"no-only": require("./lib/rules/no-only"),
"resolve-async": require("./lib/rules/resolve-async")
},
rulesConfig: {
"no-assert-equal": 0,
"no-async-in-loops": 1,
"no-commented-tests": 0,
"no-ok-equality": 1,
"no-only": 1,
"resolve-async": 2
}
};
41 changes: 41 additions & 0 deletions lib/rules/no-only.js
@@ -0,0 +1,41 @@
/**
* @fileoverview Forbid the use of QUnit.only.
* @author Kevin Partington
*/
"use strict";

//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------

var utils = require("../utils");

//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------

module.exports = function (context) {
var MESSAGE = "Unexpected QUnit.only call.";

function isQUnitOnly (calleeNode) {
return calleeNode &&
calleeNode.type === "MemberExpression" &&
calleeNode.object &&
calleeNode.object.type === "Identifier" &&
calleeNode.object.name === "QUnit" &&
calleeNode.property &&
calleeNode.property.type === "Identifier" &&
calleeNode.property.name === "only";
}

return {
"CallExpression": function (node) {
if (isQUnitOnly(node.callee)) {
context.report({
node: node,
message: MESSAGE
});
}
}
};
};
36 changes: 36 additions & 0 deletions tests/lib/rules/no-only.js
@@ -0,0 +1,36 @@
/**
* @fileoverview Forbid the use of QUnit.only.
* @author Kevin Partington
*/
"use strict";

//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------

var rule = require("../../../lib/rules/no-only"),
RuleTester = require("eslint").RuleTester;

//------------------------------------------------------------------------------
// Tests
//------------------------------------------------------------------------------

var ruleTester = new RuleTester();

ruleTester.run("no-only", rule, {
valid: [
"QUnit.test('Name', function() { });",

// QUnit.only is not exposed globally so this is valid
"only('Name', function() { });"
],

invalid: [
{
code: "QUnit.only('Name', function() { });",
errors: [
"Unexpected QUnit.only call."
]
}
]
});

0 comments on commit 8fbdc11

Please sign in to comment.