2020-05-15 05:33:08 +08:00
|
|
|
# Have control over `test` and `it` usages (`consistent-test-it`)
|
2020-03-07 11:45:40 +08:00
|
|
|
|
|
|
|
Jest allows you to choose how you want to define your tests, using the `it` or
|
|
|
|
the `test` keywords, with multiple permutations for each:
|
|
|
|
|
|
|
|
- **it:** `it`, `xit`, `fit`, `it.only`, `it.skip`.
|
|
|
|
- **test:** `test`, `xtest`, `test.only`, `test.skip`.
|
|
|
|
|
|
|
|
This rule gives you control over the usage of these keywords in your codebase.
|
|
|
|
|
|
|
|
## Rule Details
|
|
|
|
|
|
|
|
This rule can be configured as follows
|
|
|
|
|
2020-09-13 06:19:45 +08:00
|
|
|
```json5
|
2020-03-07 11:45:40 +08:00
|
|
|
{
|
2020-09-13 06:19:45 +08:00
|
|
|
type: 'object',
|
|
|
|
properties: {
|
|
|
|
fn: {
|
|
|
|
enum: ['it', 'test'],
|
|
|
|
},
|
|
|
|
withinDescribe: {
|
|
|
|
enum: ['it', 'test'],
|
2020-03-07 11:45:40 +08:00
|
|
|
},
|
2020-09-13 06:19:45 +08:00
|
|
|
},
|
|
|
|
additionalProperties: false,
|
2020-03-07 11:45:40 +08:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
#### fn
|
|
|
|
|
|
|
|
Decides whether to use `test` or `it`.
|
|
|
|
|
|
|
|
#### withinDescribe
|
|
|
|
|
2020-09-13 06:19:45 +08:00
|
|
|
Decides whether to use `test` or `it` within a `describe` scope.
|
2020-03-07 11:45:40 +08:00
|
|
|
|
|
|
|
```js
|
|
|
|
/*eslint jest/consistent-test-it: ["error", {"fn": "test"}]*/
|
|
|
|
|
|
|
|
test('foo'); // valid
|
|
|
|
test.only('foo'); // valid
|
|
|
|
|
|
|
|
it('foo'); // invalid
|
|
|
|
it.only('foo'); // invalid
|
|
|
|
```
|
|
|
|
|
|
|
|
```js
|
|
|
|
/*eslint jest/consistent-test-it: ["error", {"fn": "it"}]*/
|
|
|
|
|
|
|
|
it('foo'); // valid
|
|
|
|
it.only('foo'); // valid
|
|
|
|
|
|
|
|
test('foo'); // invalid
|
|
|
|
test.only('foo'); // invalid
|
|
|
|
```
|
|
|
|
|
|
|
|
```js
|
|
|
|
/*eslint jest/consistent-test-it: ["error", {"fn": "it", "withinDescribe": "test"}]*/
|
|
|
|
|
|
|
|
it('foo'); // valid
|
2020-09-13 06:19:45 +08:00
|
|
|
describe('foo', function () {
|
2020-03-07 11:45:40 +08:00
|
|
|
test('bar'); // valid
|
|
|
|
});
|
|
|
|
|
|
|
|
test('foo'); // invalid
|
2020-09-13 06:19:45 +08:00
|
|
|
describe('foo', function () {
|
2020-03-07 11:45:40 +08:00
|
|
|
it('bar'); // invalid
|
|
|
|
});
|
|
|
|
```
|
|
|
|
|
|
|
|
### Default configuration
|
|
|
|
|
2020-09-13 06:19:45 +08:00
|
|
|
The default configuration forces all top-level tests to use `test` and all tests
|
|
|
|
nested within `describe` to use `it`.
|
2020-03-07 11:45:40 +08:00
|
|
|
|
|
|
|
```js
|
|
|
|
/*eslint jest/consistent-test-it: ["error"]*/
|
|
|
|
|
|
|
|
test('foo'); // valid
|
2020-09-13 06:19:45 +08:00
|
|
|
describe('foo', function () {
|
2020-03-07 11:45:40 +08:00
|
|
|
it('bar'); // valid
|
|
|
|
});
|
|
|
|
|
|
|
|
it('foo'); // invalid
|
2020-09-13 06:19:45 +08:00
|
|
|
describe('foo', function () {
|
2020-03-07 11:45:40 +08:00
|
|
|
test('bar'); // invalid
|
|
|
|
});
|
|
|
|
```
|