I have been experimenting more with
JavaScript frameworks lately. Being fond of the Test Driven
Development methodology, I came across Jasmine. Here is how the site
defines it: Jasmine
is a behavior-driven development framework for testing JavaScript
code. It does not depend on any other JavaScript frameworks. It does
not require a DOM. And it has a clean, obvious syntax so that you can
easily write tests.
If you have not already, download
Jasmine from http://pivotal.github.io/jasmine/
After you decompress the downloaded file you see this directory structure.
As you can see, the SpecRunner.html file is your user interface that is simply run from a web browser. The
spec folder will hold, as you guessed, your specification code—what
you want your implementation to do. The src folder will contain the
code you are testing.
The JavaScript includes in the SpecRunner.html file need to be set as such:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- include source files here... --> | |
<script type="text/javascript" src="src/DateSort.js"></script> | |
<!-- include spec files here... --> | |
<script type="text/javascript" src="spec/SpecHelper.js"></script> | |
<script type="text/javascript" src="spec/DateSortSpec.js"></script> |
What I wanted to do was to test how
to sort a JavaScript array based on a date element in the array. So
let’s start with the spec file. I named my spec DateSortSpec.js and
placed it in the spec directory. Here is the DateSortSpec.js:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
describe("Sort by Date", function() { | |
var dateSort; | |
var booksData; | |
beforeEach(function() { | |
dateSort = new DateSort(); | |
booksData = [ | |
{ | |
title: 'Health for a friend', | |
author: 'John Doe', | |
publishDate: '01/01/2013' | |
}, | |
{ | |
title: 'A Million Biliion Dollars', | |
author: 'James Dean', | |
publishDate: '03/15/2013' | |
}, | |
{ | |
title: 'Wisdom', | |
author: 'Tom Thumb', | |
publishDate: '06/03/2013' | |
} | |
]; | |
}); | |
it("should show Health for a Friend title with ascending sort", function() { | |
var sortedAsc = dateSort.sortAsc(booksData); | |
var sortedTitle = sortedAsc[0]['title']; | |
expect(sortedTitle).toEqual('Health for a friend'); | |
}); | |
}); |
Next, the source code file,DateSort.js, goes in the scr directory:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DateSort = function (){} | |
DateSort.prototype.sortAsc = function(theArray){ | |
return ""; | |
} |
OK, with basic structure in place let's do a sanity check and make sure we get a failing test result:
Now let’s get the “should show
Health for a Friend title with ascending sort” spec to pass. First,
create a function that will sort ascending by date. What I did was to
utilize the JavaScript array.sort method passing in a compare function.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DateSort = function (){} | |
DateSort.prototype.sortAsc = function(theArray){ | |
return theArray.sort(date_sort_asc); | |
} | |
var date_sort_asc = function(a,b){ | |
a = new Date(a.publishDate); | |
b = new Date(b.publishDate); | |
return a<b ? -1 : a>b ? 1 : 0; | |
}; |
Now reload the browser and the spec
passes.
Next we will add the spec to sort
descending.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
it("should show Wisdom title with descending sort", function() { | |
var sortedDesc = dateSort.sortedDesc(booksData); | |
var sortedTitle = sortedDesc[0]['title']; | |
expect(sortedTitle).toEqual('Wisdom'); | |
}); |
Reload the browser and we see the
spec fail as we have not implemented any code for the spec.
So, the next step is clear.
Implement enough code to pass the spec. Here I again use the
JavaScript array.sort method passing in a function for a descending
comparator.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DateSort.prototype.sortedDesc = function(theArray){ | |
return theArray.sort(date_sort_desc); | |
} | |
var date_sort_desc = function(a,b){ | |
a = new Date(a.publishDate); | |
b = new Date(b.publishDate); | |
return a<b ? 1 : a>b ? -1 : 0; | |
}; |
Refresh the browser again and bang!
As you can see using Jasmine is
pretty straight forward. Setup is a matter of expanding the
compressed file. Moreover, if one has a web browser and text editor
you a ready to code the specs and source code.