Skip to content

Commit

Permalink
feat: create prefer-each rule
Browse files Browse the repository at this point in the history
  • Loading branch information
G-Rath committed Aug 27, 2022
1 parent 5508c95 commit 362d7e6
Show file tree
Hide file tree
Showing 7 changed files with 474 additions and 14 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -226,6 +226,7 @@ installations requiring long-term consistency.
| [no-test-return-statement](docs/rules/no-test-return-statement.md) | Disallow explicitly returning from tests | | |
| [prefer-called-with](docs/rules/prefer-called-with.md) | Suggest using `toBeCalledWith()` or `toHaveBeenCalledWith()` | | |
| [prefer-comparison-matcher](docs/rules/prefer-comparison-matcher.md) | Suggest using the built-in comparison matchers | | ![fixable][] |
| [prefer-each](docs/rules/prefer-each.md) | Prefer using `.each` rather than manual loops | | |
| [prefer-equality-matcher](docs/rules/prefer-equality-matcher.md) | Suggest using the built-in equality matchers | | ![suggest][] |
| [prefer-expect-assertions](docs/rules/prefer-expect-assertions.md) | Suggest using `expect.assertions()` OR `expect.hasAssertions()` | | ![suggest][] |
| [prefer-expect-resolves](docs/rules/prefer-expect-resolves.md) | Prefer `await expect(...).resolves` over `expect(await ...)` syntax | | ![fixable][] |
Expand Down
54 changes: 54 additions & 0 deletions docs/rules/prefer-each.md
@@ -0,0 +1,54 @@
# Prefer using `.each` rather than manual loops (`prefer-each`)

Reports where you might be able to use `.each` instead of native loops.

## Rule details

This rule triggers a warning if you use test case functions like `describe`,
`test`, and `it`, in a native loop - generally you should be able to use `.each`
instead which gives better output and makes it easier to run specific cases.

Examples of **incorrect** code for this rule:

```js
for (const number of getNumbers()) {
it('is greater than five', function () {
expect(number).toBeGreaterThan(5);
});
}

for (const [input, expected] of data) {
beforeEach(() => setupSomething(input));

test(`results in ${expected}`, () => {
expect(doSomething()).toBe(expected);
});
}
```

Examples of **correct** code for this rule:

```js
it.each(getNumbers())(
'only returns numbers that are greater than seven',
number => {
expect(number).toBeGreaterThan(7);
},
);

describe.each(data)('when input is %s', ([input, expected]) => {
beforeEach(() => setupSomething(input));

test(`results in ${expected}`, () => {
expect(doSomething()).toBe(expected);
});
});

// we don't warn on loops _in_ test functions because those typically involve
// complex setup that is better done in the test function itself
it('returns numbers that are greater than five', () => {
for (const number of getNumbers()) {
expect(number).toBeGreaterThan(5);
}
});
```
25 changes: 13 additions & 12 deletions src/__tests__/__snapshots__/rules.test.ts.snap
@@ -1,15 +1,15 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`rules should export configs that refer to actual rules 1`] = `
Object {
"all": Object {
"env": Object {
{
"all": {
"env": {
"jest/globals": true,
},
"plugins": Array [
"plugins": [
"jest",
],
"rules": Object {
"rules": {
"jest/consistent-test-it": "error",
"jest/expect-expect": "error",
"jest/max-expects": "error",
Expand Down Expand Up @@ -37,6 +37,7 @@ Object {
"jest/no-test-return-statement": "error",
"jest/prefer-called-with": "error",
"jest/prefer-comparison-matcher": "error",
"jest/prefer-each": "error",
"jest/prefer-equality-matcher": "error",
"jest/prefer-expect-assertions": "error",
"jest/prefer-expect-resolves": "error",
Expand All @@ -61,14 +62,14 @@ Object {
"jest/valid-title": "error",
},
},
"recommended": Object {
"env": Object {
"recommended": {
"env": {
"jest/globals": true,
},
"plugins": Array [
"plugins": [
"jest",
],
"rules": Object {
"rules": {
"jest/expect-expect": "warn",
"jest/no-commented-out-tests": "warn",
"jest/no-conditional-expect": "error",
Expand All @@ -90,11 +91,11 @@ Object {
"jest/valid-title": "error",
},
},
"style": Object {
"plugins": Array [
"style": {
"plugins": [
"jest",
],
"rules": Object {
"rules": {
"jest/no-alias-methods": "warn",
"jest/prefer-to-be": "error",
"jest/prefer-to-contain": "error",
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/rules.test.ts
Expand Up @@ -2,7 +2,7 @@ import { existsSync } from 'fs';
import { resolve } from 'path';
import plugin from '../';

const numberOfRules = 50;
const numberOfRules = 51;
const ruleNames = Object.keys(plugin.rules);
const deprecatedRules = Object.entries(plugin.rules)
.filter(([, rule]) => rule.meta.deprecated)
Expand Down

0 comments on commit 362d7e6

Please sign in to comment.