diff --git a/src/rules/__tests__/max-expects.test.ts b/src/rules/__tests__/max-expects.test.ts index 25750aa25..17a2853fb 100644 --- a/src/rules/__tests__/max-expects.test.ts +++ b/src/rules/__tests__/max-expects.test.ts @@ -120,6 +120,70 @@ ruleTester.run('max-expects', rule, { expect(true).toBeDefined(); }); `, + dedent` + function myHelper() { + expect(true).toBeDefined(); + expect(true).toBeDefined(); + expect(true).toBeDefined(); + }; + + test('should pass', () => { + expect(true).toBeDefined(); + expect(true).toBeDefined(); + expect(true).toBeDefined(); + }); + `, + dedent` + function myHelper1() { + expect(true).toBeDefined(); + expect(true).toBeDefined(); + expect(true).toBeDefined(); + }; + + test('should pass', () => { + expect(true).toBeDefined(); + expect(true).toBeDefined(); + expect(true).toBeDefined(); + }); + + function myHelper2() { + expect(true).toBeDefined(); + expect(true).toBeDefined(); + expect(true).toBeDefined(); + }; + `, + dedent` + test('should pass', () => { + expect(true).toBeDefined(); + expect(true).toBeDefined(); + expect(true).toBeDefined(); + }); + + function myHelper() { + expect(true).toBeDefined(); + expect(true).toBeDefined(); + expect(true).toBeDefined(); + }; + `, + dedent` + const myHelper1 = () => { + expect(true).toBeDefined(); + expect(true).toBeDefined(); + expect(true).toBeDefined(); + }; + + test('should pass', function() { + expect(true).toBeDefined(); + expect(true).toBeDefined(); + expect(true).toBeDefined(); + }); + + const myHelper2 = function() { + expect(true).toBeDefined(); + expect(true).toBeDefined(); + expect(true).toBeDefined(); + }; + `, { code: dedent` test('should pass', () => { diff --git a/src/rules/max-expects.ts b/src/rules/max-expects.ts index ec221a8b6..cf891b81d 100644 --- a/src/rules/max-expects.ts +++ b/src/rules/max-expects.ts @@ -36,7 +36,7 @@ export default createRule({ create(context, [{ max }]) { let count = 0; - const onFunctionExpressionEnter = (node: FunctionExpression) => { + const maybeResetCount = (node: FunctionExpression) => { const isTestFn = node.parent?.type !== AST_NODE_TYPES.CallExpression || isTypeOfJestFnCall(node.parent, context, ['test']); @@ -47,8 +47,10 @@ export default createRule({ }; return { - FunctionExpression: onFunctionExpressionEnter, - ArrowFunctionExpression: onFunctionExpressionEnter, + FunctionExpression: maybeResetCount, + 'FunctionExpression:exit': maybeResetCount, + ArrowFunctionExpression: maybeResetCount, + 'ArrowFunctionExpression:exit': maybeResetCount, CallExpression(node) { const jestFnCall = parseJestFnCall(node, context);