From 1b15b2a47ea10e51a5fc567732c58589a93e9c0c Mon Sep 17 00:00:00 2001 From: Muaz Othman Date: Wed, 11 Oct 2023 10:59:24 -0500 Subject: [PATCH] Fixed some unit tests --- .../e2e/util/mocha_custom_methods.cy.js | 48 ++++++++++++------- packages/driver/src/cypress/mocha.ts | 18 +++---- 2 files changed, 41 insertions(+), 25 deletions(-) diff --git a/packages/driver/cypress/e2e/util/mocha_custom_methods.cy.js b/packages/driver/cypress/e2e/util/mocha_custom_methods.cy.js index f5e178f0c5c9..b8ff80a05f32 100644 --- a/packages/driver/cypress/e2e/util/mocha_custom_methods.cy.js +++ b/packages/driver/cypress/e2e/util/mocha_custom_methods.cy.js @@ -29,7 +29,8 @@ describe('mocha custom methods', () => { expect(undefinedStrategyTest.final).to.be.true const detectFlakeAndPassOnThresholdStrategyTest = createMockTest() - const detectFlakeAndPassOnThreshold = calculateTestStatus(detectFlakeAndPassOnThresholdStrategyTest, 'detect-flake-and-pass-on-threshold', { + const detectFlakeAndPassOnThreshold = calculateTestStatus(detectFlakeAndPassOnThresholdStrategyTest, { + strategy: 'detect-flake-and-pass-on-threshold', maxRetries: 8, passesRequired: 5, }) @@ -41,7 +42,8 @@ describe('mocha custom methods', () => { expect(detectFlakeAndPassOnThresholdStrategyTest.final).to.be.true const detectFlakeButAlwaysFailStrategyTest = createMockTest() - const detectFlakeButAlwaysFail = calculateTestStatus(detectFlakeButAlwaysFailStrategyTest, 'detect-flake-but-always-fail', { + const detectFlakeButAlwaysFail = calculateTestStatus(detectFlakeButAlwaysFailStrategyTest, { + strategy: 'detect-flake-but-always-fail', maxRetries: 8, stopIfAnyPassed: false, }) @@ -105,7 +107,8 @@ describe('mocha custom methods', () => { it('passed: no longer signals to retry test after passesRequired threshold is reached', function () { totalRetries = 5 const mockTest1 = createMockTest('failed') - const attempt1 = calculateTestStatus(mockTest1, 'detect-flake-and-pass-on-threshold', { + const attempt1 = calculateTestStatus(mockTest1, { + strategy: 'detect-flake-and-pass-on-threshold', maxRetries: totalRetries, passesRequired: 2, }) @@ -117,7 +120,8 @@ describe('mocha custom methods', () => { expect(mockTest1.final).to.be.false const mockTest2 = createMockTest('failed', [mockTest1]) - const attempt2 = calculateTestStatus(mockTest2, 'detect-flake-and-pass-on-threshold', { + const attempt2 = calculateTestStatus(mockTest2, { + strategy: 'detect-flake-and-pass-on-threshold', maxRetries: totalRetries, passesRequired: 2, }) @@ -129,7 +133,8 @@ describe('mocha custom methods', () => { expect(mockTest2.final).to.be.false const mockTest3 = createMockTest('passed', [mockTest1, mockTest2]) - const attempt3 = calculateTestStatus(mockTest3, 'detect-flake-and-pass-on-threshold', { + const attempt3 = calculateTestStatus(mockTest3, { + strategy: 'detect-flake-and-pass-on-threshold', maxRetries: totalRetries, passesRequired: 2, }) @@ -141,7 +146,8 @@ describe('mocha custom methods', () => { expect(mockTest3.final).to.be.false const mockTest4 = createMockTest('passed', [mockTest1, mockTest2, mockTest3]) - const attempt4 = calculateTestStatus(mockTest4, 'detect-flake-and-pass-on-threshold', { + const attempt4 = calculateTestStatus(mockTest4, { + strategy: 'detect-flake-and-pass-on-threshold', maxRetries: totalRetries, passesRequired: 2, }) @@ -156,7 +162,8 @@ describe('mocha custom methods', () => { it('failed: no longer signals to retry test if the passesRequired is impossible to meet', function () { totalRetries = 4 const mockTest1 = createMockTest('failed') - const attempt1 = calculateTestStatus(mockTest1, 'detect-flake-and-pass-on-threshold', { + const attempt1 = calculateTestStatus(mockTest1, { + strategy: 'detect-flake-and-pass-on-threshold', maxRetries: totalRetries, passesRequired: 2, }) @@ -168,7 +175,8 @@ describe('mocha custom methods', () => { expect(mockTest1.final).to.be.false const mockTest2 = createMockTest('failed', [mockTest1]) - const attempt2 = calculateTestStatus(mockTest2, 'detect-flake-and-pass-on-threshold', { + const attempt2 = calculateTestStatus(mockTest2, { + strategy: 'detect-flake-and-pass-on-threshold', maxRetries: totalRetries, passesRequired: 2, }) @@ -180,7 +188,8 @@ describe('mocha custom methods', () => { expect(mockTest2.final).to.be.false const mockTest3 = createMockTest('failed', [mockTest1, mockTest2]) - const attempt3 = calculateTestStatus(mockTest3, 'detect-flake-and-pass-on-threshold', { + const attempt3 = calculateTestStatus(mockTest3, { + strategy: 'detect-flake-and-pass-on-threshold', maxRetries: totalRetries, passesRequired: 2, }) @@ -192,7 +201,8 @@ describe('mocha custom methods', () => { expect(mockTest3.final).to.be.false const mockTest4 = createMockTest('failed', [mockTest1, mockTest2, mockTest3]) - const attempt4 = calculateTestStatus(mockTest4, 'detect-flake-and-pass-on-threshold', { + const attempt4 = calculateTestStatus(mockTest4, { + strategy: 'detect-flake-and-pass-on-threshold', maxRetries: totalRetries, passesRequired: 2, }) @@ -209,7 +219,8 @@ describe('mocha custom methods', () => { it('failed: no longer signals to retry after retries are exhausted', function () { totalRetries = 3 const mockTest1 = createMockTest('failed') - const attempt1 = calculateTestStatus(mockTest1, 'detect-flake-but-always-fail', { + const attempt1 = calculateTestStatus(mockTest1, { + strategy: 'detect-flake-but-always-fail', maxRetries: totalRetries, stopIfAnyPassed: false, }) @@ -221,7 +232,8 @@ describe('mocha custom methods', () => { expect(mockTest1.final).to.be.false const mockTest2 = createMockTest('failed', [mockTest1]) - const attempt2 = calculateTestStatus(mockTest2, 'detect-flake-but-always-fail', { + const attempt2 = calculateTestStatus(mockTest2, { + strategy: 'detect-flake-but-always-fail', maxRetries: totalRetries, stopIfAnyPassed: false, }) @@ -233,7 +245,8 @@ describe('mocha custom methods', () => { expect(mockTest2.final).to.be.false const mockTest3 = createMockTest('passed', [mockTest1, mockTest2]) - const attempt3 = calculateTestStatus(mockTest3, 'detect-flake-but-always-fail', { + const attempt3 = calculateTestStatus(mockTest3, { + strategy: 'detect-flake-but-always-fail', maxRetries: totalRetries, stopIfAnyPassed: false, }) @@ -245,7 +258,8 @@ describe('mocha custom methods', () => { expect(mockTest3.final).to.be.false const mockTest4 = createMockTest('passed', [mockTest1, mockTest2, mockTest3]) - const attempt4 = calculateTestStatus(mockTest4, 'detect-flake-but-always-fail', { + const attempt4 = calculateTestStatus(mockTest4, { + strategy: 'detect-flake-but-always-fail', maxRetries: totalRetries, stopIfAnyPassed: false, }) @@ -262,7 +276,8 @@ describe('mocha custom methods', () => { it('failed: short circuits after a retry has a passed test', function () { totalRetries = 3 const mockTest1 = createMockTest('failed') - const attempt1 = calculateTestStatus(mockTest1, 'detect-flake-but-always-fail', { + const attempt1 = calculateTestStatus(mockTest1, { + strategy: 'detect-flake-but-always-fail', maxRetries: totalRetries, stopIfAnyPassed: true, }) @@ -274,7 +289,8 @@ describe('mocha custom methods', () => { expect(mockTest1.final).to.be.false const mockTest2 = createMockTest('passed', [mockTest1]) - const attempt2 = calculateTestStatus(mockTest2, 'detect-flake-but-always-fail', { + const attempt2 = calculateTestStatus(mockTest2, { + strategy: 'detect-flake-but-always-fail', maxRetries: totalRetries, stopIfAnyPassed: true, }) diff --git a/packages/driver/src/cypress/mocha.ts b/packages/driver/src/cypress/mocha.ts index e17b6f5dbdae..125c36672bba 100644 --- a/packages/driver/src/cypress/mocha.ts +++ b/packages/driver/src/cypress/mocha.ts @@ -53,7 +53,7 @@ type NormalizedRetriesConfig = { } // NOTE: 'calculateTestStatus' is marked as an individual function to make functionality easier to test. -export function calculateTestStatus (test: CypressTest, config: NormalizedRetriesConfig) { +export function calculateTestStatus (test: CypressTest, config?: NormalizedRetriesConfig) { // @ts-expect-error const totalAttemptsAlreadyExecuted = test.currentRetry() + 1 let shouldAttemptsContinue: boolean = true @@ -78,21 +78,21 @@ export function calculateTestStatus (test: CypressTest, config: NormalizedRetrie const passingAttempts = passedTests.length // Below variables are used for when strategy is "detect-flake-and-pass-on-threshold" or no strategy is defined - let passesRequired = config.strategy !== 'detect-flake-but-always-fail' ? - (config.passesRequired || 1) : + let passesRequired = config?.strategy !== 'detect-flake-but-always-fail' ? + (config?.passesRequired || 1) : null - const neededPassingAttemptsLeft = config.strategy !== 'detect-flake-but-always-fail' ? + const neededPassingAttemptsLeft = config?.strategy !== 'detect-flake-but-always-fail' ? (passesRequired as number) - passingAttempts : null // Below variables are used for when strategy is only "detect-flake-but-always-fail" - let stopIfAnyPassed = config.strategy === 'detect-flake-but-always-fail' ? + let stopIfAnyPassed = config?.strategy === 'detect-flake-but-always-fail' ? (config.stopIfAnyPassed || false) : null // Do we have the required amount of passes? If yes, we no longer need to keep running the test. - if (config.strategy !== 'detect-flake-but-always-fail' && passingAttempts >= (passesRequired as number)) { + if (config?.strategy !== 'detect-flake-but-always-fail' && passingAttempts >= (passesRequired as number)) { outerTestStatus = 'passed' test.final = true shouldAttemptsContinue = false @@ -101,13 +101,13 @@ export function calculateTestStatus (test: CypressTest, config: NormalizedRetrie // For strategy "detect-flake-and-pass-on-threshold" or no strategy (current GA retries): // If we haven't met our max attempt limit AND we have enough remaining attempts that can satisfy the passing requirement. // retry the test. - (config.strategy !== 'detect-flake-but-always-fail' && remainingAttempts >= (neededPassingAttemptsLeft as number)) || + (config?.strategy !== 'detect-flake-but-always-fail' && remainingAttempts >= (neededPassingAttemptsLeft as number)) || // For strategy "detect-flake-but-always-fail": // If we haven't met our max attempt limit AND // stopIfAnyPassed is false OR // stopIfAnyPassed is true and no tests have passed yet. // retry the test. - (config.strategy === 'detect-flake-but-always-fail' && (!stopIfAnyPassed || stopIfAnyPassed && passingAttempts === 0)) + (config?.strategy === 'detect-flake-but-always-fail' && (!stopIfAnyPassed || stopIfAnyPassed && passingAttempts === 0)) )) { test.final = false shouldAttemptsContinue = true @@ -128,7 +128,7 @@ export function calculateTestStatus (test: CypressTest, config: NormalizedRetrie } return { - strategy: config.strategy, + strategy: config?.strategy, shouldAttemptsContinue, attempts: totalAttemptsAlreadyExecuted, outerStatus: outerTestStatus,