-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
104 additions
and
0 deletions.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
packages/eslint-plugin-jest/docs/rules/prefer-to-have-length.md
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,27 @@ | ||
# Suggest using `toHaveLength()` (prefer-to-have-length) | ||
|
||
In order to have a better failure message, `toHaveLength()` should be used upon asserting expections on object's length property. | ||
|
||
## Rule details | ||
|
||
This rule triggers a warning if `toBe()` is used to assert object's length property. | ||
|
||
```js | ||
expect(files.length).toBe(1); | ||
``` | ||
|
||
This rule is enabled by default. | ||
|
||
### Default configuration | ||
|
||
The following pattern is considered warning: | ||
|
||
```js | ||
expect(files.length).toBe(1); | ||
``` | ||
|
||
The following pattern is not warning: | ||
|
||
```js | ||
expect(files).toHaveLength(1); | ||
``` |
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
34 changes: 34 additions & 0 deletions
34
packages/eslint-plugin-jest/src/rules/__tests__/prefer_to_have_length.test.js
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,34 @@ | ||
/** | ||
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @flow | ||
*/ | ||
|
||
/* eslint-disable sort-keys */ | ||
|
||
'use strict'; | ||
|
||
import {RuleTester} from 'eslint'; | ||
const {rules} = require('../../'); | ||
|
||
const ruleTester = new RuleTester(); | ||
|
||
ruleTester.run('prefer_to_have_length', rules['prefer-to-have-length'], { | ||
valid: ['expect(files).toHaveLength(1);', "expect(files.name).toBe('file');"], | ||
|
||
invalid: [ | ||
{ | ||
code: 'expect(files.length).toBe(1);', | ||
errors: [ | ||
{ | ||
message: 'Use toHaveLength() instead', | ||
column: 22, | ||
line: 1, | ||
}, | ||
], | ||
}, | ||
], | ||
}); |
39 changes: 39 additions & 0 deletions
39
packages/eslint-plugin-jest/src/rules/prefer_to_have_length.js
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,39 @@ | ||
/** | ||
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @flow | ||
*/ | ||
|
||
import type {EslintContext, CallExpression} from './types'; | ||
|
||
export default (context: EslintContext) => { | ||
return { | ||
CallExpression(node: CallExpression) { | ||
const calleeName = node.callee.name; | ||
|
||
if ( | ||
calleeName === 'expect' && | ||
node.arguments.length == 1 && | ||
node.parent && | ||
node.parent.type === 'MemberExpression' && | ||
node.parent.parent | ||
) { | ||
const parentProperty = node.parent.property; | ||
const propertyName = parentProperty.name; | ||
|
||
if ( | ||
propertyName === 'toBe' && | ||
node.arguments[0].property.name === 'length' | ||
) { | ||
context.report({ | ||
message: 'Use toHaveLength() instead', | ||
node: parentProperty, | ||
}); | ||
} | ||
} | ||
}, | ||
}; | ||
}; |
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