I write quite a bit of JavaScript these days, and one observation I have made with regards to Line-of-Business apps is that platforms like Node.js makes me lean more towards sharing business logic between the server and client side (meaning Node.js and the browsers, in practice). This creates an immediate need to unit test “universally” (across all platforms and environments), and I couldn’t find a single tool that quite did it for me.
JSUT (JavaScript Unit Testing) is an attempt at making such a tool for fun and profit (non-monetary).
JSUT stems from me growing tired of the hurdles of unit testing my JavaScript code. There are tons of alternative unit testing tools and libraries readily available, but my experience was that they all exhibited one or more of the following shortcomings:
- Imposing a “framework” or “philosophy” or “style” on my tests
- Bringing me additional dependencies to worry about
- Working only in the browser or inside Node.js
In addition, I feel that many of the available tools and libraries are either undermaintained or overly featureful, and buggy and/or painful to use as a result of one or both of those. I can’t and won’t claim that this will be different in the case of JSUT, so it’s not really an argument, but more a motivation for me.
Goals
I sat down and wrote a few goals for JSUT:
- Universal, cross-platform
- Minimalistic, simple, non-constraining
- Accessible
I then took to writing code and brushing up my rather rusty shell scripting skills, and a few hours later I wound up with what I present to you now.
JSUT – JavaScript Unit Testing – An introduction
Writing a test in JSUT is about as simple as it can get. You write a function:
function myTest(test) {
// JSUTs only requirement:
// Call test.done() when the test is done
test.done();
}
Now, let’s say you save this function to a file called test.js. You can then move to your preferred shell and write the following:
jsut -b chrome test.js
This will run your test in Chrome. To run your test in Node.js, do:
jsut -n test.js
You can combine the -b and -n flags any way you like, of course.
Getting JSUT
Getting JSUT is very simple. Assuming you have npm, just do:
npm install jsut
Which will install jsut locally. If you want a global installation, issue:
npm install jsut -g
Of course, with the goals for JSUT, you can also just download and copy deploy JSUT. Visit http://github.com/havard/jsut for source code and further instructions.
Requirements
As mentioned in the above sample, the only requirement JSUT imposes on you is to call test.done() when your test is done. Obviously, this seems redundant for the simple case of a synchronously executing function, but since most JavaScript functions these days are asynchronous at some level, there has to be a way to tell JSUT that a test is done. I am actively considering easing this requirement for simple cases, but this has not been a priority so far.
For Node.js, you are required to add the functions you want to test to the exports of each test file. Building on the example above, you can maintain cross-platform compatibility by doing the following:
if (typeof(exports) !== 'undefined') {
exports.myTest = myTest;
}
This will add exports only if the exports variable is defined. Now, if you wanted to run your test in Node.js, you simply do:
jsut -n test.js
This will run your test in Node.js. You are free to combine the -n and -b flags any way you like, so that you can target all required platforms with a single command.
Assertions
JSUT automatically injects a CommonJS compatible assert module in your browser tests, so you can write stuff like assert.ok(true); anywhere in your test.
Planned features
JSUT currently only runs browser tests on OS X. I plan to support Linux and Windows as soon as possible. Follow me and JSUT on GitHub!