Today I’m setting up my laptop to give me an easier and lazier life, so that tests can be triggered whenever a change occurs.

As I’ve been writing several unofficial tests to check that the formula conversions are occurring correctly, it’s time to get a proper testing environment set up on the laptop.

I want to run some tests, such as the following test example:

describe('Finish skin softening treatment', function () {
    it('puts the lotion in the basket', function () {
        var rope = {basket: ''};

        expect(lower(rope)).toBe({basket: 'lotion'});
    });
});

I also want to also be lazy and have the tests be automatically triggered whenever I save a file.

Working hard to be lazy

A Ruby gem called jstdutil provides an autotest that gives nicely coloured output of the test results – red for bad and green for good – and connects with Google’s js-test-driver. And because I want to use the behaviour-driven development of Jasmine, there is a Jasmine Adaptor that connects with js-test-driver.

Getting started with js-test-driver is fairly easy, and connecting in the Jasmine adaptor is easy enough. The main challenge though is in getting jstdutil working.

Jstdutil – my nemesis

Jstdutil needs a development environment called Ruby to be installed, along with an app management service called Gems. A good set of instructions for installing Ruby and Gems are found in steps 1 and 2 at https://forwardhq.com/support/installing-ruby-windows

  1. Install Ruby 1.9.3-p448.exe
    • tick box to add to path
    • tick box to associate .rb and .rbw files
  2. Extract DevKit-tdm-32-4.5.2-20111229-1559-sfx.exe which gives you access to gems
    1. Extract it to a temporary folder, such as c:\temp\devkit
    2. From a dos prompt, init and then install the devkit
      C:\Users\Paul>cd \temp\devkit
      
      C:\temp\devkit>ruby dk.rb init
      ...
      C:\temp\devkit>ruby dk.rb install
      ...
      
    3. You can now clean up by deleting the devkit folder
  3. Install jstdutil from gemcutter. Jstdutil also needs win32console for the coloured output, so get that too.
    • C:\Users\Paul>gem update --system
      ...
      C:\Users\Paul>gem install gemcutter
      ...
      C:\Users\Paul>gem install win32console
      ...
      C:\Users\Paul>gem install jstdutil
      ...
      

After jstdutil is installed, we now have access to jsautotest

Fixing up jsautotext

Because Ruby has advanced though since jsautotest was developed, jsautotest will give a warning about wanting to use RbConfig instead of Config, so you can edit the watchr.rb file to make that change, by updating line 111 in
C:\Ruby193\lib\ruby\gems\1.9.1\watchr-0.7\lib\watchr.rb to be:

case ENV['HANDLER'] || Config::CONFIG['host_os']

and updating it to be:

case ENV['HANDLER'] || RbConfig::CONFIG['host_os']

So we can now get started with testing.

Setting up a test framework

A TestDriver.conf file might be used for a project, such as:

server: http://localhost:9876

load:
  - lib/jasmine-core/jasmine.js
  - lib/jasmineAdaptor.js
  - src/*.js
  - src-test/*.js

and a batch file such as TestDriver.bat can be used to get things started:

@echo off
rem Connect the test driver to a web browser
start cmd /K java -jar lib/JsTestDriver.jar --port 9876 --browser %LOCALAPPDATA%\Google\Chrome\Application\chrome.exe

rem Ping is a hack, causing the batch file to wait for a second before continuing
ping 127.0.0.1 -n 1 -w 1000 > nul

rem tell jsautotest where the test driver is located
SET JSTESTDRIVER_HOME=lib

rem Watch for changes to the loaded test files
jsautotest lib/JsTestDriver.jar

So now the test code right from the start of this post can be automatically run whenever a change occurs, which lets me get on with doing more important things.

Advertisements