Reputation: 955
I am using mocha and puppeteer to test a page. I would like the test to fail if the page throws an uncaught exception.
This is what I have tried so far:
it('should fail because page has an error', async () => {
const browser = await puppeteer.launch({});
const page = await browser.newPage();
page.on('pageerror', (e) => {
// TODO: Make the test fail somehow
throw e;
});
await page.goto('http://example.com/page-that-throws-an-error');
});
When I run mocha
, this test succeeds but leaves an UnhandledPromiseRejectionWarning
in my terminal.
I don't think I can use a done(error)
callback because I am already using an async function and mocha will complain with Error: Resolution method is overspecified
.
Upvotes: 1
Views: 656
Reputation: 955
I realized I can keep track of the errors the page throws, then check at the end of the test if the page threw any errors.
it('should fail because page has an error', async () => {
const browser = await puppeteer.launch({});
const page = await browser.newPage();
const pageErrors = [];
page.on('pageerror', (e) => {
pageErrors.push(e);
});
await page.goto('http://example.com/page-that-throws-an-error');
for (const e of pageErrors) {
throw e;
}
});
This works, but the test only fails at the end when it checks that there are no errors. Ideally I would like to find a way for the test to fail as soon as the page throws an error.
Upvotes: 4