Speak of "testing JavaScript" and most developers will conjure up images of editing an HTML file and then hitting reload on their web browser. That's probably not going to change, at least not as long as web browser support for JavaScript is so unpredictable. But if you have files of JavaScript you find yourself using more than once, you need unit tests.
This is true for any programming language, of course, Perl included. In fact, you can find some excellent modules on the CPAN to make code testing easy. You should make yourself familiar with Test::Simple, if you're haven't already. But can you automate the testing of JavaScript as easily as Perl? Yes, and you can do it using Perl.
perl will need to learn how to execute JavaScript in order to run your JavaScript tests, which requires a JavaScript interpretter. Mozilla makes one freely available as a C library called "SpiderMonkey". With this installed you can write simple test scripts like the one below.
#!/usr/bin/perl
use Test::JavaScript qw(no_plan);
use_ok('../mock_dom.js'); # pretend there's a browser
use_ok('../Foo.js'); # this is the code I want to test
# my tests...
ok(qq{
var foo = new Foo();
foo.bar = 1;
}, 'Setting the foo.');
is('foo.bar', '1', 'foo.bar must be set.');This should look pretty familiar to anyone who's ever written Perl unit tests before. You'll have to be aware if your JavaScript is going to access parts of a web browser DOM (like an image in a web page). SpiderMonkey executes JavaScript, but it isn't a web browser, so, if you need it, you'll have to fake that bit. This is a very common scenerio in code testing--imagine the problems if you had to regularly test a script that deleted every row of a databse, and didn't mock it! A mock DOM can be built up on an ad-hoc basis, but might start out as something like the following example.
// mock_dom.js window=new Object(); document=new Object(); document.write=new Function(); document.images=new Array();
Before you can run any tests though you must install the Mozilla SpiderMonkey engine, and the necessary perl modules: JavaScript::SpiderMonkey and Test::JavaScript.
The first step is to download and compile the source for SpiderMonkey and the JavaScript-SpiderMonkey module that accesses it. Compiling SpiderMonkey should result in a C library called "libjs.so", which the perl module needs to know about. The source code can be downloaded and compiled in a temporary folder but you will need root(or sudo) access to install it. This is what it looks like on my Linux machine but, as always, check the official documentation for more information.
wget http://ftp.mozilla.org/pub/mozilla.org/js/js-1.5.tar.gz wget http://search.cpan.org/CPAN/authors/id/T/TB/TBUSCH/JavaScript-SpiderMonkey-0.15.tar.gz tar -xvzf js-1.5.tar.gz tar -xvzf JavaScript-SpiderMonkey-0.15 cd js/src make -f Makefile.ref sudo cp spidermonkey/js/src/Linux_All_DBG.OBJ/libjs.so /usr/lib export LD_LIBRARY_PATH=/usr/lib cd ../../JavaScript-SpiderMonkey-0.15 perl Makefile.PL make make test sudo make install
This alone is a great set-up. Not only can you now run JavaScript files from the command line, but you can also execute JavaScript from within your perl code as well. Now to put a bow on the whole present (a unit-testing, erm, present that is) you should grab the module Test::JavaScript from the CPAN. Using the CPAN shell makes that easy.
sudo perl -MCPAN -e shell install Test::JavaScript q
