| 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 | ```json5
|
|---|
| 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 all top-level tests 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 | ```
|
|---|