What a title. Ugh.

I spent a while trying to figure out if there was a way that I could use should.js BDD-style asserts and have WebStorm’s integrated test runner understand the results. It appears that nodeunit is integrated nicely, but you have to write nodeunit tests. Here’s a little hack I came up with to let me use should.js:

var should = require('should');
var assertFn = should.Assertion.prototype.assert;

function describe(name, fn) {
    exports[name] = function (testFn) {
        should.Assertion.prototype.assert = function () {
            testFn.doesNotThrow(Function.prototype.apply.bind(assertFn, this, arguments));
        };
        fn(testFn.expect);
        testFn.done();
    }
}

describe('foo', function () {
    true.should.be.ok;
});
describe('bar', function () {
    false.should.be.ok;
});

 

should.js extends Object with a number of items, but they all create a new Should.Assertion. Here, I’m creating a nodeunit-style function that wraps should.Assertion in a nodeunit test so that the counts and itemization come out right. Then I call the actual test code with test.expect as a parameter (for convenience), and all should-style assertions will go through the wrapper and get counted as nodeunit tests. Finally I call test.done() because, well, you’re s’posed to.

Post filed under Code.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>