Skip to content

Commit

Permalink
New: no-global-assertions rule (refs #20)
Browse files Browse the repository at this point in the history
  • Loading branch information
platinumazure committed Jan 25, 2016
1 parent a1ce0bc commit ed33c78
Show file tree
Hide file tree
Showing 4 changed files with 213 additions and 0 deletions.
71 changes: 71 additions & 0 deletions docs/rules/no-global-assertions.md
@@ -0,0 +1,71 @@
# Forbid the use of global QUnit assertions (no-global-assertions)

QUnit 2.0 is deprecating and removing global QUnit assertions such as `ok()`, requiring consumers to instead use scoped assertions provided on the test callback argument.

## Rule Details

The following patterns are considered warnings:

```js

ok(true);

equal(a, b);

strictEqual(a, b);

deepEqual(a, b);

propEqual(a, b);

notEqual(a, b);

notStrictEqual(a, b);

notDeepEqual(a, b);

notPropEqual(a, b);

raises(function (), TypeError);

throws(function (), TypeError);

```

The following patterns are not considered warnings:

```js

assert.ok(true);

assert.equal(a, b);

assert.strictEqual(a, b);

assert.deepEqual(a, b);

assert.propEqual(a, b);

assert.notEqual(a, b);

assert.notStrictEqual(a, b);

assert.notDeepEqual(a, b);

assert.notPropEqual(a, b);

assert.raises(function (), TypeError);

assert.throws(function (), TypeError);

```

Where `assert` is the name of the first argument to your test callback.

## When Not to Use It

This rule can be safely disabled if you want to tolerate global assertions, especially if your codebase does not use QUnit 2.0 syntax yet.

## Further Reading

* [QUnit 2.x Migration Guide (Assertions)](http://qunitjs.com/upgrade-guide-2.x/#replace-global-assertions-with-assert-arguments)
2 changes: 2 additions & 0 deletions index.js
Expand Up @@ -6,6 +6,7 @@ module.exports = {
"no-assert-equal": require("./lib/rules/no-assert-equal"),
"no-async-in-loops": require("./lib/rules/no-async-in-loops"),
"no-commented-tests": require("./lib/rules/no-commented-tests"),
"no-global-assertions": require("./lib/rules/no-global-assertions"),
"no-ok-equality": require("./lib/rules/no-ok-equality"),
"no-only": require("./lib/rules/no-only"),
"resolve-async": require("./lib/rules/resolve-async")
Expand All @@ -15,6 +16,7 @@ module.exports = {
"no-assert-equal": 0,
"no-async-in-loops": 1,
"no-commented-tests": 0,
"no-global-assertions": 0,
"no-ok-equality": 1,
"no-only": 1,
"resolve-async": 2
Expand Down
41 changes: 41 additions & 0 deletions lib/rules/no-global-assertions.js
@@ -0,0 +1,41 @@
/**
* @fileoverview Forbid the use of global QUnit assertions.
* @author Kevin Partington
*/
"use strict";

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

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

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

module.exports = function (context) {
var currentTestNestingLevel = 0,
MESSAGE = "Unexpected global `{{ assertion }}` assertion.";

return {
"CallExpression": function (node) {
if (utils.isTest(node.callee)) {
++currentTestNestingLevel;
} else if (node.callee.type === "Identifier" && utils.isAssertion(node.callee)) {
context.report({
node: node,
message: MESSAGE,
data: {
assertion: node.callee.name
}
});
}
},
"CallExpression:exit": function (node) {
if (utils.isTest(node.callee)) {
--currentTestNestingLevel;
}
}
};
};
99 changes: 99 additions & 0 deletions tests/lib/rules/no-global-assertions.js
@@ -0,0 +1,99 @@
/**
* @fileoverview Forbid the use of global QUnit assertions.
* @author Kevin Partington
*/
"use strict";

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

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


//------------------------------------------------------------------------------
// Helpers
//------------------------------------------------------------------------------

function wrap(assertionCode, testName) {
testName = testName || "Name";
return "QUnit.test('" +
testName +
"', function (assert) { " +
assertionCode +
" });";
}

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

var ruleTester = new RuleTester();

ruleTester.run("no-global-assertions", rule, {
valid: [
wrap("assert.ok(true);"),
wrap("assert.equal(a, b);"),
wrap("assert.strictEqual(a, b);"),
wrap("assert.deepEqual(a, b);"),
wrap("assert.propEqual(a, b);"),
wrap("assert.notEqual(a, b);"),
wrap("assert.notStrictEqual(a, b);"),
wrap("assert.notDeepEqual(a, b);"),
wrap("assert.notPropEqual(a, b);"),
wrap("assert.raises(function () {}, TypeError);"),
wrap("assert.throws(function () {}, TypeError);"),
wrap("assert.expect(1);"),

// Intentionally not covered by this rule
wrap("expect(1);")
],

invalid: [
{
code: wrap("ok(true);"),
errors: ["Unexpected global `ok` assertion."]
},
{
code: wrap("equal(a, b);"),
errors: ["Unexpected global `equal` assertion."]
},
{
code: wrap("strictEqual(a, b);"),
errors: ["Unexpected global `strictEqual` assertion."]
},
{
code: wrap("deepEqual(a, b);"),
errors: ["Unexpected global `deepEqual` assertion."]
},
{
code: wrap("propEqual(a, b);"),
errors: ["Unexpected global `propEqual` assertion."]
},
{
code: wrap("notEqual(a, b);"),
errors: ["Unexpected global `notEqual` assertion."]
},
{
code: wrap("notStrictEqual(a, b);"),
errors: ["Unexpected global `notStrictEqual` assertion."]
},
{
code: wrap("notDeepEqual(a, b);"),
errors: ["Unexpected global `notDeepEqual` assertion."]
},
{
code: wrap("notPropEqual(a, b);"),
errors: ["Unexpected global `notPropEqual` assertion."]
},
{
code: wrap("raises(function () {}, TypeError);"),
errors: ["Unexpected global `raises` assertion."]
},
{
code: wrap("throws(function () {}, TypeError);"),
errors: ["Unexpected global `throws` assertion."]
}
]
});

0 comments on commit ed33c78

Please sign in to comment.