I recently started exploring testing options for Node.js. Yesterday, I wrote about my experiences with nodeunit. Today, I found Christian Johansen’s blog post Unit testing node.js apps. (Thanks for the write-up, Christian!) Although I was looking for unit testing options, what really got me excited was his mention of Watchr.
Watchr provides a way to run tests automatically in response to system events, e.g., when a file is saved, much like Autotest. I had fallen in love with Autotest’s functionality after learning about it in Micheal Hartl’s nice Ruby on Rails tutorial. According to Watchr’s docs, Autotest leaves something to be desired, but in any case I very much would like my tests to run without my having to think about it.
Git-ting (ha!) Watchr was easy enough, but to run Node tests on my Mac, which for some reason is an idea I’m hung up on, I need Node, and to date I haven’t been able to build Node on my Mac (10.6.4) successfully, so this is my challenge. After searching here and there, I found an archived thread from the Node mailing list that seemed promising. It mentions that MacPorts can break if I upgrade to Snow Leopard without upgrading MacPorts, which I had, and that this can prevent Node from compiling. After clicking through to the MacPorts migration docs, I followed the steps outlined there and I was able to build Node like this:
- I had tried and failed to build Node multiple times, so I blew away the build directory: rm -rf build
- ./configure
- Clean things up to be thorough: make clean
- make
- Run tests just in case: make test
- sudo make install
Ok, on to the testing. Here’s my folder structure:
project/
– autotest.watchr
– lib/
– example.js
– test/
– test_example.js
My autotest.watchr file is a blend of the one on Christian’s blog, and Watchr’s tests.watchr prepackaged script. It contains
watch( 'test/test_.*\.js' ) {|md| system("node #{md[0]}") } watch( 'lib/(.*)\.js' ) {|md| system("node test/test_#{md[1]}.js") } # -------------------------------------------------- # Signal Handling # -------------------------------------------------- # Ctrl-\ Signal.trap('QUIT') do puts " --- Running all tests ---\n\n" run_all_tests end # Ctrl-C Signal.trap('INT') { abort("\n") }
example.js contains
exports.foo = 'bar';
test_example.js contains
var assert = require("assert"); var example = require('../lib/example'); assert.strictEqual(example.foo, 'bar', 'var foo should be "bar"');
I fire up watchr like this: watchr autotest.watchr
Watchr then captures the terminal until I enter Ctrl+C. Saving either example.js or test_example.js causes test_example.js to run. At this point the tests are crude, so my output is nothing if the test passes, or an assertion error, e.g., “AssertionError: var foo should be “bar””, if the test fails.
I think this is a good start. Time to listen to some Bonobo and call it a day.
One thought on “Getting started with Watchr (and trying again to install Node.js on Mac 10.6.4)”
Comments are closed.