Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// usage:
// cy.poll({
// fn: () => cy.request(...),
// until: response => response.body.count === 1,
// assertFn: response => expect(response.body).to.deep.eq({
// count: 1,
// }),
// timeout: 120000,
// interval: 10000,
// })
function poll({
fn, until, assertFn, timeout = 15000, interval = 3000,
}) {
const endTime = Number(new Date()) + timeout
const isTimeElapsed = () => Number(new Date()) > endTime;
(function _poll() {
fn().then((resp) => {
if (until(resp) || isTimeElapsed()) {
assertFn(resp)
} else {
cy.wait(interval).then(_poll)
}
})
}())
}
export default poll
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.