🚀 Big News:Socket Has Acquired Secure Annex.Learn More
Socket
Book a DemoSign in
Socket

eslint-plugin-jasmine

Package Overview
Dependencies
Maintainers
1
Versions
56
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

eslint-plugin-jasmine - npm Package Compare versions

Comparing version
2.6.2
to
2.7.0
+61
docs/rules/prefer-jasmine-matcher.md
# Enforce jasmine matchers are used instead of comparison within expect
This rule enforces that within `expect` jasmine matchers are used instead of comparison operators such as
`===`, `==`, `!==`, `!=`, `>`, `<`, `>=`, `<=`.
Standard jasmine matcher include : `toEqual`, `toBe`, `toBeLessThan`, `toBeGreaterThan`, `toThrowError`, `toContain`, `toBeNull`, `toBeUndefined` etc.
## Rule details
This rule triggers a **warning** (is set to **1** by default).
The following patterns are considered a warning:
```js
describe("", function() {
it("", function() {
var a = 1;
expect(a > 0).toBe(true);
});
});
```
```js
describe("", function() {
it("", function() {
var a = "abc";
expect(a.length === 3).toBe(true);
});
});
```
The following patterns are not warnings:
```js
describe("", function() {
it("", function() {
var a = false;
expect(a).not.toBe(true);
});
});
```
```js
describe("", function() {
it("", function() {
var a = "abc";
expect(a.length).toBe(3);
});
});
```
```js
describe("", function() {
it("", function() {
var a = 1;
expect(a).toBeGreaterThan(0);
});
});
```
'use strict'
/**
* @fileoverview Enforce jasmine matchers are used instead of comparison within expect
* @author Diana Suvorova
*/
var blockRegexp = /^((f|x)?(it|describe))$/
var operators = ['===', '==', '!==', '!=', '>', '<', '>=', '<=']
module.exports = function (context) {
var suiteDepth = 0
return {
CallExpression: function (node) {
if (blockRegexp.test(node.callee.name)) {
suiteDepth++
} else if (node.callee.name === 'expect' && suiteDepth > 0) {
var expectNode = node.arguments && node.arguments[0]
if (expectNode.type === 'BinaryExpression' && operators.indexOf(expectNode.operator) > -1) {
context.report({
node,
message: 'Prefer jasmine matcher instead of comparison'
})
}
}
},
'CallExpression:exit': function (node) {
if (blockRegexp.test(node.callee.name)) {
suiteDepth--
}
}
}
}
'use strict'
var rule = require('../../lib/rules/prefer-jasmine-matcher')
var linesToCode = require('../helpers/lines_to_code')
var RuleTester = require('eslint').RuleTester
var eslintTester = new RuleTester()
eslintTester.run('prefer jasmine matcher', rule, {
valid: [
linesToCode([
'describe("", function() {',
' it("", function(){',
'',
' expect(a).not.toBe(true)',
' });',
'});'
]),
linesToCode([
'describe("", function() {',
' it("", function(){',
' expect(1).toBeGreaterThan(0)',
' });',
'});'
]),
linesToCode([
'describe("", function() {',
' it("", function(){',
' var a = [];',
' expect(a.length).toBe(0)',
' });',
'});'
]),
linesToCode([
'describe("", function() {',
' it("", function(){',
' var a = "ab";',
' expect(a instanceof String).toBe(true)',
' });',
'});'
])
],
invalid: [
{
code: linesToCode([
'describe("", function() {',
' it("", function(){',
' expect(1 === 1).toBe(true)',
' });',
'});'
]),
errors: [
{
message: 'Prefer jasmine matcher instead of comparison'
}
]
},
{
code: linesToCode([
'describe("", function() {',
' it("", function(){',
' expect((1 === 1)).toBe(true)',
' });',
'});'
]),
errors: [
{
message: 'Prefer jasmine matcher instead of comparison'
}
]
},
{
code: linesToCode([
'describe("", function() {',
' it("", function(){',
' expect(1 < 2).toBe(true)',
' });',
'});'
]),
errors: [
{
message: 'Prefer jasmine matcher instead of comparison'
}
]
},
{
code: linesToCode([
'describe("", function() {',
' it("", function(){',
' var a = []',
' expect(a.length === 0).toBe(true)',
' });',
'});'
]),
errors: [
{
message: 'Prefer jasmine matcher instead of comparison'
}
]
}
]
})
+4
-2

@@ -19,3 +19,4 @@ 'use strict'

'new-line-between-declarations': require('./lib/rules/new-line-between-declarations'),
'new-line-before-expect': require('./lib/rules/new-line-before-expect')
'new-line-before-expect': require('./lib/rules/new-line-before-expect'),
'prefer-jasmine-matcher': require('./lib/rules/prefer-jasmine-matcher')
},

@@ -39,3 +40,4 @@ configs: {

'jasmine/new-line-between-declarations': 1,
'jasmine/new-line-before-expect': 1
'jasmine/new-line-before-expect': 1,
'jasmine/prefer-jasmine-matcher': 1
}

@@ -42,0 +44,0 @@ }

@@ -25,3 +25,9 @@ 'use strict'

if (!hasPaddingBetweenTokens(prevToken, node)) {
context.report(node, 'No new line before expect')
context.report({
node,
message: 'No new line before expect',
fix (fixer) {
return fixer.insertTextBefore(node, '\n')
}
})
}

@@ -28,0 +34,0 @@ }

@@ -15,21 +15,21 @@ 'use strict'

CallExpression: function (node) {
var pass = true
var declWithoutPadding = null
if (suiteRegexp.test(node.callee.name)) {
var declarations = getDescribeDeclarationsContent(node)
pass = declarations.every((token, i) => {
declarations.forEach((decl, i) => {
var next = declarations[i + 1]
if (next) {
return hasPaddingBetweenTokens(token, next)
} else {
return true
if (next && !hasPaddingBetweenTokens(decl, next)) {
declWithoutPadding = decl
}
})
}
if (!pass) {
if (declWithoutPadding) {
context.report({
node,
message: 'No new line between declarations'
message: 'No new line between declarations',
fix (fixer) {
return fixer.insertTextAfter(declWithoutPadding, '\n')
}
})
};
}
}

@@ -36,0 +36,0 @@ }

@@ -54,3 +54,3 @@ {

},
"version": "2.6.2"
"version": "2.7.0"
}

@@ -84,2 +84,3 @@ # eslint-plugin-jasmine

[valid-expect][] | 1 |
[prefer-jasmine-matcher][] | 1 |

@@ -123,2 +124,3 @@

[valid-expect]: docs/rules/valid-expect.md
[prefer-jasmine-matcher]: docs/rules/prefer-jasmine-matcher.md

@@ -125,0 +127,0 @@ [configuring rules]: http://eslint.org/docs/user-guide/configuring#configuring-rules

@@ -74,3 +74,11 @@ 'use strict'

}
]
],
output: linesToCode([
'describe("", function() {',
' it("", function(){',
' var a = 1',
' \nexpect(1).toBe(1)',
' });',
'});'
])
},

@@ -88,5 +96,11 @@ {

}
]
],
output: linesToCode([
'it("", helper(function() {',
' var a = 1',
' \nexpect(a).toEqual(1);',
'}));'
])
}
]
})

@@ -56,3 +56,12 @@ 'use strict'

}
]
],
output: linesToCode([
'describe("", function() {',
' describe("", function() {',
' it("", function(){});',
' });',
' it("", function(){});\n',
' it("", function(){});',
'});'
])
},

@@ -70,3 +79,9 @@ {

}
]
],
output: linesToCode([
'describe("", function() {',
' beforeEach(function(){});\n',
' it("", function(){});',
'});'
])
},

@@ -84,5 +99,11 @@ {

}
]
],
output: linesToCode([
'describe("", function() {',
' describe("", function() {})\n',
' describe("", function() {})',
'})'
])
}
]
})