Created
August 15, 2022 22:29
-
-
Save joeyparrish/c8b020a33095ea7cf191c06f4339a4dc to your computer and use it in GitHub Desktop.
A function to shim Jasmine and automatically retry failed tests
This file contains hidden or 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
function retryFailedTests(retries, millisecondsBetweenRetries, setTimeout) { | |
const originalSpecConstructor = jasmine.Spec; | |
jasmine.Spec = function(attrs) { | |
const spec = new originalSpecConstructor(attrs); | |
const originalTestFn = spec.queueableFn.fn; | |
// Handles both styles of async testing (Promises and done()) and returns a | |
// Promise. Wraps synchronous tests in a Promise, too. | |
const runOriginalTest = () => { | |
console.log('Running original test'); | |
if (originalTestFn.length == 0) { | |
return originalTestFn(); | |
} else { | |
return new Promise((resolve) => { | |
originalTestFn(resolve); | |
}); | |
} | |
}; | |
spec.queueableFn.fn = async function() { | |
let exceptionCaught; | |
let returnValue; | |
for (let i = 0; i < retries; ++i) { | |
spec.reset(); | |
returnValue = undefined; | |
exceptionCaught = undefined; | |
try { | |
returnValue = await runOriginalTest(); | |
} catch (exception) { | |
exceptionCaught = exception; | |
} | |
const failed = !spec.markedPending && | |
(exceptionCaught || spec.result.failedExpectations.length != 0); | |
if (!failed) { | |
break; | |
} | |
if (millisecondsBetweenRetries && i != retries - 1) { | |
await new Promise((resolve) => { | |
setTimeout(resolve, millisecondsBetweenRetries); | |
}); | |
} | |
} | |
if (exceptionCaught) { | |
throw exceptionCaught; | |
} | |
return returnValue; | |
}; | |
return spec; | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment