Skip to content

Commit

Permalink
Merge branch 'master' into prefer-to-be-undefined
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB authored Nov 24, 2017
2 parents 0e2f7f8 + 8591e9f commit 3f71f72
Show file tree
Hide file tree
Showing 8 changed files with 200 additions and 47 deletions.
19 changes: 13 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
language: node_js
node_js:
- node
- 8
- 6
- 4
- 8
- node
- 6
- 4
before_install: yarn global add greenkeeper-lockfile@1
before_script: greenkeeper-lockfile-update
after_script: greenkeeper-lockfile-upload
cache:
yarn: true
branches:
only:
- master
only:
- master
- "/^greenkeeper/.*$/"
env:
global:
secure: HN9UwIwZ0NIOgGaRHssKraQy5L4q0Jagtq4nsw75vUHmrc2tNYCpqREtIr7DlapurtVpXsOd5G8FiVMN6YWQA1WVuvoTD7AqJOXYF72gtSnIJ+mER6a4fFQe+2cPMu8l6/jYAFDzQ/ZJqd7QM8DZu6siYAmiJqmq1OmUSP3aohn/bCwyKcvD8MybNQvYvB9j1YzcypbE1LFUeV0oQX8rt9F+5PgY0vB/JE+X806sepgdv8LiVAfy2D/4IhHE55QtC1dfKsCl7Sn1nVriD16Z+OwsabQnp61wptOxaOsHM41eaXd1YlRyqYM1czNoFqgPMyo8uycSO/c7zl5cxWadZmvl21aZXHkb2HTH/XKP6BaZAa7DHwt1S2M8tP9YLDwhO24kSbxRA53Eiqnx+i3kgqsE+g2/XN7dMZpJ3MKgxyXB/dMC1G9qORGs1HdBgDIs4PXP4RMssXpntYJ+QW5JN/cByyA/K90V4K+QV9I4kVVBIXYvTyoyaTj/DwCXwC0zzU+F0BDcTHLtndFvJw6LND/WcYYKTJvwP0glPD/7okPtjYXzWMYPC0jkazLc85g31YvKDvfIel+OJ0j3Z/d2mbekIrL4j1+OSGv2rqSVaLT2H+QUO2WJaYvP8J0ITBVRSp5TopJL9Td9OOhIG/t6FxgKRDnKmoK3647NzcY4TFI=
19 changes: 14 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
# eslint-plugin-jest

[![Build Status](https://travis-ci.org/jest-community/eslint-plugin-jest.svg?branch=master)](https://travis-ci.org/jest-community/eslint-plugin-jest)

Eslint plugin for Jest
[![Build Status](https://travis-ci.org/jest-community/eslint-plugin-jest.svg?branch=master)](https://travis-ci.org/jest-community/eslint-plugin-jest) [![Greenkeeper badge](https://badges.greenkeeper.io/jest-community/eslint-plugin-jest.svg)](https://greenkeeper.io/)

<div align="center">
<a href="https://eslint.org/">
<img width="150" height="150" src="https://eslint.org/img/logo.svg">
</a>
<a href="https://facebook.github.io/jest/">
<img width="150" height="150" vspace="" hspace="25" src="https://cdn.worldvectorlogo.com/logos/jest.svg">
</a>
<h1>eslint-plugin-jest</h1>
<p>ESLint plugin for Jest</p>
</div>

## Installation

Expand Down Expand Up @@ -55,6 +62,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-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
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 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 @@ -46,6 +47,7 @@ module.exports = {
'no-disabled-tests': noDisabledTests,
'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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"eslint-plugin-prettier": "^2.3.1",
"husky": "^0.14.3",
"jest": "^21.2.1",
"lint-staged": "^4.3.0",
"lint-staged": "^5.0.0",
"prettier": "^1.8.1"
},
"prettier": {
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,
});
}
}
},
};
};
Loading

0 comments on commit 3f71f72

Please sign in to comment.