Skip to content

Commit

Permalink
Add prefer-to-be-defined rule
Browse files Browse the repository at this point in the history
  • Loading branch information
xfumihiro committed Dec 4, 2017
1 parent 2306f85 commit b6a59b9
Show file tree
Hide file tree
Showing 8 changed files with 181 additions and 79 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ project adheres to [Semantic Versioning](http://semver.org/).

### Changed

* `prefer-to-be-undefined` is now merged with the new `prefer-to-be-defined`
rule ([#8](https://github.com/jest-community/eslint-plugin-jest/pull/8))

### Added

## [21.4.0] - 2017-12-04
Expand Down
24 changes: 12 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,22 +72,22 @@ config file:
}
```

See
[ESLint documentation](http://eslint.org/docs/user-guide/configuring#extending-configuration-files)
See [ESLint
documentation](http://eslint.org/docs/user-guide/configuring#extending-configuration-files)
for more information about extending configuration files.

## Rules

| Rule | Description | Recommended | Fixable |
| -------------------------------------------------------------- | ----------------------------- | ----------------------------------------------------------------------- | ----------------------------------------------------------- |
| [no-disabled-tests](docs/rules/no-disabled-tests.md) | Disallow disabled tests | ![recommended](https://img.shields.io/badge/-recommended-lightgrey.svg) | |
| [no-focused-tests](docs/rules/no-focused-tests.md) | Disallow focused tests | ![recommended](https://img.shields.io/badge/-recommended-lightgrey.svg) | |
| [no-identical-title](docs/rules/no-unhandled-errors.md) | Disallow identical titles | ![recommended](https://img.shields.io/badge/-recommended-lightgrey.svg) | |
| [no-large-snapshots](docs/rules/no-large-snapshots.md) | Disallow large snapshots | | |
| [prefer-to-have-length](docs/rules/prefer-to-have-length.md) | Suggest using toHaveLength() | ![recommended](https://img.shields.io/badge/-recommended-lightgrey.svg) | ![fixable](https://img.shields.io/badge/-fixable-green.svg) |
| [prefer-to-be-null](docs/rules/prefer-to-be-null.md) | Suggest using toBeNull() | | ![fixable](https://img.shields.io/badge/-fixable-green.svg) |
| [prefer-to-be-undefined](docs/rules/prefer-to-be-undefined.md) | Suggest using toBeUndefined() | | ![fixable](https://img.shields.io/badge/-fixable-green.svg) |
| [valid-expect](docs/rules/valid-expect.md) | Enforce valid expect() usage | ![recommended](https://img.shields.io/badge/-recommended-lightgrey.svg) | |
| Rule | Description | Recommended | Fixable |
| ------------------------------------------------------------ | --------------------------------------------- | ----------------------------------------------------------------------- | ----------------------------------------------------------- |
| [no-disabled-tests](docs/rules/no-disabled-tests.md) | Disallow disabled tests | ![recommended](https://img.shields.io/badge/-recommended-lightgrey.svg) | |
| [no-focused-tests](docs/rules/no-focused-tests.md) | Disallow focused tests | ![recommended](https://img.shields.io/badge/-recommended-lightgrey.svg) | |
| [no-identical-title](docs/rules/no-unhandled-errors.md) | Disallow identical titles | ![recommended](https://img.shields.io/badge/-recommended-lightgrey.svg) | |
| [no-large-snapshots](docs/rules/no-large-snapshots.md) | Disallow large snapshots | | |
| [prefer-to-have-length](docs/rules/prefer-to-have-length.md) | Suggest using toHaveLength() | ![recommended](https://img.shields.io/badge/-recommended-lightgrey.svg) | ![fixable](https://img.shields.io/badge/-fixable-green.svg) |
| [prefer-to-be-null](docs/rules/prefer-to-be-null.md) | Suggest using toBeNull() | | ![fixable](https://img.shields.io/badge/-fixable-green.svg) |
| [prefer-to-be-defined](docs/rules/prefer-to-be-defined.md) | Suggest using toBeDefined() / toBeUndefined() | | ![fixable](https://img.shields.io/badge/-fixable-green.svg) |
| [valid-expect](docs/rules/valid-expect.md) | Enforce valid expect() usage | ![recommended](https://img.shields.io/badge/-recommended-lightgrey.svg) | |

## Credit

Expand Down
32 changes: 32 additions & 0 deletions docs/rules/prefer-to-be-defined.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Suggest using `toBeDefined()` / `toBeUndefined()` (prefer-to-be-defined)

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

## Rule details

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

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

This rule is enabled by default.

### Default configuration

The following patterns are considered warning:

```js
expect(true).not.toBeUndefined();
expect(undefined).toBe(undefined);
```

The following patterns are not warning:

```js
expect(true).toBeDefined();
expect(undefined).toBeUndefined();
```
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const noFocusedTests = require('./rules/no_focused_tests');
const noIdenticalTitle = require('./rules/no_identical_title');
const noLargeSnapshots = require('./rules/no_large_snapshots');
const preferToBeNull = require('./rules/prefer_to_be_null');
const preferToBeUndefined = require('./rules/prefer_to_be_undefined');
const preferToBeDefined = require('./rules/prefer_to_be_defined');
const preferToHaveLength = require('./rules/prefer_to_have_length');
const validExpect = require('./rules/valid_expect');

Expand Down Expand Up @@ -55,7 +55,7 @@ module.exports = {
'no-identical-title': noIdenticalTitle,
'no-large-snapshots': noLargeSnapshots,
'prefer-to-be-null': preferToBeNull,
'prefer-to-be-undefined': preferToBeUndefined,
'prefer-to-be-defined': preferToBeDefined,
'prefer-to-have-length': preferToHaveLength,
'valid-expect': validExpect,
},
Expand Down
73 changes: 73 additions & 0 deletions rules/__tests__/prefer_to_be_defined.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
'use strict';

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

const ruleTester = new RuleTester();

ruleTester.run('prefer_to_be_defined', rules['prefer-to-be-defined'], {
valid: [
'expect(true).toBeDefined();',
'expect(true).not.toBeDefined();',
'expect(true).toBe(true);',
'expect(undefined).toBeUndefined();',
],

invalid: [
{
code: 'expect(true).not.toBe(undefined);',
errors: [
{
message: 'Use toBeDefined() instead',
column: 14,
line: 1,
},
],
output: 'expect(true).toBeDefined();',
},
{
code: 'expect(true).not.toEqual(undefined);',
errors: [
{
message: 'Use toBeDefined() instead',
column: 14,
line: 1,
},
],
output: 'expect(true).toBeDefined();',
},
{
code: 'expect(true).not.toBeUndefined();',
errors: [
{
message: 'Use toBeDefined() instead',
column: 14,
line: 1,
},
],
output: 'expect(true).toBeDefined();',
},
{
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();',
},
],
});
38 changes: 0 additions & 38 deletions rules/__tests__/prefer_to_be_undefined.test.js

This file was deleted.

59 changes: 59 additions & 0 deletions rules/prefer_to_be_defined.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
'use strict';
const argument = require('./util').argument;
const argument2 = require('./util').argument2;
const expectNotToBeCase = require('./util').expectNotToBeCase;
const expectNotToBeUndefinedCase = require('./util').expectNotToBeUndefinedCase;
const expectNotToEqualCase = require('./util').expectNotToEqualCase;
const expectToBeCase = require('./util').expectToBeCase;
const expectToEqualCase = require('./util').expectToEqualCase;
const method = require('./util').method;
const method2 = require('./util').method2;

module.exports = context => {
return {
CallExpression(node) {
if (
expectNotToBeCase(node, undefined) ||
expectNotToEqualCase(node, undefined) ||
expectNotToBeUndefinedCase(node)
) {
context.report({
fix(fixer) {
const propertyDot = context
.getSourceCode()
.getFirstTokenBetween(
method(node),
method2(node),
token => token.value === '.'
);
const fixes = [
fixer.remove(method(node)),
fixer.remove(propertyDot),
fixer.replaceText(method2(node), 'toBeDefined'),
];
if (argument2(node)) {
fixes.push(fixer.remove(argument2(node)));
}
return fixes;
},
message: 'Use toBeDefined() instead',
node: method(node),
});
} else if (
expectToBeCase(node, undefined) ||
expectToEqualCase(node, undefined)
) {
context.report({
fix(fixer) {
return [
fixer.replaceText(method(node), 'toBeUndefined'),
fixer.remove(argument(node)),
];
},
message: 'Use toBeUndefined() instead',
node: method(node),
});
}
},
};
};
27 changes: 0 additions & 27 deletions rules/prefer_to_be_undefined.js

This file was deleted.

0 comments on commit b6a59b9

Please sign in to comment.