abidibo.net

Just Open Source

ajsut, abidibo's javascript unit testing library

ajsut javascript testing

You can also be interested in:

Introduction

I know since time ago' that writing tests for our applications is one of the programming best practices, django taught me that many years ago, and gipi repeated it to me many times. But as many best practices it costs time to acquire the necessary skills, and time to start the implementation.

Maybe the best thing could be to place the testing phase at the beginning of the development flow, as the test driven development process shows you, so that you're forced to write tests for your application and you are forced to focus on code specifications, targets, runtime conditions, and all these things let's you write a cleaner and reusable code.

Actually is not so simple to change drastically the proper development flow, and the lack of time which always stays behind the scenes of a work project doesn't  help.

But coming back to the tests importance, we can say that:

  • tests let you verify in a few seconds if changes made to the code have broken some functionalities, or introduced bugs in some parts of your application. The more accurate and specific are the tests you write and  greater is your control over the application.
  • while writing tests, we are forced to think at each functionality individually, seeing them as distinct entities, enforcing the concept of encapsulation, which also is a good practice.

So, the point here is that I think tests are important and some months ago' I had the opportunity to write my own javascript library for unit testing. Ideas come from the awesome book "Secrets of the JavaScript Ninja" by John Resig (jQuery creator) and Bear Bibeault.

ajsut

ajsut is a very lightweight and simple library (2.8 kB the release 0.1), which let's you write and execute tests. The following kind of tests are provided:

  • assertion
  • group of assertion
  • async group of assertion
  • performance
  • performance with mean and standard deviation

Download

You can download a tarball from github here, or directly clone the repository:

$ git clone http://github.com/abidibo/ajsut.git ./ajsut.git

Usage

One thing to consider: do not use async group tests with simple assertions because simple assertions are not queued, use syng group of tests instead.

Include the library

Include the library and the provided css if you like:

<script src="/path/to/ajsut.js" type="text/javascript"></script>
<link type="text/css" rel="stylesheet" href="style/ajsut.css" />

Assertions

ajsut.assert(condition, 'my condition description');

Group of assertions

ajsut.test('Testing a group of assertions', function() {
  ajsut.assert(window, 'window is defined');
  ajsut.assert(typeof window == 'object', 'window is an object')
});

Group of asyncronous assertions

The pause and resume functions are to be called in that positions in order to ensure that tests are executed in the right sequence.

ajsut.test('Group of async assertions', function() {
  ajsut.pause();
  setTimeout(function() {
    ajsut.assert(this === window , 'the context here is the window object');
    ajsut.assert(1 === 1, 'one is equal to one');
    ajsut.resume();
  }, 1000)
});

Performance

Measure of the time needed to perform the given function x times, where x is the third parameter passed to the performance function.

ajsut.performance('test performance', function() {
  var d = document.createElement('div');
  return d;
}, 100000);

Performance with mean and standard deviation

As above, but the calculation is repeated y times, where y is the fourth parameter given to the function meanPerformance (default 100), and the mean and standard deviation are calculated.

ajsut.meanPerformance('test performance', function() {
  var d = document.createElement('div');
  return d;
}, 100000, 100);

Contribute

As usual, feel free to fork and contribute to the project which is hosted on github, pull requests are welcome!
If you find bugs and errors please report them in the github issues page.
Enjoy!

Subscribe to abidibo.net!

If you want to stay up to date with new contents published on this blog, then just enter your email address, and you will receive blog updates! You can set you preferences and decide to receive emails only when articles are posted regarding a precise topic.

I promise, you'll never receive spam or advertising of any kind from this subscription, just content updates.

* indicates required

abidibo.net topics

Comments are welcome!

blog comments powered by Disqus

Your Smartwatch Loves Tasker!

Your Smartwatch Loves Tasker!

Now available for purchase!

Featured