From b4b7cbc6195b47ba032fcf9ef1443de6b851d42b Mon Sep 17 00:00:00 2001 From: Gareth Jones Date: Sun, 7 Apr 2024 10:51:25 +1200 Subject: [PATCH] fix(max-expects): properly reset counter when exiting a test case (#1550) --- src/rules/__tests__/max-expects.test.ts | 229 ++++++++++++++++++++++++ src/rules/max-expects.ts | 4 + 2 files changed, 233 insertions(+) diff --git a/src/rules/__tests__/max-expects.test.ts b/src/rules/__tests__/max-expects.test.ts index e629c5695..c5aaabc1d 100644 --- a/src/rules/__tests__/max-expects.test.ts +++ b/src/rules/__tests__/max-expects.test.ts @@ -119,6 +119,39 @@ ruleTester.run('max-expects', rule, { expect(true).toBeDefined(); }); `, + dedent` + test('should not pass', () => { + const checkValue = (value) => { + expect(value).toBeDefined(); + expect(value).toBeDefined(); + }; + + checkValue(true); + }); + test('should not pass', () => { + expect(true).toBeDefined(); + expect(true).toBeDefined(); + expect(true).toBeDefined(); + }); + `, + dedent` + test('should not pass', done => { + emitter.on('event', value => { + expect(value).toBeDefined(); + expect(value).toBeDefined(); + expect(value).toBeDefined(); + expect(value).toBeDefined(); + + done(); + }); + }); + test('should not pass', () => { + expect(true).toBeDefined(); + expect(true).toBeDefined(); + expect(true).toBeDefined(); + expect(true).toBeDefined(); + }); + `, dedent` function myHelper() { expect(true).toBeDefined(); @@ -204,6 +237,24 @@ ruleTester.run('max-expects', rule, { }, ], }, + { + code: dedent` + describe('given decimal places', () => { + it("test 1", fakeAsync(() => { + expect(true).toBeTrue(); + expect(true).toBeTrue(); + expect(true).toBeTrue(); + })) + + it("test 2", fakeAsync(() => { + expect(true).toBeTrue(); + expect(true).toBeTrue(); + expect(true).toBeTrue(); + })) + }) + `, + options: [{ max: 5 }], + }, ], invalid: [ { @@ -314,6 +365,184 @@ ruleTester.run('max-expects', rule, { }, ], }, + { + code: dedent` + test('should not pass', () => { + const checkValue = (value) => { + expect(value).toBeDefined(); + expect(value).toBeDefined(); + }; + + checkValue(true); + }); + test('should not pass', () => { + expect(true).toBeDefined(); + expect(true).toBeDefined(); + expect(true).toBeDefined(); + }); + `, + options: [{ max: 1 }], + errors: [ + { + messageId: 'exceededMaxAssertion', + line: 4, + column: 5, + }, + { + messageId: 'exceededMaxAssertion', + line: 11, + column: 3, + }, + { + messageId: 'exceededMaxAssertion', + line: 12, + column: 3, + }, + ], + }, + { + code: dedent` + test('should not pass', () => { + const checkValue = (value) => { + expect(value).toBeDefined(); + expect(value).toBeDefined(); + }; + + checkValue(true); + }); + test('should not pass', () => { + expect(true).toBeDefined(); + expect(true).toBeDefined(); + expect(true).toBeDefined(); + }); + `, + options: [{ max: 2 }], + errors: [ + { + messageId: 'exceededMaxAssertion', + line: 12, + column: 3, + }, + ], + }, + { + code: dedent` + test('should not pass', () => { + const checkValue = (value) => { + expect(value).toBeDefined(); + expect(value).toBeDefined(); + }; + + expect(value).toBeDefined(); + checkValue(true); + }); + test('should not pass', () => { + expect(true).toBeDefined(); + expect(true).toBeDefined(); + expect(true).toBeDefined(); + }); + `, + options: [{ max: 2 }], + errors: [ + { + messageId: 'exceededMaxAssertion', + line: 13, + column: 3, + }, + ], + }, + { + code: dedent` + test('should not pass', done => { + emitter.on('event', value => { + expect(value).toBeDefined(); + expect(value).toBeDefined(); + + done(); + }); + }); + test('should not pass', () => { + expect(true).toBeDefined(); + expect(true).toBeDefined(); + expect(true).toBeDefined(); + }); + `, + options: [{ max: 1 }], + errors: [ + { + messageId: 'exceededMaxAssertion', + line: 4, + column: 5, + }, + { + messageId: 'exceededMaxAssertion', + line: 11, + column: 3, + }, + { + messageId: 'exceededMaxAssertion', + line: 12, + column: 3, + }, + ], + }, + { + code: dedent` + test('should not pass', done => { + emitter.on('event', value => { + expect(value).toBeDefined(); + expect(value).toBeDefined(); + + done(); + }); + }); + test('should not pass', () => { + expect(true).toBeDefined(); + expect(true).toBeDefined(); + expect(true).toBeDefined(); + }); + `, + options: [{ max: 2 }], + errors: [ + { + messageId: 'exceededMaxAssertion', + line: 12, + column: 3, + }, + ], + }, + { + code: dedent` + describe('given decimal places', () => { + it("test 1", fakeAsync(() => { + expect(true).toBeTrue(); + expect(true).toBeTrue(); + expect(true).toBeTrue(); + })) + + it("test 2", fakeAsync(() => { + expect(true).toBeTrue(); + expect(true).toBeTrue(); + expect(true).toBeTrue(); + expect(true).toBeTrue(); + expect(true).toBeTrue(); + })) + }) + `, + options: [{ max: 3 }], + errors: [ + { + messageId: 'exceededMaxAssertion', + line: 12, + column: 5, + }, + { + messageId: 'exceededMaxAssertion', + line: 13, + column: 5, + }, + ], + }, { code: dedent` describe('test', () => { diff --git a/src/rules/max-expects.ts b/src/rules/max-expects.ts index ce62a2bc1..3c2a615fe 100644 --- a/src/rules/max-expects.ts +++ b/src/rules/max-expects.ts @@ -52,6 +52,10 @@ export default createRule({ CallExpression(node) { const jestFnCall = parseJestFnCall(node, context); + if (jestFnCall?.type === 'test') { + count = 0; + } + if ( jestFnCall?.type !== 'expect' || jestFnCall.head.node.parent?.type === AST_NODE_TYPES.MemberExpression