Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ajax-driven JavaScript runtime assertion framework

While working on a larger web application with an increasing amount of JavaScript code, we did a brainstorming session on how to improve code quality.

One of the first ideas was to introduce unit tests. This will be a long term goal; that will not, however, fix the most common causes of regression: the changing DOM and browser specific issues.

Unit tests run in a mocked, DOM-less environment and are not on the page.

What I'm looking for is an assertion framework that can be plugged into the code like this:

var $div = $("div.fooBarClass");
assertNotEmpty($div);
$div.fooBarAction();

I've found assertion frameworks that can do this, but they all either log into the console or into the DOM or open a silly pop-up. None of these work together with (thousands of) automated tests. What I'm looking for is a run-time assertion framework that logs failed assertion via AJAX! Ideally, it should be:

  • Have common assertions built-in.
  • Integrate with JQuery modules, closures.
  • Log (via Ajax) the assertion, the file name, the page, line number, the cause of failure, some pre-configured variables of the environment (browser, release version etc.).
  • Support callbacks in case of failures. (If any assertion framework can just do this, I would be gladly willing to write callbacks doing the Ajax part.)
  • Work well with all browsers.
  • Trivial to exclude from production release.
  • Maintained code base.
like image 971
sibidiba Avatar asked Feb 15 '11 12:02

sibidiba


1 Answers

We've been using the YUI Test Library. It seems to work fairly well.

Has a variety of assertion methods for different types

Assertions exist for equality, sameness, true, false, object type, and even array item comparison.

Allows for mock objects to test DOM objects and other functions Our code does a lot of AJAX calls, or requires methods / objects that don't need to be tested (as they are tested elsewhere). Using Mock objects, we can tell the tests what to expect. For example:

var mockXhr = Y.Mock();

//I expect the open() method to be called with the given arguments
Y.Mock.expect(mockXhr, {
    method: "open",
    args: ["get", "/log.php?msg=hi", true]                            
});

Works with all browsers

We run our tests in IE, Chrome, and Firefox, and aside from some differences in what the test runner itself looks like, it works!

Trivial to exclude from production release

We have all of our testing code in a separate folder which accesses all the production code. Excluding the tests from production is as easy as excluding a folder.

Maintained codebase

YUI 3 is used on the Yahoo homepage, and seems to be fairly well maintained.

like image 135
NT3RP Avatar answered Nov 06 '22 11:11

NT3RP