JavaScript Unit Testing Part 2: JSSpec

17. April 2009

In this post we will examine the 2nd of 4 JavaScript Unit Testing frameworks... JSSpec.

Previous parts of this series...

In subsequent posts we will cover QUnit, and YUI Test.

I like this Unit Testing framework a lot more than JsUnit for the following reasons...

  • As you can see from the screenshot below, it has a nice interactive Test Runner
  • The testing API has an easy to read Fluent Interface (a.k.a. It reads like English)
  • The project appears to still be active... the latest version was available on Sep 23, 2008
  • The test files don't need to be hosted in a web server in order to run (unlike JsUnit).

[More]

Unit Testing


JavaScript Unit Testing Part 1: JsUnit

7. April 2009

In this post we will examine the 1st of 4 JavaScript Unit Testing frameworks... JsUnit.

In subsequent posts we will cover JSSpec, QUnit, and YUI Test.

First of all, lets define some JavaScript code that we want to Unit Test. I wrote the following Pig Latin JavaScript utility for the sole purpose of this Unit Testing series…

*Note: I am aware the code needs to be refactored. We will address that in a future blog post :) 



/*const*/ var CONSONANTS = 'bcdfghjklmnpqrstvwxyz';
/*const*/ var VOWELS = 'aeiou';

function EnglishToPigLatin(english) {
/*const*/ var SYLLABLE = 'ay';

var pigLatin = '';

if (english != null && english.length > 0 &&
(VOWELS.indexOf(english[0]) > -1 || CONSONANTS.indexOf(english[0]) > -1 )) {
if (VOWELS.indexOf(english[0]) > -1) {
pigLatin = english + SYLLABLE;
} else {
var preConsonants = '';
for (var i = 0; i < english.length; ++i) {
if (CONSONANTS.indexOf(english[i]) > -1) {
preConsonants += english[i];
if (preConsonants == 'q' && i+1 < english.length && english[i+1] == 'u') {
preConsonants += 'u';
i += 2;
break;
}
} else {
break;
}
}
pigLatin = english.substring(i) + preConsonants + SYLLABLE;
}
}

return pigLatin;
}


[More]

JavaScript, Unit Testing ,


Olark Livehelp