Wednesday, September 5, 2012

Running QUnit on Save with PhantomJS


I have started doing TDD in my CoffeeScript using QUnit. This is pretty amazing really and it has been making me pretty happy. However there is still the very repetitive cycle of save, switch to a browser run the tests and switch back to emacs. What would be amazing is if I could get emacs to do everything for me. After all I should be able to program my system to do the boring repetitive stuff 

It turns out that this is not even that hard, but it does take several parts. 

First of all we need a browser to run the tests. We could use Selenium (which I rather like actually for other things) but even better would be to use PhantomJS which is a headless browser.

What PhantomJS is is a browser based on WebKit (which is also used by Chrome and Safari) that does not display anything. But you can interact with it via JavaScript. So what I want to do is launch my web page (which is on a local web server) then have QUnit run and return the results. Thankfully the PhantomJS folks have a script do just that in the form of a QUnit Driver. (They also have one for Jasmine if you like that).

So now I can call PhantomJS like this:

phantomjs run-qunit.js http://localhost:8080/test.html

I can have it load up my page and return the results of qunit:

'waitFor()' finished in 407ms.
Tests completed in 222 milliseconds.
80 tests of 95 passed, 15 failed.

It gives me a result like that. Which is nice, it tells me exactly what I want to know, if everything passed or not. So what I want is for emacs to run it when I save a file, to do this I run this bit of emacs lisp. 



What I would really like for future is to change it so that if everything passes it will give me a line
that says "203 tests pass" or the like and if all tests do not pass then it should open a buffer to tell me exactly which tests failed. (Maybe even try to figure out which file the failing code is in to bring me to the right spot to fix it )

No comments:

Post a Comment