Skip to content

Commit

Permalink
feat(rules): add consistent-test-it rule
Browse files Browse the repository at this point in the history
Fixes #12
  • Loading branch information
ranyitz authored and SimenB committed Feb 11, 2018
1 parent 8518811 commit eb0a2a2
Show file tree
Hide file tree
Showing 7 changed files with 652 additions and 24 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ for more information about extending configuration files.

| Rule | Description | Recommended | Fixable |
| ------------------------------------------------------------------ | --------------------------------------------------------------- | ----------------------------------------------------------------------- | ----------------------------------------------------------- |
| [consistent-test-it](docs/rules/consistent-test-it.md) | Enforce consistent test or it keyword | | ![fixable](https://img.shields.io/badge/-fixable-green.svg) |
| [no-disabled-tests](docs/rules/no-disabled-tests.md) | Disallow disabled tests | ![recommended](https://img.shields.io/badge/-recommended-lightgrey.svg) | |
| [no-focused-tests](docs/rules/no-focused-tests.md) | Disallow focused tests | ![recommended](https://img.shields.io/badge/-recommended-lightgrey.svg) | |
| [no-identical-title](docs/rules/no-identical-title.md) | Disallow identical titles | ![recommended](https://img.shields.io/badge/-recommended-lightgrey.svg) | |
Expand Down
89 changes: 89 additions & 0 deletions docs/rules/consistent-test-it.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
# Have control over `test` and `it` usages (consistent-test-it)

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

```js
{
type: 'object',
properties: {
fn: {
enum: ['it', 'test'],
},
withinDescribe: {
enum: ['it', 'test'],
},
},
additionalProperties: false,
}
```

#### fn

Decides whether to use `test` or `it`.

#### withinDescribe

Decides whether to use `test` or `it` within a describe scope.

```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
describe('foo', function() {
test('bar'); // valid
});

test('foo'); // invalid
describe('foo', function() {
it('bar'); // invalid
});
```

### Default configuration

The default configuration forces top level test to use `test` and all tests
nested within describe to use `it`.

```js
/*eslint jest/consistent-test-it: ["error"]*/

test('foo'); // valid
describe('foo', function() {
it('bar'); // valid
});

it('foo'); // invalid
describe('foo', function() {
test('bar'); // invalid
});
```
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';

const consistentTestIt = require('./rules/consistent-test-it');
const noDisabledTests = require('./rules/no-disabled-tests');
const noFocusedTests = require('./rules/no-focused-tests');
const noIdenticalTitle = require('./rules/no-identical-title');
Expand Down Expand Up @@ -57,6 +58,7 @@ module.exports = {
'.snap': snapshotProcessor,
},
rules: {
'consistent-test-it': consistentTestIt,
'no-disabled-tests': noDisabledTests,
'no-focused-tests': noFocusedTests,
'no-identical-title': noIdenticalTitle,
Expand Down
Loading

0 comments on commit eb0a2a2

Please # to comment.