Created
January 30, 2019 03:46
-
-
Save yanshiyason/4082c80cdd214c32304bee0d4202af1b to your computer and use it in GitHub Desktop.
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
// 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