Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New: no-global-assertions rule (refs #20)
- Loading branch information
1 parent
a1ce0bc
commit ed33c78
Showing
4 changed files
with
213 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
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,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; | ||
} | ||
} | ||
}; | ||
}; |
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,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."] | ||
} | ||
] | ||
}); |