-
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-expect-in-po' rule
- Loading branch information
Showing
6 changed files
with
190 additions
and
3 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,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. |
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 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' | ||
}) | ||
} | ||
} | ||
} | ||
} | ||
} |
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,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' | ||
}] | ||
} | ||
] | ||
}) |