Rory Primrose

Learn from my mistakes, you don't have time to make them yourself

View project on GitHub

ReSharper Jasmine and RequireJS

Posted on January 30, 2015

I’ve been converting my code over to RequireJS and have been battling how to get this to work with the ReSharper test running which uses Jasmine. The main issue is that Jasmine runs the page on load of the browser document (the spec runner) before RequireJS has loaded dependencies. The reason for this is that RequireJS loads dependencies asynchronously. By the time they have loaded, the test run has already completed.

The fix for this is to use the Jasmine beforeEach function with the done parameter. This is something new from Jasmine 2.0 onwards. Previous versions did not use support the done callback.

The syntax looks something like this:

/// <reference path='/../../MyWebsite/Scripts/require.js'/>
/// <reference path='/../../MyWebsite/Scripts/Custom/ResponseParser.js'/>

describe('ResponseParser', function () {

    var target;

    beforeEach(function (done) {
        if (target) {
            done();

            return;
        }

        require(['ResponseParser'], function (parser) {
            target = parser;
            done();
        });
    });

    it('parse - returns empty set when data is null', function () {
        console.log('describe');

        var actual = target.parse(null);

        expect(actual).not.toBeNull();
        expect(actual.length).toBe(0);
    });

});

What happens here is before the first test is run, RequireJS is told to resolve the SUT. The done callback is only invoked once RequireJS has provided this value at which point it is stored at a scope available for the tests to run. All other tests in the describe block will call done straight away because the SUT has already been resolved.