-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(rules): add 'no-get-in-it' rule
- Loading branch information
Showing
5 changed files
with
103 additions
and
1 deletion.
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,22 @@ | ||
# Recommend against having `browser.get()` or `browser.driver.get()` inside `it()` | ||
|
||
This rule enforces the [Navigate to the page under test before each test](https://github.com/angular/protractor/blob/master/docs/style-guide.md#navigate-to-the-page-under-test-before-each-test) Protractor Style Guide recommendation. | ||
|
||
The rule currently does not allow to configure the test function names and will only look for `it()` test names. | ||
|
||
## Rule details | ||
|
||
Any use of the following patterns are considered warnings: | ||
|
||
```js | ||
it("should do something", function() { browser.get("mypage"); }); | ||
it("should do something", function() { browser.driver.get("mypage"); }); | ||
``` | ||
|
||
The following patterns are not warnings: | ||
|
||
```js | ||
beforeEach(function() { browser.get("mypage"); }); | ||
beforeEach(function() { browser.driver.get("mypage"); }); | ||
it("should do something", function() { browser.waitForAngular() }); | ||
``` |
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,46 @@ | ||
'use strict' | ||
|
||
/** | ||
* @fileoverview Recommend against having `browser.get()` or `browser.driver.get()` inside `it()` | ||
* @author Alexander Afanasyev | ||
*/ | ||
|
||
var testFunctionNames = [ | ||
'it' | ||
] | ||
|
||
module.exports = { | ||
meta: { | ||
schema: [] | ||
}, | ||
|
||
create: function (context) { | ||
return { | ||
'CallExpression': function (node) { | ||
var object = node.callee.object | ||
var property = node.callee.property | ||
|
||
if (object && property && property.name === 'get') { | ||
var isBrowserGet = object.name === 'browser' | ||
var isBrowserDriverGet = object.object && object.object.name === 'browser' && | ||
object.property && object.property.name === 'driver' | ||
|
||
if (isBrowserGet || isBrowserDriverGet) { | ||
// Use ancestors to determine if we are inside the it() block currently | ||
for (var i = 0; i < context.getAncestors().length; i++) { | ||
var parent = context.getAncestors()[i] | ||
if (parent.type === 'CallExpression' && testFunctionNames.indexOf(parent.callee.name) >= 0) { | ||
var methodName = isBrowserGet ? 'browser.get()' : 'browser.driver.get()' | ||
context.report({ | ||
node: node, | ||
message: 'Unexpected "' + methodName + '" inside it' | ||
}) | ||
break | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
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,29 @@ | ||
'use strict' | ||
|
||
var rule = require('../../lib/rules/no-get-in-it') | ||
var RuleTester = require('eslint').RuleTester | ||
var eslintTester = new RuleTester() | ||
|
||
eslintTester.run('no-get-in-it', rule, { | ||
valid: [ | ||
'beforeEach(function() { browser.get("mypage"); });', | ||
'beforeEach(function() { browser.driver.get("mypage"); });', | ||
'browser.get("mypage");', | ||
'browser.driver.get("mypage");' | ||
], | ||
|
||
invalid: [ | ||
{ | ||
code: 'it("should do something", function() { browser.get("mypage"); });', | ||
errors: [{ | ||
message: 'Unexpected "browser.get()" inside it' | ||
}] | ||
}, | ||
{ | ||
code: 'it("should do something", function() { browser.driver.get("mypage"); });', | ||
errors: [{ | ||
message: 'Unexpected "browser.driver.get()" inside it' | ||
}] | ||
} | ||
] | ||
}) |