Skip to content

Commit eb0a2a2

Browse files
ranyitzSimenB
authored andcommitted
feat(rules): add consistent-test-it rule
Fixes #12
1 parent 8518811 commit eb0a2a2

File tree

7 files changed

+652
-24
lines changed

7 files changed

+652
-24
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ for more information about extending configuration files.
8080

8181
| Rule | Description | Recommended | Fixable |
8282
| ------------------------------------------------------------------ | --------------------------------------------------------------- | ----------------------------------------------------------------------- | ----------------------------------------------------------- |
83+
| [consistent-test-it](docs/rules/consistent-test-it.md) | Enforce consistent test or it keyword | | ![fixable](https://img.shields.io/badge/-fixable-green.svg) |
8384
| [no-disabled-tests](docs/rules/no-disabled-tests.md) | Disallow disabled tests | ![recommended](https://img.shields.io/badge/-recommended-lightgrey.svg) | |
8485
| [no-focused-tests](docs/rules/no-focused-tests.md) | Disallow focused tests | ![recommended](https://img.shields.io/badge/-recommended-lightgrey.svg) | |
8586
| [no-identical-title](docs/rules/no-identical-title.md) | Disallow identical titles | ![recommended](https://img.shields.io/badge/-recommended-lightgrey.svg) | |

docs/rules/consistent-test-it.md

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# Have control over `test` and `it` usages (consistent-test-it)
2+
3+
Jest allows you to choose how you want to define your tests, using the `it` or
4+
the `test` keywords, with multiple permutations for each:
5+
6+
* **it:** `it`, `xit`, `fit`, `it.only`, `it.skip`.
7+
* **test:** `test`, `xtest`, `test.only`, `test.skip`.
8+
9+
This rule gives you control over the usage of these keywords in your codebase.
10+
11+
## Rule Details
12+
13+
This rule can be configured as follows
14+
15+
```js
16+
{
17+
type: 'object',
18+
properties: {
19+
fn: {
20+
enum: ['it', 'test'],
21+
},
22+
withinDescribe: {
23+
enum: ['it', 'test'],
24+
},
25+
},
26+
additionalProperties: false,
27+
}
28+
```
29+
30+
#### fn
31+
32+
Decides whether to use `test` or `it`.
33+
34+
#### withinDescribe
35+
36+
Decides whether to use `test` or `it` within a describe scope.
37+
38+
```js
39+
/*eslint jest/consistent-test-it: ["error", {"fn": "test"}]*/
40+
41+
test('foo'); // valid
42+
test.only('foo'); // valid
43+
44+
it('foo'); // invalid
45+
it.only('foo'); // invalid
46+
```
47+
48+
```js
49+
/*eslint jest/consistent-test-it: ["error", {"fn": "it"}]*/
50+
51+
it('foo'); // valid
52+
it.only('foo'); // valid
53+
54+
test('foo'); // invalid
55+
test.only('foo'); // invalid
56+
```
57+
58+
```js
59+
/*eslint jest/consistent-test-it: ["error", {"fn": "it", "withinDescribe": "test"}]*/
60+
61+
it('foo'); // valid
62+
describe('foo', function() {
63+
test('bar'); // valid
64+
});
65+
66+
test('foo'); // invalid
67+
describe('foo', function() {
68+
it('bar'); // invalid
69+
});
70+
```
71+
72+
### Default configuration
73+
74+
The default configuration forces top level test to use `test` and all tests
75+
nested within describe to use `it`.
76+
77+
```js
78+
/*eslint jest/consistent-test-it: ["error"]*/
79+
80+
test('foo'); // valid
81+
describe('foo', function() {
82+
it('bar'); // valid
83+
});
84+
85+
it('foo'); // invalid
86+
describe('foo', function() {
87+
test('bar'); // invalid
88+
});
89+
```

index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict';
22

3+
const consistentTestIt = require('./rules/consistent-test-it');
34
const noDisabledTests = require('./rules/no-disabled-tests');
45
const noFocusedTests = require('./rules/no-focused-tests');
56
const noIdenticalTitle = require('./rules/no-identical-title');
@@ -57,6 +58,7 @@ module.exports = {
5758
'.snap': snapshotProcessor,
5859
},
5960
rules: {
61+
'consistent-test-it': consistentTestIt,
6062
'no-disabled-tests': noDisabledTests,
6163
'no-focused-tests': noFocusedTests,
6264
'no-identical-title': noIdenticalTitle,

0 commit comments

Comments
 (0)