Skip to content

Commit

Permalink
Add prefer-to-be-null rule
Browse files Browse the repository at this point in the history
  • Loading branch information
xfumihiro committed Nov 19, 2017
1 parent 4bbec5f commit ec92314
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ You can also whitelist the environment variables provided by Jest by doing:
* [no-focused-tests](/docs/rules/no-focused-tests.md) - disallow focused tests.
* [no-identical-title](/docs/rules/no-identical-title.md) - disallow identical
titles.
* [prefer-to-be-null](/docs/rules/prefer-to-be-null.md) - suggest using
`toBeNull()`.
* [prefer-to-have-length](/docs/rules/prefer-to-have-length.md) - suggest using
`toHaveLength()`.
* [valid-expect](/docs/rules/valid-expect.md) - ensure expect is called
Expand Down
28 changes: 28 additions & 0 deletions docs/rules/prefer-to-be-null.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Suggest using `toBeNull()` (prefer-to-be-null)

In order to have a better failure message, `toBeNull()` should be used upon
asserting expections on null value.

## Rule details

This rule triggers a warning if `toBe()` is used to assert a null value.

```js
expect(null).toBe(null);
```

This rule is enabled by default.

### Default configuration

The following pattern is considered warning:

```js
expect(null).toBe(null);
```

The following pattern is not warning:

```js
expect(null).toBeNull();
```
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const noDisabledTests = require('./rules/no_disabled_tests');
const noFocusedTests = require('./rules/no_focused_tests');
const noIdenticalTitle = require('./rules/no_identical_title');
const preferToBeNull = require('./rules/prefer_to_be_null');
const preferToHaveLength = require('./rules/prefer_to_have_length');
const validExpect = require('./rules/valid_expect');

Expand Down Expand Up @@ -45,6 +46,7 @@ module.exports = {
'no-disabled-tests': noDisabledTests,
'no-focused-tests': noFocusedTests,
'no-identical-title': noIdenticalTitle,
'prefer-to-be-null': preferToBeNull,
'prefer-to-have-length': preferToHaveLength,
'valid-expect': validExpect,
},
Expand Down
35 changes: 35 additions & 0 deletions rules/__tests__/prefer_to_be_null.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
'use strict';

const RuleTester = require('eslint').RuleTester;
const rules = require('../../').rules;

const ruleTester = new RuleTester();

ruleTester.run('prefer_to_be_null', rules['prefer-to-be-null'], {
valid: ['expect(null).toBeNull();'],

invalid: [
{
code: 'expect(null).toBe(null);',
errors: [
{
message: 'Use toBeNull() instead',
column: 14,
line: 1,
},
],
output: 'expect(null).toBeNull();',
},
{
code: 'expect(null).toEqual(null);',
errors: [
{
message: 'Use toBeNull() instead',
column: 14,
line: 1,
},
],
output: 'expect(null).toBeNull();',
},
],
});
37 changes: 37 additions & 0 deletions rules/prefer_to_be_null.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
'use strict';

module.exports = context => {
return {
CallExpression(node) {
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;
const argument = node.parent.parent.arguments[0];

if (
(propertyName === 'toBe' || propertyName === 'toEqual') &&
argument.value === null
) {
context.report({
fix(fixer) {
return [
fixer.replaceText(parentProperty, 'toBeNull'),
fixer.remove(argument),
];
},
message: 'Use toBeNull() instead',
node: parentProperty,
});
}
}
},
};
};

0 comments on commit ec92314

Please sign in to comment.