UNPKG

8.64 kBMarkdownView Raw
1[![Build Status](https://travis-ci.org/jest-community/eslint-plugin-jest.svg?branch=master)](https://travis-ci.org/jest-community/eslint-plugin-jest)
2[![Greenkeeper badge](https://badges.greenkeeper.io/jest-community/eslint-plugin-jest.svg)](https://greenkeeper.io/)
3
4<div align="center">
5 <a href="https://eslint.org/">
6 <img width="150" height="150" src="https://eslint.org/img/logo.svg">
7 </a>
8 <a href="https://facebook.github.io/jest/">
9 <img width="150" height="150" vspace="" hspace="25" src="https://jestjs.io/img/jest.png">
10 </a>
11 <h1>eslint-plugin-jest</h1>
12 <p>ESLint plugin for Jest</p>
13</div>
14
15## Installation
16
17```
18$ yarn add --dev eslint eslint-plugin-jest
19```
20
21**Note:** If you installed ESLint globally then you must also install
22`eslint-plugin-jest` globally.
23
24## Usage
25
26Add `jest` to the plugins section of your `.eslintrc` configuration file. You
27can omit the `eslint-plugin-` prefix:
28
29```json
30{
31 "plugins": ["jest"]
32}
33```
34
35Then configure the rules you want to use under the rules section.
36
37```json
38{
39 "rules": {
40 "jest/no-disabled-tests": "warn",
41 "jest/no-focused-tests": "error",
42 "jest/no-identical-title": "error",
43 "jest/prefer-to-have-length": "warn",
44 "jest/valid-expect": "error"
45 }
46}
47```
48
49You can also whitelist the environment variables provided by Jest by doing:
50
51```json
52{
53 "env": {
54 "jest/globals": true
55 }
56}
57```
58
59## Shareable configurations
60
61### Recommended
62
63This plugin exports a recommended configuration that enforces good testing
64practices.
65
66To enable this configuration use the `extends` property in your `.eslintrc`
67config file:
68
69```json
70{
71 "extends": ["plugin:jest/recommended"]
72}
73```
74
75### Style
76
77This plugin also exports a configuration named `style`, which adds some
78stylistic rules, such as `prefer-to-be-null`, which enforces usage of `toBeNull`
79over `toBe(null)`. All rules included are:
80
81- `prefer-to-be-null`
82- `prefer-to-be-undefined`
83- `prefer-to-contain`
84- `prefer-to-have-length`
85
86See
87[ESLint documentation](http://eslint.org/docs/user-guide/configuring#extending-configuration-files)
88for more information about extending configuration files.
89
90## Rules
91
92| Rule | Description | Recommended | Fixable |
93| ---------------------------- | ----------------------------------------------------------------- | ---------------- | ------------------- |
94| [consistent-test-it][] | Enforce consistent test or it keyword | | ![fixable-green][] |
95| [expect-expect][] | Enforce assertion to be made in a test body | | |
96| [lowercase-name][] | Disallow capitalized test names | | ![fixable-green][] |
97| [no-alias-methods][] | Disallow alias methods | ![recommended][] | ![fixable-green][] |
98| [no-disabled-tests][] | Disallow disabled tests | ![recommended][] | |
99| [no-empty-title][] | Disallow empty titles | | |
100| [no-focused-tests][] | Disallow focused tests | ![recommended][] | |
101| [no-hooks][] | Disallow setup and teardown hooks | | |
102| [no-identical-title][] | Disallow identical titles | ![recommended][] | |
103| [no-jasmine-globals][] | Disallow Jasmine globals | ![recommended][] | ![fixable-yellow][] |
104| [no-jest-import][] | Disallow importing `jest` | ![recommended][] | |
105| [no-large-snapshots][] | Disallow large snapshots | | |
106| [no-test-callback][] | Using a callback in asynchronous tests | | ![fixable-green][] |
107| [no-test-prefixes][] | Disallow using `f` & `x` prefixes to define focused/skipped tests | ![recommended][] | ![fixable-green][] |
108| [no-test-return-statement][] | Disallow explicitly returning from tests | | |
109| [no-truthy-falsy][] | Disallow using `toBeTruthy()` & `toBeFalsy()` | | |
110| [prefer-expect-assertions][] | Suggest using `expect.assertions()` OR `expect.hasAssertions()` | | |
111| [prefer-spy-on][] | Suggest using `jest.spyOn()` | | ![fixable-green][] |
112| [prefer-strict-equal][] | Suggest using `toStrictEqual()` | | ![fixable-green][] |
113| [prefer-to-be-null][] | Suggest using `toBeNull()` | | ![fixable-green][] |
114| [prefer-to-be-undefined][] | Suggest using `toBeUndefined()` | | ![fixable-green][] |
115| [prefer-to-contain][] | Suggest using `toContain()` | | ![fixable-green][] |
116| [prefer-to-have-length][] | Suggest using `toHaveLength()` | | ![fixable-green][] |
117| [prefer-inline-snapshots][] | Suggest using `toMatchInlineSnapshot()` | | ![fixable-green][] |
118| [require-tothrow-message][] | Require that `toThrow()` and `toThrowError` includes a message | | |
119| [valid-describe][] | Enforce valid `describe()` callback | ![recommended][] | |
120| [valid-expect-in-promise][] | Enforce having return statement when testing with promises | ![recommended][] | |
121| [valid-expect][] | Enforce valid `expect()` usage | ![recommended][] | |
122| [prefer-todo][] | Suggest using `test.todo()` | | ![fixable-green][] |
123| [prefer-called-with][] | Suggest using `toBeCalledWith()` OR `toHaveBeenCalledWith()` | | |
124
125## Credit
126
127- [eslint-plugin-mocha](https://github.com/lo1tuma/eslint-plugin-mocha)
128- [eslint-plugin-jasmine](https://github.com/tlvince/eslint-plugin-jasmine)
129
130[consistent-test-it]: docs/rules/consistent-test-it.md
131[expect-expect]: docs/rules/expect-expect.md
132[lowercase-name]: docs/rules/lowercase-name.md
133[no-alias-methods]: docs/rules/no-alias-methods.md
134[no-disabled-tests]: docs/rules/no-disabled-tests.md
135[no-empty-title]: docs/rules/no-empty-title.md
136[no-focused-tests]: docs/rules/no-focused-tests.md
137[no-hooks]: docs/rules/no-hooks.md
138[no-identical-title]: docs/rules/no-identical-title.md
139[no-jasmine-globals]: docs/rules/no-jasmine-globals.md
140[no-jest-import]: docs/rules/no-jest-import.md
141[no-large-snapshots]: docs/rules/no-large-snapshots.md
142[no-test-callback]: docs/rules/no-test-callback.md
143[no-test-prefixes]: docs/rules/no-test-prefixes.md
144[no-test-return-statement]: docs/rules/no-test-return-statement.md
145[no-truthy-falsy]: docs/rules/no-truthy-falsy.md
146[prefer-called-with]: docs/rules/prefer-called-with.md
147[prefer-expect-assertions]: docs/rules/prefer-expect-assertions.md
148[prefer-spy-on]: docs/rules/prefer-spy-on.md
149[prefer-strict-equal]: docs/rules/prefer-strict-equal.md
150[prefer-to-be-null]: docs/rules/prefer-to-be-null.md
151[prefer-to-be-undefined]: docs/rules/prefer-to-be-undefined.md
152[prefer-to-contain]: docs/rules/prefer-to-contain.md
153[prefer-to-have-length]: docs/rules/prefer-to-have-length.md
154[prefer-inline-snapshots]: docs/rules/prefer-inline-snapshots.md
155[require-tothrow-message]: docs/rules/require-tothrow-message.md
156[valid-describe]: docs/rules/valid-describe.md
157[valid-expect-in-promise]: docs/rules/valid-expect-in-promise.md
158[valid-expect]: docs/rules/valid-expect.md
159[prefer-todo]: docs/rules/prefer-todo.md
160[fixable-green]: https://img.shields.io/badge/-fixable-green.svg
161[fixable-yellow]: https://img.shields.io/badge/-fixable-yellow.svg
162[recommended]: https://img.shields.io/badge/-recommended-lightgrey.svg