Skip to content

Commit

Permalink
Add prefer-to-be-undefined rule (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
xfumihiro authored and SimenB committed Nov 24, 2017
1 parent 8591e9f commit 8972c41
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 @@ -64,6 +64,8 @@ You can also whitelist the environment variables provided by Jest by doing:
titles.
* [prefer-to-be-null](/docs/rules/prefer-to-be-null.md) - suggest using
`toBeNull()`.
* [prefer-to-be-undefined](/docs/rules/prefer-to-be-undefined.md) - suggest
using `toBeUndefined()`.
* [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-undefined.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Suggest using `toBeUndefined()` (prefer-to-be-undefined)

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

## Rule details

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

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

This rule is enabled by default.

### Default configuration

The following pattern is considered warning:

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

The following pattern is not warning:

```js
expect(undefined).toBeUndefined();
```
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,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 preferToBeUndefined = require('./rules/prefer_to_be_undefined');
const preferToHaveLength = require('./rules/prefer_to_have_length');
const validExpect = require('./rules/valid_expect');

Expand Down Expand Up @@ -47,6 +48,7 @@ module.exports = {
'no-focused-tests': noFocusedTests,
'no-identical-title': noIdenticalTitle,
'prefer-to-be-null': preferToBeNull,
'prefer-to-be-undefined': preferToBeUndefined,
'prefer-to-have-length': preferToHaveLength,
'valid-expect': validExpect,
},
Expand Down
35 changes: 35 additions & 0 deletions rules/__tests__/prefer_to_be_undefined.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_undefined', rules['prefer-to-be-undefined'], {
valid: ['expect(undefined).toBeUndefined();'],

invalid: [
{
code: 'expect(undefined).toBe(undefined);',
errors: [
{
message: 'Use toBeUndefined() instead',
column: 19,
line: 1,
},
],
output: 'expect(undefined).toBeUndefined();',
},
{
code: 'expect(undefined).toEqual(undefined);',
errors: [
{
message: 'Use toBeUndefined() instead',
column: 19,
line: 1,
},
],
output: 'expect(undefined).toBeUndefined();',
},
],
});
37 changes: 37 additions & 0 deletions rules/prefer_to_be_undefined.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 === undefined
) {
context.report({
fix(fixer) {
return [
fixer.replaceText(parentProperty, 'toBeUndefined'),
fixer.remove(argument),
];
},
message: 'Use toBeUndefined() instead',
node: parentProperty,
});
}
}
},
};
};

0 comments on commit 8972c41

Please sign in to comment.