eslint-plugin-jest 29.4.1 → 29.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -351,6 +351,7 @@ Manually fixable by
351
351
  | [no-standalone-expect](docs/rules/no-standalone-expect.md) | Disallow using `expect` outside of `it` or `test` blocks | ✅ | | | |
352
352
  | [no-test-prefixes](docs/rules/no-test-prefixes.md) | Require using `.only` and `.skip` over `f` and `x` | ✅ | | 🔧 | |
353
353
  | [no-test-return-statement](docs/rules/no-test-return-statement.md) | Disallow explicitly returning from tests | | | | |
354
+ | [no-unneeded-async-expect-function](docs/rules/no-unneeded-async-expect-function.md) | Disallow unnecessary async function wrapper for expected promises | | | 🔧 | |
354
355
  | [no-untyped-mock-factory](docs/rules/no-untyped-mock-factory.md) | Disallow using `jest.mock()` factories without an explicit type parameter | | | 🔧 | |
355
356
  | [padding-around-after-all-blocks](docs/rules/padding-around-after-all-blocks.md) | Enforce padding around `afterAll` blocks | | | 🔧 | |
356
357
  | [padding-around-after-each-blocks](docs/rules/padding-around-after-each-blocks.md) | Enforce padding around `afterEach` blocks | | | 🔧 | |
@@ -0,0 +1,39 @@
1
+ # Disallow unnecessary async function wrapper for expected promises (`no-unneeded-async-expect-function`)
2
+
3
+ 🔧 This rule is automatically fixable by the
4
+ [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix).
5
+
6
+ <!-- end auto-generated rule header -->
7
+
8
+ `Jest` can handle fulfilled/rejected promisified function call normally but
9
+ occassionally, engineers wrap said function in another `async` function that is
10
+ excessively verbose and make the tests harder to read.
11
+
12
+ ## Rule details
13
+
14
+ This rule triggers a warning if `expect` is passed with an an `async` function
15
+ that has a single `await` call.
16
+
17
+ Examples of **incorrect** code for this rule
18
+
19
+ ```js
20
+ it('wrong1', async () => {
21
+ await expect(async () => {
22
+ await doSomethingAsync();
23
+ }).rejects.toThrow();
24
+ });
25
+
26
+ it('wrong2', async () => {
27
+ await expect(async function () {
28
+ await doSomethingAsync();
29
+ }).rejects.toThrow();
30
+ });
31
+ ```
32
+
33
+ Examples of **correct** code for this rule
34
+
35
+ ```js
36
+ it('right1', async () => {
37
+ await expect(doSomethingAsync()).rejects.toThrow();
38
+ });
39
+ ```
@@ -0,0 +1,57 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+ var _utils = require("@typescript-eslint/utils");
8
+ var _utils2 = require("./utils");
9
+ var _default = exports.default = (0, _utils2.createRule)({
10
+ name: __filename,
11
+ meta: {
12
+ docs: {
13
+ description: 'Disallow unnecessary async function wrapper for expected promises'
14
+ },
15
+ fixable: 'code',
16
+ messages: {
17
+ noAsyncWrapperForExpectedPromise: 'Unnecessary async function wrapper'
18
+ },
19
+ schema: [],
20
+ type: 'suggestion'
21
+ },
22
+ defaultOptions: [],
23
+ create(context) {
24
+ return {
25
+ CallExpression(node) {
26
+ const jestFnCall = (0, _utils2.parseJestFnCall)(node, context);
27
+ if (jestFnCall?.type !== 'expect') {
28
+ return;
29
+ }
30
+ const {
31
+ parent
32
+ } = jestFnCall.head.node;
33
+ if (parent?.type !== _utils.AST_NODE_TYPES.CallExpression) {
34
+ return;
35
+ }
36
+ const [awaitNode] = parent.arguments;
37
+ if (!awaitNode || !(0, _utils2.isFunction)(awaitNode) || !awaitNode?.async || awaitNode.body.type !== _utils.AST_NODE_TYPES.BlockStatement || awaitNode.body.body.length !== 1) {
38
+ return;
39
+ }
40
+ const [callback] = awaitNode.body.body;
41
+ if (callback.type === _utils.AST_NODE_TYPES.ExpressionStatement && callback.expression.type === _utils.AST_NODE_TYPES.AwaitExpression && callback.expression.argument.type === _utils.AST_NODE_TYPES.CallExpression) {
42
+ const innerAsyncFuncCall = callback.expression.argument;
43
+ context.report({
44
+ node: awaitNode,
45
+ messageId: 'noAsyncWrapperForExpectedPromise',
46
+ fix(fixer) {
47
+ const {
48
+ sourceCode
49
+ } = context;
50
+ return [fixer.replaceTextRange([awaitNode.range[0], awaitNode.range[1]], sourceCode.getText(innerAsyncFuncCall))];
51
+ }
52
+ });
53
+ }
54
+ }
55
+ };
56
+ }
57
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-jest",
3
- "version": "29.4.1",
3
+ "version": "29.5.0",
4
4
  "description": "ESLint rules for Jest",
5
5
  "keywords": [
6
6
  "eslint",