Skip to content

Commit

Permalink
feat(prefer-expect-resolves): add fixer
Browse files Browse the repository at this point in the history
  • Loading branch information
G-Rath authored and SimenB committed Sep 29, 2021
1 parent 29e6820 commit 1a34267
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,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-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 | | |
| [prefer-expect-resolves](docs/rules/prefer-expect-resolves.md) | Prefer `await expect(...).resolves` over `expect(await ...)` syntax | | ![fixable][] |
| [prefer-hooks-on-top](docs/rules/prefer-hooks-on-top.md) | Suggest having hooks before any test cases | | |
| [prefer-spy-on](docs/rules/prefer-spy-on.md) | Suggest using `jest.spyOn()` | | ![fixable][] |
| [prefer-strict-equal](docs/rules/prefer-strict-equal.md) | Suggest using `toStrictEqual()` | | ![suggest][] |
Expand Down
12 changes: 12 additions & 0 deletions src/rules/__tests__/prefer-expect-resolves.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ ruleTester.run('prefer-expect-resolves', rule, {
expect(await someValue()).toBe(true);
});
`,
output: dedent`
it('passes', async () => {
await expect(someValue()).resolves.toBe(true);
});
`,
errors: [{ endColumn: 27, column: 10, messageId: 'expectResolves' }],
},
{
Expand All @@ -47,6 +52,13 @@ ruleTester.run('prefer-expect-resolves', rule, {
expect(await myPromise).toBe(true);
});
`,
output: dedent`
it('is true', async () => {
const myPromise = Promise.resolve(true);
await expect(myPromise).resolves.toBe(true);
});
`,
errors: [{ endColumn: 25, column: 10, messageId: 'expectResolves' }],
},
],
Expand Down
16 changes: 14 additions & 2 deletions src/rules/prefer-expect-resolves.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export default createRule({
'Prefer `await expect(...).resolves` over `expect(await ...)` syntax',
recommended: false,
},
fixable: 'code',
messages: {
expectResolves: 'Use `await expect(...).resolves instead.',
},
Expand All @@ -22,14 +23,25 @@ export default createRule({
defaultOptions: [],
create: context => ({
CallExpression(node: TSESTree.CallExpression) {
const [awaitNode] = node.arguments;

if (
isExpectCall(node) &&
node.arguments.length &&
node.arguments[0].type === AST_NODE_TYPES.AwaitExpression
awaitNode?.type === AST_NODE_TYPES.AwaitExpression
) {
context.report({
node: node.arguments[0],
messageId: 'expectResolves',
fix(fixer) {
return [
fixer.insertTextBefore(node, 'await '),
fixer.removeRange([
awaitNode.range[0],
awaitNode.argument.range[0],
]),
fixer.insertTextAfter(node, '.resolves'),
];
},
});
}
},
Expand Down

0 comments on commit 1a34267

Please sign in to comment.