Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use strict';

module.exports = {
extends: ['eslint:recommended', 'prettier'],
plugins: ['prettier'],
parserOptions: {
ecmaVersion: 2017,
},
env: {
node: true,
},
rules: {
strict: 'error',
'prettier/prettier': 'error',
},
};
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
2 changes: 0 additions & 2 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
**/__mocks__/**
**/__tests__/**
src
11 changes: 11 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
language: node_js
node_js:
- node
- 8
- 6
- 4
cache:
yarn: true
branches:
only:
- master
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

geez, I love Travis config being so simple compared to Circle 2

43 changes: 26 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# 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

## Installation
Expand All @@ -8,21 +10,20 @@ Eslint plugin for Jest
$ yarn add --dev eslint eslint-plugin-jest
```

**Note:** If you installed ESLint globally then you must also install `eslint-plugin-jest` globally.
**Note:** If you installed ESLint globally then you must also install
`eslint-plugin-jest` globally.

## Usage

Add `jest` to the plugins section of your `.eslintrc` configuration file. You can omit the `eslint-plugin-` prefix:
Add `jest` to the plugins section of your `.eslintrc` configuration file. You
can omit the `eslint-plugin-` prefix:

```json
{
"plugins": [
"jest"
]
"plugins": ["jest"]
}
```


Then configure the rules you want to use under the rules section.

```json
Expand All @@ -48,33 +49,41 @@ You can also whitelist the environment variables provided by Jest by doing:

## Supported Rules

- [no-disabled-tests](/packages/eslint-plugin-jest/docs/rules/no-disabled-tests.md) - disallow disabled tests.
- [no-focused-tests](/packages/eslint-plugin-jest/docs/rules/no-focused-tests.md) - disallow focused tests.
- [no-identical-title](/packages/eslint-plugin-jest/docs/rules/no-identical-title.md) - disallow identical titles.
- [valid-expect](/packages/eslint-plugin-jest/docs/rules/valid-expect.md) - ensure expect is called correctly.
* [no-disabled-tests](/packages/eslint-plugin-jest/docs/rules/no-disabled-tests.md) -
disallow disabled tests.
* [no-focused-tests](/packages/eslint-plugin-jest/docs/rules/no-focused-tests.md) -
disallow focused tests.
* [no-identical-title](/packages/eslint-plugin-jest/docs/rules/no-identical-title.md) -
disallow identical titles.
* [valid-expect](/packages/eslint-plugin-jest/docs/rules/valid-expect.md) -
ensure expect is called correctly.

## Shareable configurations

### Recommended

This plugin exports a recommended configuration that enforces good testing practices.
This plugin exports a recommended configuration that enforces good testing
practices.

To enable this configuration use the `extends` property in your `.eslintrc` config file:
To enable this configuration use the `extends` property in your `.eslintrc`
config file:

```json
{
"extends": ["plugin:jest/recommended"]
}
```

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

The rules enabled in this configuration are:

- [jest/no-disabled-tests](/packages/eslint-plugin-jest/docs/rules/no-disabled-tests.md)
- [jest/no-focused-tests](/packages/eslint-plugin-jest/docs/rules/no-focused-tests.md)
- [jest/no-identical-title](/packages/eslint-plugin-jest/docs/rules/no-identical-title.md)
- [jest/valid-expect](/packages/eslint-plugin-jest/docs/rules/valid-expect.md)
* [jest/no-disabled-tests](/packages/eslint-plugin-jest/docs/rules/no-disabled-tests.md)
* [jest/no-focused-tests](/packages/eslint-plugin-jest/docs/rules/no-focused-tests.md)
* [jest/no-identical-title](/packages/eslint-plugin-jest/docs/rules/no-identical-title.md)
* [jest/valid-expect](/packages/eslint-plugin-jest/docs/rules/valid-expect.md)

## Credit

Expand Down
3 changes: 2 additions & 1 deletion docs/rules/no-disabled-tests.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ This rule raises a warning about disabled tests.
## Rule Details

There are a number of ways to disable tests in Jest:

* by appending `.skip` to the test-suite or test-case
* by prepending the test function name with `x`
* by declaring a test with a name but no function body
Expand All @@ -34,7 +35,7 @@ it('bar');
test('bar');

it('foo', () => {
pending()
pending();
});
```

Expand Down
16 changes: 10 additions & 6 deletions docs/rules/no-focused-tests.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
# Disallow Focused Tests (no-focused-tests)

Jest has a feature that allows you to focus tests by appending `.only` or prepending `f` to a test-suite or a test-case.
This feature is really helpful to debug a failing test, so you don’t have to execute all of your tests.
After you have fixed your test and before committing the changes you have to remove `.only` to ensure all tests are executed on your build system.
Jest has a feature that allows you to focus tests by appending `.only` or
prepending `f` to a test-suite or a test-case. This feature is really helpful to
debug a failing test, so you don’t have to execute all of your tests. After you
have fixed your test and before committing the changes you have to remove
`.only` to ensure all tests are executed on your build system.

This rule reminds you to remove `.only` from your tests by raising a warning whenever you are using the exclusivity feature.
This rule reminds you to remove `.only` from your tests by raising a warning
whenever you are using the exclusivity feature.

## Rule Details

This rule looks for every `describe.only`, `it.only`, `test.only`, `fdescribe`, `fit` and `ftest` occurrences within the source code.
Of course there are some edge-cases which can’t be detected by this rule e.g.:
This rule looks for every `describe.only`, `it.only`, `test.only`, `fdescribe`,
`fit` and `ftest` occurrences within the source code. Of course there are some
edge-cases which can’t be detected by this rule e.g.:

```js
const describeOnly = describe.only;
Expand Down
15 changes: 11 additions & 4 deletions docs/rules/no-identical-title.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
# Disallow identical titles (no-identical-title)

Having identical titles for two different tests or test suites may create confusion. For example, when a test with the same title as another test in the same test suite fails, it is harder to know which one failed and thus harder to fix.
Having identical titles for two different tests or test suites may create
confusion. For example, when a test with the same title as another test in the
same test suite fails, it is harder to know which one failed and thus harder to
fix.

## Rule Details

This rule looks at the title of every test and test suites. It will report when two test suites or two test cases at the same level of a test suite have the same title.
This rule looks at the title of every test and test suites. It will report when
two test suites or two test cases at the same level of a test suite have the
same title.

The following patterns are considered warnings:

Expand All @@ -17,7 +22,8 @@ describe('foo', () => {
// ...
});

describe('baz', () => { // Has the same title as a previous test suite
describe('baz', () => {
// Has the same title as a previous test suite
// ...
});
});
Expand All @@ -37,7 +43,8 @@ describe('foo', () => {
it('should work', () => {});
});

describe('baz', () => { // Has the same title as a previous test suite
describe('baz', () => {
// Has the same title as a previous test suite
// Has the same name as a test in a sibling test suite, which is fine
it('should work', () => {});
});
Expand Down
8 changes: 5 additions & 3 deletions docs/rules/prefer-to-have-length.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
# Suggest using `toHaveLength()` (prefer-to-have-length)
# Suggest using `toHaveLength()` (prefer-to-have-length)

In order to have a better failure message, `toHaveLength()` should be used upon asserting expections on object's length property.
In order to have a better failure message, `toHaveLength()` should be used upon
asserting expections on object's length property.

## Rule details

This rule triggers a warning if `toBe()` is used to assert object's length property.
This rule triggers a warning if `toBe()` is used to assert object's length
property.

```js
expect(files.length).toBe(1);
Expand Down
8 changes: 5 additions & 3 deletions docs/rules/valid-expect.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
# Enforce valid `expect()` usage (valid-expect)

Ensure `expect()` is called with a single argument and there is an actual expectation made.
Ensure `expect()` is called with a single argument and there is an actual
expectation made.

## Rule details

This rule triggers a warning if `expect()` is called with more than one argument or without arguments.
It would also issue a warning if there is nothing called on `expect()`, e.g.:
This rule triggers a warning if `expect()` is called with more than one argument
or without arguments. It would also issue a warning if there is nothing called
on `expect()`, e.g.:

```js
expect();
Expand Down
19 changes: 6 additions & 13 deletions src/index.js → index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
/**
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/
'use strict';

import noDisabledTests from './rules/no_disabled_tests';
import noFocusedTests from './rules/no_focused_tests';
import noIdenticalTitle from './rules/no_identical_title';
import preferToHaveLength from './rules/prefer_to_have_length';
import validExpect from './rules/valid_expect';
const noDisabledTests = require('./rules/no_disabled_tests');
const noFocusedTests = require('./rules/no_focused_tests');
const noIdenticalTitle = require('./rules/no_identical_title');
const preferToHaveLength = require('./rules/prefer_to_have_length');
const validExpect = require('./rules/valid_expect');

module.exports = {
configs: {
Expand Down
38 changes: 28 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,41 @@
"name": "eslint-plugin-jest",
"version": "21.2.0",
"description": "Eslint rules for Jest",
"repository": {
"type": "git",
"url": "https://github.com/facebook/jest.git"
},
"repository": "jest-community/eslint-plugin-jest",
"license": "MIT",
"keywords": [
"eslint",
"eslintplugin",
"eslint-plugin"
],
"keywords": ["eslint", "eslintplugin", "eslint-plugin"],
"author": {
"name": "Jonathan Kim",
"email": "hello@jkimbo.com",
"url": "jkimbo.com"
},
"main": "build/index.js",
"files": ["docs/", "rules/", "index.js"],
"peerDependencies": {
"eslint": ">=3.6"
},
"scripts": {
"lint": "eslint . --ignore-pattern '!.eslintrc.js'",
"test": "jest",
"precommit": "lint-staged"
},
"devDependencies": {
"eslint": "^4.10.0",
"eslint-config-prettier": "^2.7.0",
"eslint-plugin-prettier": "^2.3.1",
"husky": "^0.14.3",
"jest": "^21.2.1",
"lint-staged": "^4.3.0",
"prettier": "^1.8.1"
},
"prettier": {
"singleQuote": true,
"trailingComma": "all"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add "bracketSpacing": false maybe?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you prefer it? Seems like a fb-ism. Happy to add, of course, I don't really care as long as it's formatted for me :D

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have no preference, just that the diff would be smaller :D. We can scratch that

},
"lint-staged": {
"*.js": ["eslint --fix", "git add"],
"*.{md,json}": ["prettier --write", "git add"]
},
"jest": {
"testEnvironment": "node"
}
}
55 changes: 55 additions & 0 deletions rules/__tests__/no_focused_tests.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
'use strict';

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

const ruleTester = new RuleTester();
const expectedErrorMessage = 'Unexpected focused test.';

ruleTester.run('no-focused-tests', rules['no-focused-tests'], {
valid: [
'describe()',
'it()',
'describe.skip()',
'it.skip()',
'test()',
'test.skip()',
'var appliedOnly = describe.only; appliedOnly.apply(describe)',
'var calledOnly = it.only; calledOnly.call(it)',
],

invalid: [
{
code: 'describe.only()',
errors: [{ message: expectedErrorMessage, column: 10, line: 1 }],
},
{
code: 'describe["only"]()',
errors: [{ message: expectedErrorMessage, column: 10, line: 1 }],
},
{
code: 'it.only()',
errors: [{ message: expectedErrorMessage, column: 4, line: 1 }],
},
{
code: 'it["only"]()',
errors: [{ message: expectedErrorMessage, column: 4, line: 1 }],
},
{
code: 'test.only()',
errors: [{ message: expectedErrorMessage, column: 6, line: 1 }],
},
{
code: 'test["only"]()',
errors: [{ message: expectedErrorMessage, column: 6, line: 1 }],
},
{
code: 'fdescribe()',
errors: [{ message: expectedErrorMessage, column: 1, line: 1 }],
},
{
code: 'fit()',
errors: [{ message: expectedErrorMessage, column: 1, line: 1 }],
},
],
});
Original file line number Diff line number Diff line change
@@ -1,18 +1,7 @@
/**
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/

/* eslint-disable sort-keys */

'use strict';

import {RuleTester} from 'eslint';
const {rules} = require('../../');
const RuleTester = require('eslint').RuleTester;
const rules = require('../..').rules;

const ruleTester = new RuleTester();

Expand Down
Loading