-
Notifications
You must be signed in to change notification settings - Fork 248
feat: create new no-unneeded-async-expect-function rule
#1863
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
G-Rath
merged 10 commits into
jest-community:main
from
hainenber:feat/no-async-for-expected-promises
Dec 14, 2025
+346
−1
Merged
Changes from 1 commit
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
c497904
feat(rule): disallow unnecessary async function wrapper for single `a…
hainenber c7e1d1e
chore: regenerate docs for changed error message
hainenber b23a00b
chore: remove from recommended config
hainenber 36e4235
chore: correct indentation for test code samples + use isFunction uti…
hainenber 0bf81a6
chore: add valid test case
hainenber 3e890b2
Merge branch 'main' into feat/no-async-for-expected-promises
hainenber 3f1d42d
chore: resolve merge conflict with `main` branch
hainenber 6c023de
chore: correct English grammar for invalid test case title
hainenber 785c04c
chore: resolve lint issues
hainenber 65ba578
feat: add tests to cover more invalid/valid cases + rename rule
hainenber File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| # Disallow unnecessary async wrapper for expected promises (`no-async-wrapper-for-expected-promise`) | ||
|
|
||
| 💼 This rule is enabled in the ✅ `recommended` | ||
| [config](https://github.com/jest-community/eslint-plugin-jest/blob/main/README.md#shareable-configurations). | ||
|
|
||
| 🔧 This rule is automatically fixable by the | ||
| [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix). | ||
|
|
||
| <!-- end auto-generated rule header --> | ||
|
|
||
| `Jest` can handle fulfilled/rejected promisified function call normally but | ||
| occassionally, engineers wrap said function in another `async` function that is | ||
| excessively verbose and make the tests harder to read. | ||
|
|
||
| ## Rule details | ||
|
|
||
| This rule triggers a warning if a single `await` function call is wrapped by an | ||
| unnecessary `async` function. | ||
|
|
||
| Examples of **incorrect** code for this rule | ||
|
|
||
| ```js | ||
| it('wrong1', async () => { | ||
| await expect(async () => { | ||
| await doSomethingAsync(); | ||
| }).rejects.toThrow(); | ||
| }); | ||
|
|
||
| it('wrong2', async () => { | ||
| await expect(async function () { | ||
| await doSomethingAsync(); | ||
| }).rejects.toThrow(); | ||
| }); | ||
| ``` | ||
|
|
||
| Examples of **correct** code for this rule | ||
|
|
||
| ```js | ||
| it('right1', async () => { | ||
| await expect(doSomethingAsync()).rejects.toThrow(); | ||
| }); | ||
| ``` | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
141 changes: 141 additions & 0 deletions
141
src/rules/__tests__/no-async-wrapper-for-expected-promise.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,141 @@ | ||
| import dedent from 'dedent'; | ||
| import rule from '../no-async-wrapper-for-expected-promise'; | ||
| import { FlatCompatRuleTester as RuleTester, espreeParser } from './test-utils'; | ||
|
|
||
| const ruleTester = new RuleTester({ | ||
| parser: espreeParser, | ||
| parserOptions: { | ||
| ecmaVersion: 2017, | ||
| }, | ||
| }); | ||
|
|
||
| ruleTester.run('no-async-wrapper-for-expected-promise', rule, { | ||
| valid: [ | ||
| 'expect.hasAssertions()', | ||
| dedent` | ||
| it('pass', async () => { | ||
| expect(); | ||
| }) | ||
| `, | ||
| dedent` | ||
| it('pass', async () => { | ||
| await expect(doSomethingAsync()).rejects.toThrow(); | ||
| }) | ||
| `, | ||
| dedent` | ||
| it('pass', async () => { | ||
| await expect(doSomethingAsync(1, 2)).resolves.toBe(1); | ||
| }) | ||
| `, | ||
| dedent` | ||
| it('pass', async () => { | ||
| await expect(async () => { | ||
| await doSomethingAsync(); | ||
| await doSomethingTwiceAsync(1, 2); | ||
| }).rejects.toThrow(); | ||
| }) | ||
| `, | ||
| { | ||
| code: dedent` | ||
| import { expect as pleaseExpect } from '@jest/globals'; | ||
| it('pass', async () => { | ||
| await pleaseExpect(doSomethingAsync()).rejects.toThrow(); | ||
| }) | ||
| `, | ||
| parserOptions: { sourceType: 'module' }, | ||
| }, | ||
| dedent` | ||
| it('pass', async () => { | ||
| await expect(async () => { | ||
| doSomethingSync(); | ||
| }).rejects.toThrow(); | ||
| }) | ||
| `, | ||
| ], | ||
| invalid: [ | ||
| { | ||
| code: dedent` | ||
| it('should be fix', async () => { | ||
|
||
| await expect(async () => { | ||
| await doSomethingAsync(); | ||
| }).rejects.toThrow(); | ||
| }) | ||
| `, | ||
| output: dedent` | ||
| it('should be fix', async () => { | ||
| await expect(doSomethingAsync()).rejects.toThrow(); | ||
| }) | ||
| `, | ||
| errors: [ | ||
| { | ||
| endColumn: 6, | ||
| column: 18, | ||
| messageId: 'noAsyncWrapperForExpectedPromise', | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| code: dedent` | ||
| it('should be fix', async () => { | ||
| await expect(async function () { | ||
| await doSomethingAsync(); | ||
| }).rejects.toThrow(); | ||
| }) | ||
| `, | ||
| output: dedent` | ||
| it('should be fix', async () => { | ||
| await expect(doSomethingAsync()).rejects.toThrow(); | ||
| }) | ||
| `, | ||
| errors: [ | ||
| { | ||
| endColumn: 6, | ||
| column: 18, | ||
| messageId: 'noAsyncWrapperForExpectedPromise', | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| code: dedent` | ||
| it('should be fix', async () => { | ||
| await expect(async () => { | ||
| await doSomethingAsync(1, 2); | ||
| }).rejects.toThrow(); | ||
| }) | ||
| `, | ||
| output: dedent` | ||
| it('should be fix', async () => { | ||
| await expect(doSomethingAsync(1, 2)).rejects.toThrow(); | ||
| }) | ||
| `, | ||
| errors: [ | ||
| { | ||
| endColumn: 6, | ||
| column: 18, | ||
| messageId: 'noAsyncWrapperForExpectedPromise', | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| code: dedent` | ||
| it('should be fix', async () => { | ||
| await expect(async function () { | ||
| await doSomethingAsync(1, 2); | ||
| }).rejects.toThrow(); | ||
| }) | ||
| `, | ||
| output: dedent` | ||
| it('should be fix', async () => { | ||
| await expect(doSomethingAsync(1, 2)).rejects.toThrow(); | ||
| }) | ||
| `, | ||
| errors: [ | ||
| { | ||
| endColumn: 6, | ||
| column: 18, | ||
| messageId: 'noAsyncWrapperForExpectedPromise', | ||
| }, | ||
| ], | ||
| }, | ||
| ], | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| import { AST_NODE_TYPES, type TSESTree } from '@typescript-eslint/utils'; | ||
| import { createRule, parseJestFnCall } from './utils'; | ||
|
|
||
| export default createRule({ | ||
| name: __filename, | ||
| meta: { | ||
| docs: { | ||
| description: | ||
| 'Disallow unnecessary async function wrapper for expected promises', | ||
| }, | ||
| fixable: 'code', | ||
| messages: { | ||
| noAsyncWrapperForExpectedPromise: | ||
| 'Rejected/resolved promises should not be wrapped in async function', | ||
G-Rath marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| }, | ||
| schema: [], | ||
| type: 'suggestion', | ||
| }, | ||
| defaultOptions: [], | ||
| create(context) { | ||
| return { | ||
| CallExpression(node: TSESTree.CallExpression) { | ||
| const jestFnCall = parseJestFnCall(node, context); | ||
|
|
||
| if (jestFnCall?.type !== 'expect') { | ||
| return; | ||
| } | ||
|
|
||
| const { parent } = jestFnCall.head.node; | ||
|
|
||
| if (parent?.type !== AST_NODE_TYPES.CallExpression) { | ||
| return; | ||
| } | ||
|
|
||
| const [awaitNode] = parent.arguments; | ||
|
|
||
| if ( | ||
| (awaitNode?.type !== AST_NODE_TYPES.ArrowFunctionExpression && | ||
| awaitNode?.type !== AST_NODE_TYPES.FunctionExpression) || | ||
G-Rath marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| !awaitNode?.async || | ||
| awaitNode.body.type !== AST_NODE_TYPES.BlockStatement || | ||
| awaitNode.body.body.length !== 1 | ||
| ) { | ||
| return; | ||
| } | ||
|
|
||
| const [callback] = awaitNode.body.body; | ||
|
|
||
| if ( | ||
| callback.type === AST_NODE_TYPES.ExpressionStatement && | ||
| callback.expression.type === AST_NODE_TYPES.AwaitExpression && | ||
| callback.expression.argument.type === AST_NODE_TYPES.CallExpression | ||
| ) { | ||
| const innerAsyncFuncCall = callback.expression.argument; | ||
|
|
||
| context.report({ | ||
| node: awaitNode, | ||
| messageId: 'noAsyncWrapperForExpectedPromise', | ||
| fix(fixer) { | ||
| const { sourceCode } = context; | ||
|
|
||
| return [ | ||
| fixer.replaceTextRange( | ||
| [awaitNode.range[0], awaitNode.range[1]], | ||
| sourceCode.getText(innerAsyncFuncCall), | ||
| ), | ||
| ]; | ||
| }, | ||
| }); | ||
| } | ||
| }, | ||
| }; | ||
| }, | ||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it would be better to say something like "This rule triggers a warning if
expectis passed anasyncfunction that has a singleawaitcall", since that is what "unnecessary" means in this contextThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is done in HEAD commit