TDD videos

The plan was to watch videos today about test-driven-development (TDD) with JavaScript, including some from this playlist

One that really took my interest is Test Driving JavaScript by Venkat Subramanium because he goes in to details about how to achieve automated testing, where when you save the file, tests run automatically.

Automated testing

All you need is js-test-driver and a file monitor – it’s that easy! Where have I heard this before?

js-test-driver does installs fine, and apart from a decision on where to place the driver file, things start off well. The server starts and I can automatically connect a driver page from my browser of choice, or several browsers, and when I run the test driver, it runs my tests without any trouble.

>java -jar JsTestDriver.jar --port 9876

And in a separate command prompt window:

>java -jar lib\JsTestDriver.jar --tests all
setting runnermode QUIET
.
Total 1 tests (Passed: 1; Fails: 0; Errors: 0) (6.00 ms)
 Chrome 28.0.1500.71 Windows: Run 1 tests (Passed: 1;
 Fails: 0; Errors 0) (6.00 ms)

So that’s all good there – but! I don’t want to have to run the test command each and every time. I want to be lazy, like my hero Venkat  from the above video.  How can I have the above commands run automatically when a file is changed, and have the test results appear in green for good and red for bad?

Venkat says that there’s a Ruby plugin that does this, but we get no information from him about it other than a script that he runs called:

./runTestsAuto.rb

Eventually I find a gem called Jstdutil, which is a package for a programming language called Ruby. An informative blog post Jstdutil – A Ruby wrapper over JsTestDriver is where I learn that Jstdutil needs GemCutter, which needs RubyGems, which is a package manager for the Ruby programming language. Oh, and Jstdutil also needs to have another gem called win32console installed too.

Hoo-boy – this is going to take all day.

gem install win32console

Update: It has since been learned that win32console is not needed on Ruby 2.0, so removal of that requirement from Jstdutil’s redgreen.rb scripting file fixes all ongoing issues. I’ll provide details about this easy fix in tomorrow’s post. But for now, please feel free to carry on reading down to the “One last error” section for entertainment purposes.

===

Everything installs fine, even win32console which is to give me nice colours on the output (such as green for good and red for bad) – and this is where my problems begin.

After installing win32console I try to run the jstdutil jstestdriver, and it complains that win32console is not there:

C:/Ruby200/lib/ruby/gems/2.0.0/gems/jstdutil-0.3.13/
lib/jstdutil/redgreen.rb:5:in `rescue in <top (required)>':
You must gem install win32console to use color on Windows
(RuntimeError)

Searching the web for answers, I see that others have the same problem with win32console, such as in How do I get colour with Windows command prompt using RSpec in Ruby? They have resorted to using a different solution, which is to permanently change all the windows command prompts using ansicon instead.

I installed ansicon and after fixing a compatibility problem, got things working with that, but I’m not comfortable about it taking over all of the command prompts that my computer runs, and I still have to edit jstdutil (and presumably recompile) to tell it not to attempt to use win32console anyhow. So if either way means that I have to edit things, I should try and fix the problem before taking steps to avoid it.

It is decided – I remove ansicon and knuckle down to resolve the win32console problem.

But win32console really is installed – really!

Why is win32console not working? Time to see if we can garner more information. Using irb (the interactive Ruby shell) I can issue a command to require win32console, and learn more about the problem.

irb#1(main):002:0> require 'win32console'
LoadError: 126: The specified module could not be found.
- C:/Ruby200/lib/ruby/gems/2.0.0/gems/win32console-1.3.2
-x86-mingw32/lib/2.0/Console_ext.so

Aha! So, win32console really is there, but it’s having some trouble getting what it needs. It’s wanting to access the 2.0 folder, and when I check the lib folder I see that there is only 1.8 and 1.9 in there. Why is it trying to load 2.0?

begin
  require 'Console_ext'
rescue LoadError
  RUBY_VERSION =~ /(\d+.\d+)/
  require "#{$1}/Console_ext"
end

So Console_ext could not be found, and it’s instead attempting to find it based on the version of Ruby that I’m using, that being version 2.0. That’s why win32console doesn’t work. It’s because it can’t find Console_ext and I’m using the latest Ruby 2.0.

Fixing win32console

I should be able to get around this issue by doing a manual install from the source code of win32console. Time to install the Ruby development kit, and attempt to create a version of it that works. Fortunately the instructions for that are easy to follow.

While I’m at it, this will also be a good chance to fix an existing problem that rdoc has with some documentation files too:

Parsing documentation for win32console-1.3.2-x86-mingw32
unable to convert "\xF1" from ASCII-8BIT to UTF-8 for
extra/Console.rdoc, skipping
unable to convert "\xF1" from ASCII-8BIT to UTF-8 for
extra/Console_ANSI.rdoc, skipping

That problem with rdoc seems to be a known issue, and I can avoid that by having the files start out as utf-8 files, so Notepad++ is employed to convert them to UTF-8 documents. Now rdoc doesn’t get its knickers in a knot about that particular issue.

Back to installing win32console, the instructions for installing it from its source code are nice and clear, though I do have to be careful that version number in the readme file is different than what I actually have to use, but apart from that issue the process goes fine. The 1.8 and 1.9 folders aren’t in the installed lib folder anymore, and don’t seem to be needed because this time, Console_ext can now be correctly located.

One last error

Running jstestdriver now results in success, but when trying to use jsautotest it fails when I update a scripting file and jsautotest tries to do the testing. It seems that Jstdutil is not quite satisfied with the pain and agony I’ve gone through up to this point, and is throwing one last curveball in to the mix:

An error occurred
Unable to load jar file from C:/Users/Paul/Documents/
Programming/web/javascript/testing/examples/validate-email
Check that $JSTESTDRIVER_HOME is set correctly

The Jstdutil – A Ruby wrapper over JsTestDriver instructions are specific to Linux, not Windows, so I have to remove the $ symbol from there for things to work properly.

Putting on a bow

Once that is done, everything works perfectly. I can now copy that js-test-driver.jar file in to its own location, so that I have a directory structure of:

projectname\
projectname\td.bat
projectname\jsTestDriver.conf
projectname\lib\
projectname\lib\JsTestDriver.jar
projectname\src\
projectname\src\project.js
projectname\src-test\
projectname\src-test\project.js

And I also create a td.bat batch file which starts off the test-driver, and runs the autotest. This also helps to remove the need for the JSTESTDRIVER_HOME from before, too.

@echo off
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

jsautotest lib/JsTestDriver.jar

It’s a custom batch file that helps to setup js-test-driver and starts the automated testing. After running this I can now go straight on to developing without any delays, with saved edits resulting in a separate window updating with the new test results.

It’s been quite the long process, but I think that it’s been well worth it. Here’s a screenshot of things now. Whenever I save a change to the text file, the bottom-right window triggers a running of new tests.

automated-testing

Only one thing remains now and that is to look for posts that other people have made about this problem, and pass on to them the good news.

Advertisements