0

I'm running BDD tests using the grunt-contrib-jasmine plugin. When I generate the _SpecRunner.html and run the tests through Chrome everything is fine. When I run the tests via PhantomJS I receive error messages which indicate that jQuery is not loaded.

The file that I'm testing uses LABjs to load jQuery, jQueryUI, and few other files that depend on them. This tag will be placed on a third party site so this script is required to load jQuery.

(function(){
    function callback() {
        $LAB
        .script('path_to_jQuerry').wait()
        .script('path_to_jQueryUI').wait()
        .script('files_that_use_jQuery').wait();
    }
    loadJS('path_to_LABjs', callback);
})();

Assume that loadJS successfully loads LABjs. Like I said, when I run the tests on this file through the browser there are no errors, but using grunt-contrib-jasmine via PhantomJS I receive the following error before any of my tests complete:

TypeError: 'undefined' is not a function (evaluating '$.publish("foo")')

The code that produces looks like:

(function($) {
    $.publish('foo');
})(jQuery);

My initial thought is that PhantomJS isn't compatible with the LABjs script loader or that the code is 'evaluated' but PhantomJS at the wrong time. Snooping around the grunt-contrib-jasmine, jasmine, and phantomjs code hasn't gotten me anywhere.

Any comment is appreciated.

johnsorrentino
  • 2,731
  • 1
  • 16
  • 21

1 Answers1

0

I came to the conclusion that grunt-contrib-jasmine does not accurately mimic the tests as if they were run in the browser due to the dynamic loading of the JavaScript with LABjs. The module begins evaluating the JavaScript on the page before it has finished loading and before the tests begin. This may have to do with how grunt-contrib-jasmine uses the onPageLoad event, but I'm not sure of the specific cause. Manually testing the with the _SpecRunner.html file in the browser produces no errors. In the end I removed the dynamic loading from the JavaScript file and will test the dynamic loading another way.

johnsorrentino
  • 2,731
  • 1
  • 16
  • 21