Skip to content

Commit 1df3bd5

Browse files
authored
Fix to-have-length when there are no argument properties (#6)
* Fix to-have-length when there are no argument properties * Support both `toBe` and `toEqual` in prefer-to-have-length * Group argument and property together
1 parent 7dfe0b1 commit 1df3bd5

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

rules/__tests__/prefer_to_have_length.test.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@ const rules = require('../..').rules;
66
const ruleTester = new RuleTester();
77

88
ruleTester.run('prefer_to_have_length', rules['prefer-to-have-length'], {
9-
valid: ['expect(files).toHaveLength(1);', "expect(files.name).toBe('file');"],
9+
valid: [
10+
'expect(files).toHaveLength(1);',
11+
"expect(files.name).toBe('file');",
12+
'expect(result).toBe(true);',
13+
],
1014

1115
invalid: [
1216
{
@@ -20,5 +24,16 @@ ruleTester.run('prefer_to_have_length', rules['prefer-to-have-length'], {
2024
],
2125
output: 'expect(files).toHaveLength(1);',
2226
},
27+
{
28+
code: 'expect(files.length).toEqual(1);',
29+
errors: [
30+
{
31+
message: 'Use toHaveLength() instead',
32+
column: 22,
33+
line: 1,
34+
},
35+
],
36+
output: 'expect(files).toHaveLength(1);',
37+
},
2338
],
2439
});

rules/prefer_to_have_length.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@ module.exports = context => {
1717
const argumentObject = node.arguments[0].object;
1818
const argumentProperty = node.arguments[0].property;
1919

20-
if (propertyName === 'toBe' && argumentProperty.name === 'length') {
20+
if (
21+
(propertyName === 'toBe' || propertyName === 'toEqual') &&
22+
argumentProperty &&
23+
argumentProperty.name === 'length'
24+
) {
2125
const propertyDot = context
2226
.getSourceCode()
2327
.getFirstTokenBetween(

0 commit comments

Comments
 (0)