function failTestsOnUnhandledErrors() {
  // https://developer.mozilla.org/en-US/docs/Web/Events/unhandledrejection
  window.addEventListener('unhandledrejection', (event) => {
    /** @type {?} */
    const error = event.reason;
    failOnError('Unhandled rejection in Promise', error);
  });

  // https://developer.mozilla.org/en-US/docs/Web/API/Window/error_event
  // https://developer.mozilla.org/en-US/docs/Web/API/ErrorEvent
  window.addEventListener('error', (event) => {
    if (typeof event == 'string') {
      // Since we moved to a flat (frameless) testing environment, we sometimes
      // get error "events" on Chromecast where we only get the string "Script
      // error." instead of an actual event object.  This would be consistent
      // with an unhandled error thrown by a cross-origin script (such as the
      // Cast SDK), but this string-only form should only be sent to the
      // onerror callback and not the 'error' event listener.  This is both a
      // bug in the SDK (unhandled error) and in the platform (called event
      // listener with wrong format).  We ignore it here.
      console.log('Suppressing error event with only string:', event);
      return;
    }

    /** @type {?} */
    const error = event['error'];
    if (!error) {
      // Since we moved to a flat (frameless) testing environment, we sometimes
      // get error events on Chromecast where the error field is null.  It's not
      // clear why.  Ignore these.
      console.log('Suppressing error event with no error:', event);
      return;
    }

    failOnError('Unhandled error', error);
  });
}
Listen for unhandled errors and Promise rejections (which may occur after a
test) and convert them into test failures.