Search code examples
javascriptrequirejsgruntjsjasminegrunt-contrib-jasmine

Why is grunt-contrib-jasmine hanging?


I'm asking this for posterity because it took me some time to figure this out.

I have an application that uses require.js and tests based on jasmine, automated with grunt.

I have grouped my modules into logical components, each of which is in a directory under app. My requirejs.config() call sets up path prefixes for those components like so:

paths: {
    /*... vendor libraries are listed here ...*/
    ui: 'app/ui',
    data: 'app/data',
    /* etc */
}

Then I can specify dependencies on those modules like so:

define([ 'ui/some/module', 'data/another/module' ], function(ui_module, data_module) {
    /* code for module being defined goes here */
});

My app works, and my tests pass in my web-based runner, which follows a structure similar to what I've used in the application, which is a single <script> element in the document body pointing to require.js, with a data-main set to test/index, which contains a very similar requirejs.config() call, only with additional paths for the tests themselves, and a call to window.onload(), which runs the tests.

When I use grunt-contrib-jasmine, however, the tests hang and no tests are run. The message I get is:

>> Error: timeout: Load timeout for modules: [list of modules, all my own application modules]
>> http://requirejs.org/docs/errors.html#timeout at
>> http:/127.0.0.1:8000/_SpecRunner.html:21 
>> http:/127.0.0.1:8000/.grunt/grunt-contrib-jasmine/require.js:12 v
>> http:/127.0.0.1:8000/.grunt/grunt-contrib-jasmine/require.js:14 C
>> http:/127.0.0.1:8000/.grunt/grunt-contrib-jasmine/require.js:14 

How do I get my tests to pass under grunt?


Solution

  • It turns out that the path prefixes in my requirejs.config() call should contain a trailing slash:

    paths: {
        /*... vendor libraries are listed here ...*/
        ui: 'app/ui/',
        data: 'app/data/',
        /* etc */
    }
    

    So, 'app/ui/' instead of 'app/ui'.

    I'm still not sure as to the underlying cause of this, because both versions work in the browser; perhaps it is a different version of require.js being used by the grunt task, or perhaps it is something peculiar to phantom.

    Leaving this question and answer so that it might save someone some time.