Skip to content

Commit

Permalink
Add prefer-to-have-length rule
Browse files Browse the repository at this point in the history
  • Loading branch information
xfumihiro committed Oct 26, 2017
1 parent 2890498 commit d518e11
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 0 deletions.
3 changes: 3 additions & 0 deletions packages/eslint-plugin-jest/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import noDisabledTests from './rules/no_disabled_tests';
import noFocusedTests from './rules/no_focused_tests';
import noIdenticalTitle from './rules/no_identical_title';
import preferToHaveLength from './rules/prefer_to_have_length';
import validExpect from './rules/valid_expect';

module.exports = {
Expand All @@ -19,6 +20,7 @@ module.exports = {
'jest/no-disabled-tests': 'warn',
'jest/no-focused-tests': 'error',
'jest/no-identical-title': 'error',
'jest/prefer-to-have-length': 'warning',
'jest/valid-expect': 'error',
},
},
Expand Down Expand Up @@ -50,6 +52,7 @@ module.exports = {
'no-disabled-tests': noDisabledTests,
'no-focused-tests': noFocusedTests,
'no-identical-title': noIdenticalTitle,
'prefer-to-have-length': preferToHaveLength,
'valid-expect': validExpect,
},
};
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,
},
],
},
],
});
40 changes: 40 additions & 0 deletions packages/eslint-plugin-jest/src/rules/prefer_to_have_length.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* 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') {
if (
node.arguments.length == 1 &&
node.parent &&
node.parent.type === 'MemberExpression' &&
node.parent.parent
) {
const parentNode = node.parent;
const parentProperty = parentNode.property;
const propertyName = parentProperty.name;

if (
propertyName === 'toBe' &&
node.arguments[0].property.name === 'length'
) {
context.report({
message: 'Use toHaveLength() instead',
node: parentProperty,
});
}
}
}
},
};
};

0 comments on commit d518e11

Please sign in to comment.