Suffice to say, my minions write a quantity of Javascript. And testing it isn’t all that easy. While rummaging the internet, I came across @NeilCrosby‘s FrontEndTestSuite which aims to automate e.g. w3c validator checks and so on – there will be more on that later I suspect once I get it working.
Anyway, the first part I wanted to do is to run a Javascript linter on things…. as I haven’t really come across these before.
I’ve found two approaches :
Install JavascriptLint
see http://www.javascriptlint.com/download.htm –
Build instructions for the lazy – uncompress/extract the files from the archive, then :
- cd jsl-0.3.0/src
- make -f Makefile.ref
- cp Linux_All_DBG.OBJ/jsl /usr/local/bin
Usage looks a bit like :
jsl -process $file
As per the ‘help’ documentation, it returns different exit codes depending how things went (e.g. 0 – everything good; 1 – warnings etc).
Aside from the annoying compile step, this seemed the easiest to setup, and friendliest to use from the command line.
Mozilla’s Rhino & JSLint.js
This is the approach expected by Neil’s TestSuite above (more soon, perhaps).
Download the Mozilla Rhino thing – for me this is a simple ‘apt-get install rhino‘ YMMV.
- export CLASSPATH=/usr/share/java/js.jar
- java org.mozilla.javascript.tools.shell.Main /path/to/some/javascript.js
Again, this will give some sort of return error code if it can’t parse it – but it’s not yet running through jslint… which is what we really want.
Firstly, download JSLint.js via https://github.com/douglascrockford/JSLint/blob/master/fulljslint.js (click on the ‘raw’ button)…
- (Requires CLASSPATH thing from above)
- java org.mozilla.javascript.tools.shell.Main
- load(‘fulljslint.js’);
- to_test = readFile(‘/path/to/javascript/file/to/test.js’);
- result = JSLINT(to_test, null);
If ‘result’ is ‘false’ then you can inspect the errors via JSLINT.errors.
Next up, getting frontend-test-suite running, or something based upon it….