Also available at

Also available at my website and on Twitter @toshafanasiev

Tuesday, 10 June 2014

Loading "Vanilla" JavaScript files in Node

I'm writing more and more JavaScript these days and it's nice to have some sort of test infrastructure in place for all but the most trivial codebase.

If you're using a framework like AngularJS where modularity and dependency injection are first-class citizens then you're all set up. If, for whatever reason, you can't package things up in nice modules then testing can be tricky, particularly when there are dependencies involved.

I came up against this recently and my solution was to use Node.js and the excellent jasmine-node package
installing jasmine-node

I wrote a very simple Node module called include

Which lets me test simple JS files with no dependencies ...

... and more interestingly, those that rely on global objects such as window and document

So with a directory structure like this

directory structure

Tests can go from this ...
failing tests
... to this

Most of the code I write is more involved than this though :)

No comments:

Post a Comment