eslint-plugin-jasmine
Advanced tools
Comparing version 2.1.0 to 2.1.1
@@ -8,4 +8,15 @@ # Enforce valid `expect()` usage (valid-expect) | ||
This rule triggers a warning if `expect()` is called with more than one argument or without arguments. | ||
It would also issue a warning if there is nothing called on `expect()`. | ||
It would also issue a warning if there is nothing called on `expect()`, e.g.: | ||
```js | ||
expect(); | ||
expect("something"); | ||
``` | ||
or when a matcher function was not called, e.g.: | ||
```js | ||
expect(true).toBeDefined | ||
``` | ||
This rule is enabled by default. | ||
@@ -22,2 +33,3 @@ | ||
expect("something"); | ||
expect(true).toBeDefined; | ||
``` | ||
@@ -29,2 +41,4 @@ | ||
expect("something").toEqual("something"); | ||
expect([1, 2, 3]).toEqual([1, 2, 3]); | ||
expect(true).toBeDefined(); | ||
``` |
@@ -14,14 +14,13 @@ 'use strict' | ||
} | ||
if (node.callee.property.name !== 'createSpy') { | ||
return | ||
} | ||
if (node.callee.object.name !== 'jasmine') { | ||
return | ||
} | ||
var identifier | ||
if (node.parent.type === 'VariableDeclarator') { | ||
identifier = node.parent.id | ||
} else if (node.parent.type === 'AssignmentExpression') { | ||
identifier = node.parent.left | ||
} | ||
var identifier = findIdentifier(node) | ||
if (!node.arguments.length || node.arguments[0].type !== 'Literal') { | ||
@@ -32,4 +31,22 @@ context.report(node, 'Unnamed spy') | ||
} | ||
function findIdentifier (node) { | ||
var parent = node.parent | ||
while (parent) { | ||
if (parent.type === 'VariableDeclarator') { | ||
return parent.id | ||
} else if (parent.type === 'Property') { | ||
return parent.key | ||
} else if (parent.type === 'AssignmentExpression' && parent.left) { | ||
if (parent.left.type === 'Identifier') { | ||
return parent.left | ||
} else if (parent.left.property && parent.left.property.type === 'Identifier') { | ||
return parent.left.property | ||
} | ||
} | ||
parent = parent.parent | ||
} | ||
} | ||
} | ||
} | ||
} |
@@ -10,5 +10,5 @@ 'use strict' | ||
return { | ||
// checking "expect()" arguments | ||
CallExpression: function (node) { | ||
if (node.callee.name === 'expect') { | ||
// checking "expect()" arguments | ||
if (node.arguments.length > 1) { | ||
@@ -19,2 +19,7 @@ context.report(node, 'More than one argument passed to expect()') | ||
} | ||
// matcher was not called | ||
if (node.parent && node.parent.parent && node.parent.parent.type !== 'CallExpression') { | ||
context.report(node, 'Matcher was not called') | ||
} | ||
} | ||
@@ -21,0 +26,0 @@ }, |
@@ -41,3 +41,3 @@ { | ||
"mocha": "^3.1.2", | ||
"semantic-release": "^4.0.2", | ||
"semantic-release": "^6.3.2", | ||
"standard": "^8.5.0", | ||
@@ -55,3 +55,3 @@ "validate-commit-msg": "^2.8.2" | ||
}, | ||
"version": "2.1.0" | ||
"version": "2.1.1" | ||
} |
'use strict' | ||
var rule = require('../../lib/rules/named-spy') | ||
var linesToCode = require('../helpers/lines_to_code') | ||
var RuleTester = require('eslint').RuleTester | ||
@@ -20,3 +21,18 @@ | ||
'var onSuccess = jasmine.createSpy("onSuccess")', | ||
'onSuccess = jasmine.createSpy("onSuccess")' | ||
'onSuccess = jasmine.createSpy("onSuccess")', | ||
linesToCode([ | ||
'someObject = {', | ||
' someFunc: jasmine.createSpy("someFunc")', | ||
'};' | ||
]), | ||
linesToCode([ | ||
'someObject = {', | ||
' someFunc: jasmine.createSpy("someFunc").and.callThrough()', | ||
'};' | ||
]), | ||
linesToCode([ | ||
'function someFunc() {', | ||
' this.spy = jasmine.createSpy("spy").and.callThrough()', | ||
'};' | ||
]) | ||
], | ||
@@ -47,4 +63,34 @@ invalid: [ | ||
] | ||
}, | ||
{ | ||
code: linesToCode([ | ||
'someObject = {', | ||
' spy: jasmine.createSpy("someFunc")', | ||
'};' | ||
]), | ||
errors: [ | ||
{message: 'Variable should be named after the spy name'} | ||
] | ||
}, | ||
{ | ||
code: linesToCode([ | ||
'someObject = {', | ||
' spy: jasmine.createSpy("someFunc").and.callThrough()', | ||
'};' | ||
]), | ||
errors: [ | ||
{message: 'Variable should be named after the spy name'} | ||
] | ||
}, | ||
{ | ||
code: linesToCode([ | ||
'function someFunc() {', | ||
' this.spy = jasmine.createSpy("someSpy").and.callThrough()', | ||
'};' | ||
]), | ||
errors: [ | ||
{message: 'Variable should be named after the spy name'} | ||
] | ||
} | ||
] | ||
}) |
@@ -44,2 +44,5 @@ 'use strict' | ||
{ | ||
message: 'Matcher was not called' | ||
}, | ||
{ | ||
message: 'Nothing called on expect()' | ||
@@ -56,7 +59,18 @@ } | ||
{ | ||
message: 'Matcher was not called' | ||
}, | ||
{ | ||
message: 'Nothing called on expect()' | ||
} | ||
] | ||
}, | ||
{ | ||
code: 'expect(true).toBeDefined;', | ||
errors: [ | ||
{ | ||
message: 'Matcher was not called' | ||
} | ||
] | ||
} | ||
] | ||
}) |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
128911
1355
42