Skip to content

Commit f41d5c4

Browse files
eranshabiSimenB
authored andcommitted
feat(rules): no-expect-resolves (#364)
1 parent a334368 commit f41d5c4

File tree

5 files changed

+96
-1
lines changed

5 files changed

+96
-1
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ installations requiring long-term consistency.
114114
| [no-disabled-tests][] | Disallow disabled tests | ![recommended][] | |
115115
| [no-duplicate-hooks][] | Disallow duplicate hooks within a `describe` block | | |
116116
| [no-empty-title][] | Disallow empty titles | | |
117+
| [no-expect-resolves][] | Disallow using `expect().resolves` | | |
117118
| [no-export][] | Disallow export from test files | | |
118119
| [no-focused-tests][] | Disallow focused tests | ![recommended][] | |
119120
| [no-hooks][] | Disallow setup and teardown hooks | | |
@@ -166,6 +167,7 @@ https://github.com/dangreenisrael/eslint-plugin-jest-formatting
166167
[no-disabled-tests]: docs/rules/no-disabled-tests.md
167168
[no-duplicate-hooks]: docs/rules/no-duplicate-hooks.md
168169
[no-empty-title]: docs/rules/no-empty-title.md
170+
[no-expect-resolves]: docs/rules/no-expect-resolves.md
169171
[no-export]: docs/rules/no-export.md
170172
[no-focused-tests]: docs/rules/no-focused-tests.md
171173
[no-hooks]: docs/rules/no-hooks.md

docs/rules/no-expect-resolves.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Avoid using `expect().resolves` (no-expect-resolves)
2+
3+
Jest allows you to test a promise resolve value using `await expect().resolves`.
4+
For consistency and readability this rule bans `expect().resolves` in favor of
5+
`expect(await promise)`.
6+
7+
## Rule details
8+
9+
This rule triggers a warning if `expect().resolves` is used.
10+
11+
This rule is disabled by default.
12+
13+
### Default configuration
14+
15+
The following patterns is considered warning:
16+
17+
```js
18+
test('some test', async () => {
19+
await expect(Promise.resolve(1)).resolves.toBe(1);
20+
});
21+
```
22+
23+
The following pattern is not considered warning:
24+
25+
```js
26+
test('some test', async () => {
27+
expect(await Promise.resolve(1)).toBe(1);
28+
});
29+
```

src/__tests__/rules.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { resolve } from 'path';
44
import { rules } from '../';
55

66
const ruleNames = Object.keys(rules);
7-
const numberOfRules = 37;
7+
const numberOfRules = 38;
88

99
describe('rules', () => {
1010
it('should have a corresponding doc for each rule', () => {
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { TSESLint } from '@typescript-eslint/experimental-utils';
2+
import rule from '../no-expect-resolves';
3+
4+
const ruleTester = new TSESLint.RuleTester({
5+
parserOptions: {
6+
ecmaVersion: 2017,
7+
},
8+
});
9+
10+
ruleTester.run('no-expect-resolves', rule, {
11+
valid: [
12+
`test('some test', async () => {
13+
expect(await Promise.resolve(1)).toBe(1);
14+
});`,
15+
],
16+
invalid: [
17+
{
18+
code: `test('some test', async () => {
19+
await expect(Promise.resolve(1)).resolves.toBe(1);
20+
});`,
21+
errors: [{ endColumn: 55, column: 47, messageId: 'expectResolves' }],
22+
},
23+
],
24+
});

src/rules/no-expect-resolves.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import {
2+
AST_NODE_TYPES,
3+
TSESTree,
4+
} from '@typescript-eslint/experimental-utils';
5+
import { createRule, isExpectCall } from './tsUtils';
6+
7+
function isIdentifierResolves(node: TSESTree.MemberExpression) {
8+
return (
9+
node.property.type === AST_NODE_TYPES.Identifier &&
10+
node.property.name === 'resolves'
11+
);
12+
}
13+
14+
function isExpectResolves(node: TSESTree.MemberExpression) {
15+
return isExpectCall(node.object) && isIdentifierResolves(node);
16+
}
17+
18+
export default createRule({
19+
name: __filename,
20+
meta: {
21+
docs: {
22+
category: 'Best Practices',
23+
description: 'Disallow expect.resolves',
24+
recommended: false,
25+
},
26+
messages: {
27+
expectResolves: 'Use `expect(await promise)` instead.',
28+
},
29+
schema: [],
30+
type: 'suggestion',
31+
},
32+
defaultOptions: [],
33+
create: context => ({
34+
MemberExpression(node) {
35+
if (isExpectResolves(node)) {
36+
context.report({ node: node.property, messageId: 'expectResolves' });
37+
}
38+
},
39+
}),
40+
});

0 commit comments

Comments
 (0)