Skip to content

Commit

Permalink
feat(rules): add 'no-get-in-it' rule
Browse files Browse the repository at this point in the history
  • Loading branch information
alecxe committed Jul 10, 2016
1 parent 04ecfda commit 8a3bd68
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ Rule | Default | Options
[use-simple-repeaters][] | 1 |
[no-shadowing][] | 1 |
[use-first-last][] | 1 |
[no-get-in-it][] | 1 |
[by-css-shortcut][] | 0 |

For example, the `missing-perform` rule is enabled by default and will cause
Expand Down Expand Up @@ -75,6 +76,7 @@ See [configuring rules][] for more information.
[use-simple-repeaters]: docs/rules/use-simple-repeaters.md
[no-shadowing]: docs/rules/no-shadowing.md
[use-first-last]: docs/rules/use-first-last.md
[no-get-in-it]: docs/rules/no-get-in-it.md
[by-css-shortcut]: docs/rules/by-css-shortcut.md
[configuring rules]: http://eslint.org/docs/user-guide/configuring#configuring-rules

Expand Down
22 changes: 22 additions & 0 deletions docs/rules/no-get-in-it.md
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() });
```
5 changes: 4 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ var useAngularLocators = require('./lib/rules/use-angular-locators')
var useSimpleRepeaters = require('./lib/rules/use-simple-repeaters')
var noShadowing = require('./lib/rules/no-shadowing')
var useFirstLast = require('./lib/rules/use-first-last')
var noGetInIt = require('./lib/rules/no-get-in-it')

module.exports = {
rules: {
Expand All @@ -26,7 +27,8 @@ module.exports = {
'use-angular-locators': useAngularLocators,
'use-simple-repeaters': useSimpleRepeaters,
'no-shadowing': noShadowing,
'use-first-last': useFirstLast
'use-first-last': useFirstLast,
'no-get-in-it': noGetInIt
},
configs: {
recommended: {
Expand All @@ -42,6 +44,7 @@ module.exports = {
'protractor/use-simple-repeaters': 1,
'protractor/no-shadowing': 1,
'protractor/use-first-last': 1,
'protractor/no-get-in-it': 1,
'protractor/by-css-shortcut': 0
},
globals: {
Expand Down
46 changes: 46 additions & 0 deletions lib/rules/no-get-in-it.js
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
}
}
}
}
}
}
}
}
29 changes: 29 additions & 0 deletions test/rules/no-get-in-it.js
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'
}]
}
]
})

0 comments on commit 8a3bd68

Please sign in to comment.