From 1a342673d85c92213aa4073e5d5c2004bfee6d1b Mon Sep 17 00:00:00 2001 From: Gareth Jones Date: Fri, 16 Jul 2021 09:31:01 +1200 Subject: [PATCH] feat(prefer-expect-resolves): add fixer --- README.md | 2 +- .../__tests__/prefer-expect-resolves.test.ts | 12 ++++++++++++ src/rules/prefer-expect-resolves.ts | 16 ++++++++++++++-- 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 0465b39c8..44e2c5cdc 100644 --- a/README.md +++ b/README.md @@ -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][] | diff --git a/src/rules/__tests__/prefer-expect-resolves.test.ts b/src/rules/__tests__/prefer-expect-resolves.test.ts index c76e99f53..0eeb1be84 100644 --- a/src/rules/__tests__/prefer-expect-resolves.test.ts +++ b/src/rules/__tests__/prefer-expect-resolves.test.ts @@ -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' }], }, { @@ -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' }], }, ], diff --git a/src/rules/prefer-expect-resolves.ts b/src/rules/prefer-expect-resolves.ts index d4802fb62..dd644d11d 100644 --- a/src/rules/prefer-expect-resolves.ts +++ b/src/rules/prefer-expect-resolves.ts @@ -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.', }, @@ -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'), + ]; + }, }); } },