Skip to content

Commit

Permalink
feat(rules): add 'no-expect-in-po' rule
Browse files Browse the repository at this point in the history
  • Loading branch information
alecxe committed Jul 19, 2016
1 parent 28d5d5b commit 04687bd
Show file tree
Hide file tree
Showing 6 changed files with 190 additions and 3 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
This plugin ships with a default configuration for each rule:

Rule | Default | Options
---- | ------- | -------
---- | ------- | ---------
[missing-perform][] | 2 |
[no-browser-pause][] | 2 |
[missing-wait-message][] | 1 |
Expand All @@ -51,6 +51,7 @@ Rule | Default | Options
[no-get-in-it][] | 1 |
[array-callback-return][] | 1 |
[no-absolute-url][] | 1 |
[no-expect-in-po][] | 1 | requires plugin "settings"
[by-css-shortcut][] | 0 |

For example, the `missing-perform` rule is enabled by default and will cause
Expand Down Expand Up @@ -84,6 +85,7 @@ See [configuring rules][] for more information.
[array-callback-return]: docs/rules/array-callback-return.md
[no-absolute-url]: docs/rules/no-absolute-url.md
[by-css-shortcut]: docs/rules/by-css-shortcut.md
[no-expect-in-po]: docs/rules/no-expect-in-po.md
[configuring rules]: http://eslint.org/docs/user-guide/configuring#configuring-rules

## Recommended configuration
Expand Down
23 changes: 23 additions & 0 deletions docs/rules/no-expect-in-po.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Recommend against making assertions inside Page Objects

This rule enforces the [Avoid using `expect()` in page objects](https://github.com/angular/protractor/blob/master/docs/style-guide.md#avoid-using-expect-in-page-objects) Protractor Style Guide recommendation.

**Important:** This rule requires plugin `settings` to have the configured "glob"-style paths (see [`minimatch`](https://github.com/isaacs/minimatch) to learn more about the supported "glob" syntax) to distinguish Page Object files from others.

Edit your ESLint config and add (this is example configuration):

settings: {
"eslint-plugin-protractor":
paths: {
po: ["**/test/e2e/po/*.po.js"]
}
}
}

If this setting is not present, the rule would be disabled.

*Note: Because ESLint uses absolute paths and it is difficult to correctly locate base path of your project from within a plugin, so it is highly suggested to use complete paths to folders you want to disable to leverage the risk of targeting wrong directories and files.*

## Rule details

This rule would warn if it sees `expect()` function call inside a file that matches at least one of the patterns configured in paths/po array.
5 changes: 4 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ var useFirstLast = require('./lib/rules/use-first-last')
var noGetInIt = require('./lib/rules/no-get-in-it')
var arrayCallbackReturn = require('./lib/rules/array-callback-return')
var noAbsoluteURL = require('./lib/rules/no-absolute-url')
var noExpectInPO = require('./lib/rules/no-expect-in-po')

module.exports = {
rules: {
Expand All @@ -34,7 +35,8 @@ module.exports = {
'use-first-last': useFirstLast,
'no-get-in-it': noGetInIt,
'array-callback-return': arrayCallbackReturn,
'no-absolute-url': noAbsoluteURL
'no-absolute-url': noAbsoluteURL,
'no-expect-in-po': noExpectInPO
},
configs: {
recommended: {
Expand All @@ -54,6 +56,7 @@ module.exports = {
'protractor/no-get-in-it': 1,
'protractor/array-callback-return': 1,
'protractor/no-absolute-url': 1,
'protractor/no-expect-in-po': 1,
'protractor/by-css-shortcut': 0
},
globals: {
Expand Down
46 changes: 46 additions & 0 deletions lib/rules/no-expect-in-po.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
'use strict'

/**
* @fileoverview Recommend against making assertions inside Page Objects
* @author Alexander Afanasyev
*/

var PLUGIN_NAME = 'eslint-plugin-protractor'
var multimatch = require('multimatch')

module.exports = {
meta: {
schema: []
},

create: function (context) {
// do nothing if appropriate settings are not present
var settings = context.settings
if (!settings || !settings[PLUGIN_NAME] || !settings[PLUGIN_NAME].paths || !settings[PLUGIN_NAME].paths.po) {
return {}
}

// get glob matches
var filename = context.getFilename()
var patterns = settings[PLUGIN_NAME].paths.po
var matches = multimatch(filename, patterns)

// do nothing if a filename does not match pre-configured patterns
if (!matches || matches.length === 0) {
return {}
}

return {
'CallExpression': function (node) {
var callee = node.callee

if (callee && callee.name === 'expect') {
context.report({
node: node,
message: 'Unexpected "expect()" inside a Page Object'
})
}
}
}
}
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
}
},
"dependencies": {
"css-selector-parser": "^1.1.0"
"css-selector-parser": "^1.1.0",
"multimatch": "^2.1.0"
}
}
112 changes: 112 additions & 0 deletions test/rules/no-expect-in-po.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
'use strict'

var rule = require('../../lib/rules/no-expect-in-po')
var RuleTester = require('eslint').RuleTester
var eslintTester = new RuleTester()

eslintTester.run('no-expect-in-po', rule, {
valid: [
{
code: 'var test = "bar"'
},

{
code: 'expect(1).toEqual(1);'
},

{
code: 'expect(2).toEqual(2);',
'eslint-plugin-protractor': {
settings: {
paths: {
po: ['*.po.js']
}
}
},
filename: 'test.spec.js'
},

{
code: 'expect(3).toEqual(3);',
'eslint-plugin-protractor': {
settings: {
paths: {
po: ['*.somethingelse.js']
}
}
},
filename: 'test.po.js'
},

{
code: 'expect(4).toEqual(4);',
'eslint-plugin-protractor': {
settings: {
paths: {
po: []
}
}
},
filename: 'test.po.js'
},

{
code: 'expect(5).toEqual(5);',
'eslint-plugin-protractor': {
settings: {
paths: {
po: ['*.somethingelse1.js', '*.somethingelse2.js']
}
}
},
filename: 'test.po.js'
}
],

invalid: [
{
code: 'expect(-1).toEqual(-1);',
settings: {
'eslint-plugin-protractor': {
paths: {
po: ['**/po/*.po.js']
}
}
},
filename: '/var/app/test/e2e/po/test.po.js',
errors: [{
message: 'Unexpected "expect()" inside a Page Object'
}]
},

{
code: 'expect(-2).toEqual(-2);',
settings: {
'eslint-plugin-protractor': {
paths: {
po: ['**/*.js']
}
}
},
filename: '/var/app/test/e2e/po/test.po.js',
errors: [{
message: 'Unexpected "expect()" inside a Page Object'
}]
},

{
code: 'expect(-3).toEqual(-3);',
settings: {
'eslint-plugin-protractor': {
paths: {
po: ['**/*.somethingelse1.js', '**/*.po.js', '**/*.somethingelse2.js']
}
}
},
filename: '/var/app/test/e2e/po/test.po.js',
errors: [{
message: 'Unexpected "expect()" inside a Page Object'
}]
}
]
})

0 comments on commit 04687bd

Please sign in to comment.