I have built a cookie consent manager open source project.
As part of the e2e tests I load in third-party scripts, because its realistic. This was all good and fine until Google shipped a bug in GTM.js last week raising uncaught exceptions (TypeError in this instance) which cause tests to fail.
My github actions are failing tests with this error, but not locally for some reason.
● a fresh load of biscuitman.withcss.min.mjs › should hide UI and save consents correctly after selecting some sections
TypeError: Failed to execute 'getValue' on 'CookieDeprecationLabel': Illegal invocation
at ll (https:/www.googletagmanager.com/gtag/js?id=G-TEST:244:406)
at c (https:/www.googletagmanager.com/gtag/js?id=G-TEST:564:130)
Test Suites: 1 failed, 1 total
Tests: 13 failed, 67 passed, 80 total
Snapshots: 0 total
Time: 6.971 s
Ran all test suites.
Error: Process completed with exit code 1.
The specific error doesn’t matter, I want to prevent any third-party javascript errors from failing tests.
How can I prevent JavaScript errors from third party scripts from failing builds? I only care about errors raised by my locally served scripts, and by default all Javascript errors will fail.
Is it possible to prevent JS errors from failing tests by default, and then forcing locally-raised exceptions to fail with something like this?
page.on('console', (msg) => {
const text = msg.text()
const location = msg.location()
if (msg.type() === 'error') {
// Raise errors if it originated from a locally-served resource
if (location && location.url && location.url.startsWith('http://localhost')) {
console.error('Console error:', msg.text())
} else return false
}
})