Skip to content

Commit

Permalink
Merge pull request #51 from tlvince/remcohaszing-no-assign-spyon
Browse files Browse the repository at this point in the history
feat(no-assign-spyon): add no-assign-spyon rule
  • Loading branch information
tlvince committed Apr 4, 2016
2 parents 8443747 + ba485ad commit aec3661
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .vimrc
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
let $PATH = './node_modules/.bin:' . $PATH
let g:syntastic_javascript_checkers = ['eslint']
let g:neomake_javascript_enabled_makers = ['standard']
13 changes: 9 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,15 @@ configuration files.

Rule | Recommended | Options
---- | ----------- | -------
[named-spy][] | 0 |
[no-focused-tests][] | 2 |
[no-disabled-tests][] | 1 |
[no-spec-dupes][] | 1, `'block'` | `['block', 'branch']`
[no-suite-dupes][] | 1, `'block'` | `['block', 'branch']`
[no-suite-callback-args][] | 2 |
[no-spec-dupes][] | 1, `'block'` | `['block', 'branch']`
[missing-expect][] | 0, `'expect()'` | expectation function names
[no-suite-callback-args][] | 2 |
[valid-expect][] | 1 |
[no-assign-spyon][] | 0 |
For example, using the recommended configuration, the `no-focused-tests` rule
is enabled and will cause ESLint to throw an error (with an exit code of `1`)
Expand All @@ -73,13 +75,16 @@ rules:
See [configuring rules][] for more information.
[named-spy]: docs/rules/named-spy.md
[no-focused-tests]: docs/rules/no-focused-tests.md
[no-disabled-tests]: docs/rules/no-disabled-tests.md
[no-spec-dupes]: docs/rules/no-spec-dupes.md
[no-suite-dupes]: docs/rules/no-suite-dupes.md
[no-suite-callback-args]: docs/rules/no-suite-callback-args.md
[no-spec-dupes]: docs/rules/no-spec-dupes.md
[missing-expect]: docs/rules/missing-expect.md
[no-suite-callback-args]: docs/rules/no-suite-callback-args.md
[valid-expect]: docs/rules/valid-expect.md
[no-assign-spyon]: docs/rules/no-assign-spyon.md
[configuring rules]: http://eslint.org/docs/user-guide/configuring#configuring-rules
## Author
Expand Down
20 changes: 20 additions & 0 deletions docs/rules/no-assign-spyon.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Disallow the assignment of a `spyOn` return value

It is often more obvious to pass the spy as a property of the object spied upon
instead of from a referencing variable.

The following are considered warnings:

```js
var someSpy = spyOn(someObj, 'someMethod');
// Handle someSpy, for example
// expect(someSpy).toHaveBeenCalled();
```

The following are not warnings:

```js
spyOn(someObj, 'someMethod');
// Handle someObj.someMethod, for example
// expect(someObj.someMethod).toHaveBeenCalled();
```
6 changes: 4 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ module.exports = {
'no-spec-dupes': require('./lib/rules/no-spec-dupes'),
'missing-expect': require('./lib/rules/missing-expect'),
'no-suite-callback-args': require('./lib/rules/no-suite-callback-args'),
'valid-expect': require('./lib/rules/valid-expect')
'valid-expect': require('./lib/rules/valid-expect'),
'no-assign-spyon': require('./lib/rules/no-assign-spyon')
},
configs: {
recommended: {
Expand All @@ -21,7 +22,8 @@ module.exports = {
'no-spec-dupes': 1,
'missing-expect': 0,
'no-suite-callback-args': 2,
'valid-expect': 1
'valid-expect': 1,
'no-assign-spyon': 0
}
}
}
Expand Down
16 changes: 16 additions & 0 deletions lib/rules/no-assign-spyon.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use strict'

/**
* @fileoverview Disallow the assignment of a spyOn call result.
* @author Remco Haszing
*/

module.exports = function (context) {
return {
CallExpression: function (node) {
if (node.callee.name === 'spyOn' && (node.parent.type === 'AssignmentExpression' || node.parent.type === 'VariableDeclarator')) {
context.report(node, 'The result of spyOn() should not be assigned')
}
}
}
}
31 changes: 31 additions & 0 deletions test/rules/no-assign-spyon.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
'use strict'

var rule = require('../../lib/rules/no-assign-spyon')
var RuleTester = require('eslint').RuleTester

var eslintTester = new RuleTester()

eslintTester.run('no-assign-spyon', rule, {
valid: [
'var result = someFunc()',
'result = someFunc()'
],
invalid: [
{
code: 'var spy = spyOn(object, "property");',
errors: [
{
message: 'The result of spyOn() should not be assigned'
}
]
},
{
code: 'spy = spyOn(object, "property");',
errors: [
{
message: 'The result of spyOn() should not be assigned'
}
]
}
]
})

0 comments on commit aec3661

Please sign in to comment.