-
-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from SimenB/check-arguments-describe
Add check for no argument in callback in describes
- Loading branch information
Showing
5 changed files
with
178 additions
and
9 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
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,66 @@ | ||
# Enforce that a suites's callback does not contain any arguments | ||
|
||
Jasmine's specs takes an argument, normally called `done`, to enable | ||
asynchronous testing. However, a suite is only a name or title, and contains | ||
only specs, on assertions or expectations. | ||
|
||
Having a suite's callback take an argument and calling that in in a spec to | ||
indicate completion of an asynchronous test is an error, and may cause | ||
confusing errors in other parts of the code du to it's asynchronous nature | ||
|
||
## Rule details | ||
|
||
This rule triggers an **error** (is set to **2** by default) whenever it | ||
encounters a suite's (`describe`) callback receiving an argument. | ||
|
||
The following patterns are considered errors: | ||
|
||
```js | ||
describe('A suite', function(done) {}); | ||
|
||
describe('A suite', function(done) { | ||
it('A spec', function() {}); | ||
}); | ||
|
||
describe('A suite', function(done) { | ||
it('A spec', function(done) {}); | ||
}); | ||
|
||
describe('A suite', function(done) { | ||
it('A spec', function(done) { | ||
done(); | ||
}); | ||
}); | ||
|
||
fdescribe('A suite', function(done) {}); | ||
|
||
ddescribe('A suite', function(done) {}); | ||
|
||
xdescribe('A suite', function(done) {}); | ||
``` | ||
|
||
The following patterns are not warnings: | ||
|
||
```js | ||
describe('My suite', function() {}); | ||
|
||
describe('My suite', function() { | ||
it('My spec', function() {}); | ||
}); | ||
|
||
describe('My suite', function() { | ||
it('My spec', function(done) {}); | ||
}); | ||
|
||
describe('My suite', function() { | ||
it('My spec', function(done) { | ||
done(); | ||
}); | ||
}); | ||
|
||
fdescribe('My suite', function() {}); | ||
|
||
ddescribe('My suite', function() {}); | ||
|
||
xdescribe('My suite', function() {}); | ||
``` |
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,22 @@ | ||
'use strict'; | ||
|
||
/** | ||
* @fileoverview Enforce that a suites's callback does not contain any arguments | ||
* @author Simen Bekkhus | ||
*/ | ||
|
||
var suiteRegexp = /^(f|d|x)?describe$/; | ||
|
||
module.exports = function(context) { | ||
return { | ||
CallExpression: function(node) { | ||
if (suiteRegexp.test(node.callee.name) && node.arguments.length > 1) { | ||
var arg = node.arguments[1]; | ||
|
||
if (arg.type === 'FunctionExpression' && arg.params.length) { | ||
context.report(node, 'Unexpected argument in suite\'s callback') | ||
} | ||
} | ||
} | ||
}; | ||
}; |
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,77 @@ | ||
'use strict'; | ||
|
||
var rule = require('../../lib/rules/no-suite-callback-args'); | ||
var RuleTester = require('eslint').RuleTester; | ||
|
||
var eslintTester = new RuleTester(); | ||
|
||
eslintTester.run('describe-with-done', rule, { | ||
valid: [ | ||
'describe("My suite", function() {});', | ||
'fdescribe("My suite", function() {});', | ||
'ddescribe("My suite", function() {});', | ||
'xdescribe("My suite", function() {});', | ||
'describe("My suite", function() {\nit("A spec", function() {});\n});', | ||
'describe("My suite", function() {\nit("A spec", function(done) {});\n});', | ||
'describe("My suite", function() {\nit("A spec", function(done) {\ndone();\n});\n });' | ||
], | ||
|
||
invalid: [ | ||
{ | ||
code: 'describe("A suite", function(done) {});', | ||
errors: [ | ||
{ | ||
message: 'Unexpected argument in suite\'s callback' | ||
} | ||
] | ||
}, | ||
{ | ||
code: 'fdescribe("A suite", function(done) {});', | ||
errors: [ | ||
{ | ||
message: 'Unexpected argument in suite\'s callback' | ||
} | ||
] | ||
}, | ||
{ | ||
code: 'ddescribe("A suite", function(done) {});', | ||
errors: [ | ||
{ | ||
message: 'Unexpected argument in suite\'s callback' | ||
} | ||
] | ||
}, | ||
{ | ||
code: 'xdescribe("A suite", function(done) {});', | ||
errors: [ | ||
{ | ||
message: 'Unexpected argument in suite\'s callback' | ||
} | ||
] | ||
}, | ||
{ | ||
code: 'describe("A suite", function(done) {\nit("A spec", function() {});\n});', | ||
errors: [ | ||
{ | ||
message: 'Unexpected argument in suite\'s callback' | ||
} | ||
] | ||
}, | ||
{ | ||
code: 'describe("A suite", function(done) {\nit("A spec", function(done) {});\n});', | ||
errors: [ | ||
{ | ||
message: 'Unexpected argument in suite\'s callback' | ||
} | ||
] | ||
}, | ||
{ | ||
code: 'describe("A suite", function(done) {\nit("A spec", function(done) {\ndone();\n});\n });', | ||
errors: [ | ||
{ | ||
message: 'Unexpected argument in suite\'s callback' | ||
} | ||
] | ||
} | ||
] | ||
}); |