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 |
|
26 | Add `jest` to the plugins section of your `.eslintrc` configuration file. You
|
27 | can omit the `eslint-plugin-` prefix:
|
28 |
|
29 | ```json
|
30 | {
|
31 | "plugins": ["jest"]
|
32 | }
|
33 | ```
|
34 |
|
35 | Then 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 |
|
49 | You 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 |
|
63 | This plugin exports a recommended configuration that enforces good testing
|
64 | practices.
|
65 |
|
66 | To enable this configuration use the `extends` property in your `.eslintrc`
|
67 | config file:
|
68 |
|
69 | ```json
|
70 | {
|
71 | "extends": ["plugin:jest/recommended"]
|
72 | }
|
73 | ```
|
74 |
|
75 | ### Style
|
76 |
|
77 | This plugin also exports a configuration named `style`, which adds some
|
78 | stylistic rules, such as `prefer-to-be-null`, which enforces usage of `toBeNull`
|
79 | over `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 |
|
86 | See
|
87 | [ESLint documentation](http://eslint.org/docs/user-guide/configuring#extending-configuration-files)
|
88 | for more information about extending configuration files.
|
89 |
|
90 | ### All
|
91 |
|
92 | If you want to enable all rules instead of only some you can do so by adding the
|
93 | `all` configuration to your `.eslintrc` config file:
|
94 |
|
95 | ```json
|
96 | {
|
97 | "extends": ["plugin:jest/all"]
|
98 | }
|
99 | ```
|
100 |
|
101 | While the `recommended` and `style` configurations only change in major versions
|
102 | the `all` configuration may change in any release and is thus unsuited for
|
103 | installations requiring long-term consistency.
|
104 |
|
105 | ## Rules
|
106 |
|
107 | | Rule | Description | Recommended | Fixable |
|
108 | | ---------------------------- | ----------------------------------------------------------------- | ---------------- | ------------------- |
|
109 | | [consistent-test-it][] | Enforce consistent test or it keyword | | ![fixable-green][] |
|
110 | | [expect-expect][] | Enforce assertion to be made in a test body | | |
|
111 | | [lowercase-name][] | Disallow capitalized test names | | ![fixable-green][] |
|
112 | | [no-alias-methods][] | Disallow alias methods | ![recommended][] | ![fixable-green][] |
|
113 | | [no-disabled-tests][] | Disallow disabled tests | ![recommended][] | |
|
114 | | [no-commented-out-tests][] | Disallow commented out tests | | |
|
115 | | [no-empty-title][] | Disallow empty titles | | |
|
116 | | [no-focused-tests][] | Disallow focused tests | ![recommended][] | |
|
117 | | [no-hooks][] | Disallow setup and teardown hooks | | |
|
118 | | [no-identical-title][] | Disallow identical titles | ![recommended][] | |
|
119 | | [no-jasmine-globals][] | Disallow Jasmine globals | ![recommended][] | ![fixable-yellow][] |
|
120 | | [no-jest-import][] | Disallow importing `jest` | ![recommended][] | |
|
121 | | [no-mocks-import][] | Disallow manually importing from `__mocks__` | | |
|
122 | | [no-large-snapshots][] | Disallow large snapshots | | |
|
123 | | [no-test-callback][] | Using a callback in asynchronous tests | | ![fixable-green][] |
|
124 | | [no-test-prefixes][] | Disallow using `f` & `x` prefixes to define focused/skipped tests | ![recommended][] | ![fixable-green][] |
|
125 | | [no-test-return-statement][] | Disallow explicitly returning from tests | | |
|
126 | | [no-truthy-falsy][] | Disallow using `toBeTruthy()` & `toBeFalsy()` | | |
|
127 | | [prefer-expect-assertions][] | Suggest using `expect.assertions()` OR `expect.hasAssertions()` | | |
|
128 | | [prefer-spy-on][] | Suggest using `jest.spyOn()` | | ![fixable-green][] |
|
129 | | [prefer-strict-equal][] | Suggest using `toStrictEqual()` | | ![fixable-green][] |
|
130 | | [prefer-to-be-null][] | Suggest using `toBeNull()` | | ![fixable-green][] |
|
131 | | [prefer-to-be-undefined][] | Suggest using `toBeUndefined()` | | ![fixable-green][] |
|
132 | | [prefer-to-contain][] | Suggest using `toContain()` | | ![fixable-green][] |
|
133 | | [prefer-to-have-length][] | Suggest using `toHaveLength()` | | ![fixable-green][] |
|
134 | | [prefer-inline-snapshots][] | Suggest using `toMatchInlineSnapshot()` | | ![fixable-green][] |
|
135 | | [require-tothrow-message][] | Require that `toThrow()` and `toThrowError` includes a message | | |
|
136 | | [valid-describe][] | Enforce valid `describe()` callback | ![recommended][] | |
|
137 | | [valid-expect-in-promise][] | Enforce having return statement when testing with promises | ![recommended][] | |
|
138 | | [valid-expect][] | Enforce valid `expect()` usage | ![recommended][] | |
|
139 | | [prefer-todo][] | Suggest using `test.todo()` | | ![fixable-green][] |
|
140 | | [prefer-called-with][] | Suggest using `toBeCalledWith()` OR `toHaveBeenCalledWith()` | | |
|
141 |
|
142 | ## Credit
|
143 |
|
144 | - [eslint-plugin-mocha](https://github.com/lo1tuma/eslint-plugin-mocha)
|
145 | - [eslint-plugin-jasmine](https://github.com/tlvince/eslint-plugin-jasmine)
|
146 |
|
147 | ## Related Projects
|
148 |
|
149 | ### eslint-plugin-jest-formatting
|
150 |
|
151 | This project aims to provide formatting rules (auto-fixable where possible) to
|
152 | ensure consistency and readability in jest test suites.
|
153 |
|
154 | https://github.com/dangreenisrael/eslint-plugin-jest-formatting
|
155 |
|
156 | [consistent-test-it]: docs/rules/consistent-test-it.md
|
157 | [expect-expect]: docs/rules/expect-expect.md
|
158 | [lowercase-name]: docs/rules/lowercase-name.md
|
159 | [no-alias-methods]: docs/rules/no-alias-methods.md
|
160 | [no-disabled-tests]: docs/rules/no-disabled-tests.md
|
161 | [no-commented-out-tests]: docs/rules/no-commented-out-tests.md
|
162 | [no-empty-title]: docs/rules/no-empty-title.md
|
163 | [no-focused-tests]: docs/rules/no-focused-tests.md
|
164 | [no-hooks]: docs/rules/no-hooks.md
|
165 | [no-identical-title]: docs/rules/no-identical-title.md
|
166 | [no-jasmine-globals]: docs/rules/no-jasmine-globals.md
|
167 | [no-jest-import]: docs/rules/no-jest-import.md
|
168 | [no-mocks-import]: docs/rules/no-mocks-import.md
|
169 | [no-large-snapshots]: docs/rules/no-large-snapshots.md
|
170 | [no-test-callback]: docs/rules/no-test-callback.md
|
171 | [no-test-prefixes]: docs/rules/no-test-prefixes.md
|
172 | [no-test-return-statement]: docs/rules/no-test-return-statement.md
|
173 | [no-truthy-falsy]: docs/rules/no-truthy-falsy.md
|
174 | [prefer-called-with]: docs/rules/prefer-called-with.md
|
175 | [prefer-expect-assertions]: docs/rules/prefer-expect-assertions.md
|
176 | [prefer-spy-on]: docs/rules/prefer-spy-on.md
|
177 | [prefer-strict-equal]: docs/rules/prefer-strict-equal.md
|
178 | [prefer-to-be-null]: docs/rules/prefer-to-be-null.md
|
179 | [prefer-to-be-undefined]: docs/rules/prefer-to-be-undefined.md
|
180 | [prefer-to-contain]: docs/rules/prefer-to-contain.md
|
181 | [prefer-to-have-length]: docs/rules/prefer-to-have-length.md
|
182 | [prefer-inline-snapshots]: docs/rules/prefer-inline-snapshots.md
|
183 | [require-tothrow-message]: docs/rules/require-tothrow-message.md
|
184 | [valid-describe]: docs/rules/valid-describe.md
|
185 | [valid-expect-in-promise]: docs/rules/valid-expect-in-promise.md
|
186 | [valid-expect]: docs/rules/valid-expect.md
|
187 | [prefer-todo]: docs/rules/prefer-todo.md
|
188 | [fixable-green]: https://img.shields.io/badge/-fixable-green.svg
|
189 | [fixable-yellow]: https://img.shields.io/badge/-fixable-yellow.svg
|
190 | [recommended]: https://img.shields.io/badge/-recommended-lightgrey.svg
|