Last active
September 8, 2022 21:07
-
-
Save agoldis/a88a91343761ea9f15eeb4ddb8225a89 to your computer and use it in GitHub Desktop.
This file contains 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
// A generic "retry" mechanism | |
// Will retry invoking `fn` until it succeeds or will throw the latest exception | |
const withRetries = | |
<T extends (...args: any[]) => any, R = ReturnType<T>>( | |
fn: T, | |
retriesCount: number = 5 | |
) => | |
async (...args: Parameters<T>): Promise<R> => { | |
let i = retriesCount; | |
let error; | |
let result; | |
while (i > 0) { | |
try { | |
error = null; | |
result = await fn(...args); | |
break; | |
} catch (e) { | |
error = e; | |
i--; | |
} | |
} | |
if (error) { | |
throw error; | |
} | |
return result; | |
}; | |
/* | |
Example usage | |
- beforeFn: will throw on first 3 invocations | |
- withRetries: will try up to 5 times | |
*/ | |
let r = 0; | |
const beforeFn = () => { | |
if (r < 3) { | |
r++; | |
throw new Error("error"); | |
} | |
return "ok"; | |
}; | |
const runBeforeWithRetries = withRetries(beforeFn); | |
// reducing the retries to 2 will cause a failure | |
// const runBeforeWithRetries = withRetries(beforeFn, 2); | |
describe("All tests", function () { | |
before(runBeforeWithRetries); | |
it("Test A", function () { | |
cy.log(new Date().toLocaleString()); | |
cy.visit("/"); | |
cy.get("#simpleSearch").type("Africa"); | |
cy.get(".suggestions-result").first().click(); | |
cy.scrollTo(0, 1200); | |
cy.contains("Africa"); | |
}); | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment