Skip to content

Instantly share code, notes, and snippets.

@russHyde
Last active May 27, 2022 14:15
Show Gist options
  • Save russHyde/2bf7b80481de9866aee023531a2c298b to your computer and use it in GitHub Desktop.
Save russHyde/2bf7b80481de9866aee023531a2c298b to your computer and use it in GitHub Desktop.
Notes on puppeteer / jest tests
# Run the test file multiple times, break when it fails to pass
TEST_PATTERN="xxx"
for i in {1..10}; do
npx jest ${TEST_PATTERN} --silent --runInBand;
if [[ "$?" != 0 ]]; then echo "Failed after $i attempts" && break; fi;
done
/**
* Wait for an event to be signalled to the browser by the (shiny) app
*
* Useful events:
* - "shiny:idle" - indicates that the server is idle
* - "shiny:visualchange" - indicates that an output is resized / hidden / shown
*
* Source: https://github.com/puppeteer/puppeteer/issues/2455
*
* @param {Page} page
* @param {string} event
* @param {number} timeout
*/
async function waitForEvent(page, event, timeout = 5000) {
return Promise.race([
page.evaluate(
(event) =>
new Promise((resolve) =>
document.addEventListener(event, resolve, { once: true })
),
event
),
page.waitForTimeout(timeout),
]);
}
@russHyde
Copy link
Author

russHyde commented May 27, 2022

FIrst block from https://stackoverflow.com/a/55937841/1845650

Unfortunately the implementation from that SO post fails:

TEST_PATTERN="xxx"
for i in {1..10}; do npx jest ${TEST_PATTERN} --silent || (echo 'Failed after $i attempts' && break); done
... snip ...
Failed after $i attempts
bash: break: only meaningful in a `for', `while', or `until' loop

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment