QAssert is a powerful, easy-to-use, JavaScript assertion suite. In contrast to console.assert() it enables you to report failures via AJAX. It gives you also powerful assertion functions and is cross-browser compatible.
It can be used in both in unit, functional and manual tests, and notably also
in production code.
QAssert is especially useful for regression testing: You can place assertions for any assumption that is made in the JavaScript logic. While running the code assertion violations will be reported via AJAX and regressions become quickly visible. To give you an example how to safeguard against unexpected DOM changes:
var $panel = $("#menuPanel");
$panel.assert().load("/");
$.assert($panel.text().length);
It is a simple drop-in jQuery plugin. It is disabled by default. To enable it and to see assertions:
$.assertSetup("/assertReporter"); // Required only once.
$.assert(false)
As a result:
- You will se the assertion failure in the console including a full stack trace (Assertion failed: false undefined {anonymous}()@file:///D:/Dropbox/dev/qassert/qassert/qassert.js:708 …)
- You will see an AJAX call being made to the given URL
It is highly configurable:
$.assertSetup({
enabled: true,
ajax: "/fooBar", // You can also pass a full $.ajax() parameter object here!
catchGlobalErrors: true, // This will report ALL uncatched exceptions too!
contextCallback: function(value, message, stacktrace) {return {version: app.version, locale: app.locale}}, // Enhances the reported data
log: myLogFunction, // Used to log failures locally, by default to the console.
title: "Assertion sucked up:"
});
It has a convenient API:
- $.fn.assert(message, sizeOrCallback): Chained selector assertions.
- $.assert(value, message): Boolean assertion.
- $.assertDeepEquals(value, expected, message): Recursive equality assertion.
- $.assertEmpty(value, message): Empty assertion.
- $.assertEquals(value, expected, message): Equality assertion, no recursion.
- $.assertIs(value, type, message): Type assertion.
- $.assertNot(value, message): Boolean assertion for false.
- $.assertNotDeepEquals(value, expected, message): Recursive unequality assertion.
- $.assertNotEmpty(value, message): Non-empty assertion.
- $.assertNotEquals(value, expected, message): Unequality assertion, no recursion.
- $.assertNotIs(value, type, message): Type assertion inverted.
- $.assertNotSame(value, expected, message): Strict unequality assertion.
- $.assertSame(value, expected, message): Strict equality assertion.
See the documentation and test cases.
Join the development: https://github.com/gaboom/qassert

2 Comments
http://www.phppoet.blogspot.com
http://www.phppoet.blogspot.com