Skip to content

Instantly share code, notes, and snippets.

@yanshiyason
Created January 30, 2019 03:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yanshiyason/4082c80cdd214c32304bee0d4202af1b to your computer and use it in GitHub Desktop.
Save yanshiyason/4082c80cdd214c32304bee0d4202af1b to your computer and use it in GitHub Desktop.
// 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