Mocha 1.0
The Mocha JavaScript test framework has hit 1.0 with a bunch of great contributions from the community, here’s the change log:
- Added js API. Closes #265
- Added: initial run of tests with
--watch. Closes #345
- Added: mark
location as a global on the CS. Closes #311
- Added
markdown reporter (github flavour)
- Added: scrolling menu to coverage.html. Closes #335
- Added source line to html report for Safari [Tyson Tate]
- Added “min” reporter, useful for
--watch [Jakub Nešetřil]
- Added support for arbitrary compilers via . Closes #338 [Ian Young]
- Added Teamcity export to lib/reporters/index [Michael Riley]
- Fixed chopping of first char in error reporting. Closes #334 [reported by topfunky]
- Fixed terrible FF / Opera stack traces
Compiler support
coffee-script out of the box was removed, now you can used the --compilers <ext>:<module>,... flag to map a compiler to the given extension name. For example mocha --compilers coffee:coffee-script. There are simply too many foo -> JavaScript transpilers to directly support, this pushes that back on the author.
Min reporter
First up is the min reporter by Jakub Nešetřil, this tiny reporter works great with --watch, outputting the summary only, though still reporting verbose errors on failure. 
Markdown reporter
I added a markdown reporter which can be used to display your tests as documentation in a Github wiki page, or simply a markdown file in your repository that you can link to. For example here are the Connect markdown test docs.
I’m not super happy with how much padding Github adds, so the TOC looks pretty messy, but all of these document-style reporting mechanisms make you think twice about how clean and organized your tests are.
JavaScript API
A new JS API was added, which mocha(1) now utilizes. This higher-level JS API will make it easier for those who want to script the testing process with Mocha. I have yet to document this API but it looks like this:
var Mocha = require('mocha');
var mocha = new Mocha;
mocha.reporter('spec').ui('bdd');
mocha.addFile('test/suite.js');
mocha.addFile('test/runner.js');
mocha.addFile('test/runnable.js');
var runner = mocha.run(function(){
console.log('finished');
});
runner.on('pass', function(test){
console.log('... %s passed', test.title);
});
runner.on('fail', function(test){
console.log('... %s failed', test.title);
});
Styling Canvas Drawings With CSS
Today I implemented an idea to style canvas elements with CSS, I’ve never personally seen this done, and it’s not always practical, but in some cases it would be very handy. For example a text selection api may be comprised of several “elements”, the text, the caret, the selection range rect etc.
Let’s say by default, our drawing will look the image below:

great, looks fine, but what if designers, or even developers for organizational purposes, could tweak this with CSS. Well you can! Suppose we want to support the following css:
#editor .text {
font: 20px helvetica, arial, sans-serif;
}
#editor .text .selection {
background: #DFF3FC;
}
First, the API to access such data will look very similar to how we wrote our css, however we provide the css property as well:
style('#editor .text .selection', 'font-family')
Now for the implementation details, keeping in mind this is not targeting cross-browser support, it’s simply a quick naive implementation, and I should note this does not cover the case of style changes, only initial styles.
So, firstly we check our cache, to prevent several expensive lookups, here style.cache is a property of our style function, if it’s available, we simply return the result.
function style(selector, prop) {
var cache = style.cache = style.cache || {}
, cid = selector + ':' + prop;
if (cache[cid]) return cache[cid];
...
Next up we will naively split the selector into an array so that we can work with it.
...
var parts = selector.split(/ +/)
, len = parts.length
, parent = root = document.createElement('div')
, child
, part;
...
We loop through the “parts”, creating one div per “part”. This will allow us to see what the computed style is for an element, that otherwise, does not exist! because it’s a canvas. The naive implementation I have here, supports ids and classes only, so we check the first character, and assign the id and class respectively.
...
for (var i = 0; i < len; ++i) {
part = parts[i];
child = document.createElement('div');
parent.appendChild(child);
parent = child;
if ('#' == part[0]) {
child.setAttribute('id', part.substr(1));
} else if ('.' == part[0]) {
child.setAttribute('class', part.substr(1));
}
}
...
Finally we append the root element to the document.body so that the styles are applied, get the computed style for the given prop, remove the element, and cache/return our value.
...
document.body.appendChild(root);
var ret = getComputedStyle(child)[prop];
document.body.removeChild(root);
return cache[cid] = ret;
}
Example Usage
Now let’s try it out, first, the css, providing a light yellow background:
#chalk .text .selection {
background: #FAFFDF;
}
In our JavaScript text selection logic, we should provide a JavaScript API for altering styling as well, however for this example, let’s add it directly:
ctx.fillStyle = style('#editor .text .selection', 'background-color');
ctx.fill(...)
Once applied, we get the following result:

That’s it! you can apply this trick to assign font sizes and families, foreground and background coloring, border colors, anything you can come up with! if anyone comes up with a robust implementation I would love to see it!
Full Source
Here’s the full implementation:
function style(selector, prop) {
var cache = style.cache = style.cache || {}
, cid = selector + ':' + prop;
if (cache[cid]) return cache[cid];
var parts = selector.split(/ +/)
, len = parts.length
, parent = root = document.createElement('div')
, child
, part;
for (var i = 0; i < len; ++i) {
part = parts[i];
child = document.createElement('div');
parent.appendChild(child);
parent = child;
if ('#' == part[0]) {
child.setAttribute('id', part.substr(1));
} else if ('.' == part[0]) {
child.setAttribute('class', part.substr(1));
}
}
document.body.appendChild(root);
var ret = getComputedStyle(child)[prop];
document.body.removeChild(root);
return cache[cid] = ret;
}
Redis Implemented With Node
Nedis is a (partial) redis implementation written with node. Primarily for fun, however as our team grows larger, and as we add more non-technical team members over at LearnBoost I figured it would be nice help prevent the need for compiling development dependencies.
Nedis is an old side project I had going, and is no where near complete, but it does work, so I figured I would open-source it. Currently Nedis implements the unified Redis protocol which is an brilliantly simple binary-safe protocol that is human and machine friendly.
Using Existing Tools
Currently we use Redis for sessions in our app, so having a drop-in replacement is a great way to get session support for your app without booting up redis-server. For example the nodejs module connect-redis can utilize Matt Ranney’s fantastic redis client without change.
Another neat side-effect is that you can use existing redis tools such as redis-cli to interact with Nedis. First let’s start Nedis with nedis-server:
$ nedis-server
Now we can play with the cli, interacting with node
$ redis-cli
redis> hmset users:tj email tj@vision-media.ca age 23
OK
redis> hgetall users:tj
1) "email"
2) "tj@vision-media.ca"
3) "age"
4) "23"
redis> keys users:*
1) "users:tj"
Note that nedis-server basically consists of no more than the line of js below, so it’s easy to boot from within your process if desired.
nedis.createServer(options).listen(port);
Supported Commands
Below is a list of the commands currently supported by Nedis
- PING
- ECHO
- QUIT
- SELECT
- HLEN
- HVALS
- HKEYS
- HSET
- HMSET
- HGET
- HGETALL
- HEXISTS
- TYPE
- EXISTS
- RANDOMKEY
- DEL
- RENAME
- KEYS
- FLUSHDB
- FLUSHALL
- DBSIZE
- INFO
- BGREWRITEAOF
- GET
- GETSET
- GET
- SETNX
- INCR
- INCRBY
- DECR
- DECRBY
- STRLEN
- APPEND
- SETRANGE
- GETRANGE
- MGET
- MSET
MSETNX
I have yet to do any kind of profiling, heavy optimization, or stress testing. If nothing more hopefully Nedis will help you guys explore Redis, or how you can prototype basic databases with node. Head over to the GitHub repo for installation instructions etc.