简体   繁体   中英

How to report JavaScript errors during functional tests using Intern?

How can I report JavaScript errors that occur during test execution using Intern ? Basically, if there are any JavaScript errors on the page (even as part of things that aren't explicitly tested) I want to know.

Background

I'm just getting started with Intern and testing in general and I'm trying to test all major pages on my site in all browsers because I just changed all our JavaScript to load via require.js. While it looks good in Chrome, I've had issues with require.js and random browsers in the past so I wanted to automate everything. The most likely issue that will arise is that some random JS will fail to execute due to asynchronous loading and load of an expected global. Since there are no current tests setup, I basically want to start by running a 'test' go to through all major pages and report any JavaScript errors.

In order to report uncaught errors, you need to hook the window.onerror method of the page. This is possible, but the page load will need to be finished before you add the hook, which means that any errors that occur before/during the page load (or that occur while the page unloads) simply cannot be caught and reported. It also means if you perform an action that moves to a new page (like a form submission), you will need to make sure you retrieve the list of errors before you perform the action that causes navigation, and reconfigure the window.onerror handler after you get to the new page.

To perform such reporting with a functional test, your test would end up looking something like this:

return this.remote
  .get('http://example.com')
  .execute(function () {
    window.__internErrors__ = [];
    window.onerror = function () {
      __internErrors__.push(Array.prototype.slice.call(arguments, 0));
    };
  })
  // ... interact with the page ...
  .execute(function () {
    return window.__internErrors__;
  })
  .then(function (errors) {
    // read `errors` array to get list of errors
  });

Note that (as of August 2014) errors from window.onerror in all browsers except recent versions of Chrome provide only the message, script source, line number, and (sometimes) column number, so this information would only be useful to say “this action caused an error, go do it manually to get a stack trace”.

During unit tests, Intern already tries to automatically catch any unhandled errors and treats them as fatal errors that halt the system (since you should never have code that generates this kind of unhandled error).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM