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 10, 2017
1 parent 4bbec5f commit 0020143
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ Then configure the rules you want to use under the rules section.
"jest/no-disabled-tests": "warn",
"jest/no-focused-tests": "error",
"jest/no-identical-title": "error",
"jest/prefer-to-be-null": "warn",
"jest/prefer-to-have-length": "warn",
"jest/valid-expect": "error"
}
Expand All @@ -55,6 +56,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 Expand Up @@ -85,6 +88,7 @@ The rules enabled in this configuration are:
* [jest/no-disabled-tests](/docs/rules/no-disabled-tests.md)
* [jest/no-focused-tests](/docs/rules/no-focused-tests.md)
* [jest/no-identical-title](/docs/rules/no-identical-title.md)
* [jest/prefer-to-be-null](/docs/rules/prefer-to-be-null.md)
* [jest/prefer-to-have-length](/docs/rules/prefer-to-have-length.md)
* [jest/valid-expect](/docs/rules/valid-expect.md)

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();
```
3 changes: 3 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 All @@ -13,6 +14,7 @@ module.exports = {
'jest/no-disabled-tests': 'warn',
'jest/no-focused-tests': 'error',
'jest/no-identical-title': 'error',
'jest/prefer-to-be-null': 'warn',
'jest/prefer-to-have-length': 'warn',
'jest/valid-expect': 'error',
},
Expand Down Expand Up @@ -45,6 +47,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
24 changes: 24 additions & 0 deletions rules/__tests__/prefer_to_be_null.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
'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();',
},
],
});
34 changes: 34 additions & 0 deletions rules/prefer_to_be_null.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
'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' && 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 0020143

Please sign in to comment.