grunt-contrib-jasmine with PhantomJS, EJS templates and XMLHttpRequest Exception 101

588 Views Asked by At

I'm trying to set up a basic unit test environment for a simple project, but I'm hitting a weird hurdle I have not been able to overcome after hours of research.

For some reason, I am getting Error: NETWORK_ERR: XMLHttpRequest Exception 101 returned directly from EJS trying to load the template using XHR, but only once in a while.

The case is simple, I have a backbone application instantiating a backbone view that renders an EJS template. This looks like so :

BackboneView = Backbone.View.extend({
  initialize: function() {
    this.template = new EJS({url: '/app/views/root/root.template.ejs'});
  }
});

To setup grunt-contrib-jasmine, I have the following snippet in my Gruntfile :

jasmine: {
  cms: {
    src: "app/**/*.js"
    options: {
      specs: "tests/fe/spec/*Spec.js",
      vendor: [
          "vendor/jquery/dist/jquery.js",
          "vendor/underscore/underscore.js",
          "vendor/backbone/backbone.js",
          "vendor/ejs/ejs.js",
      ],
      keepRunner : true,
      outfile : "tests/fe/_SpecRunner.html",
      host : "http://0.0.0.0:8000"
    }
  }
}

I'm able to run my test suite most of the time :

$ grunt jasmine
Running "jasmine:cms" (jasmine) task
Testing jasmine specs via PhantomJS

log: came in request!, /app/views/root/root.template.ejs

log: 200

log: /app/views/root/root.template.ejs

 website.header
   header.test()
     - test should return 10...
log: 
     ✓ test should return 10

1 spec in 0.008s.
>> 0 failures

Done, without errors.

But every now and then, I will get the console spitting this at me :

$ grunt jasmine
Running "jasmine:cms" (jasmine) task
Testing jasmine specs via PhantomJS

log: came in request!, /app/views/root/root.template.ejs

log: ERROR in request, Error: NETWORK_ERR: XMLHttpRequest Exception 101

log: /app/views/root/root.template.ejs
>> Error caught from PhantomJS. More info can be found by opening the Spec Runner in a browser.
Warning: There is no template at /app/views/root/root.template.ejs Use --force to continue.

Aborted due to warnings.

The logs were added in the relevant part of the EJS engine to help me debug. That would be :

// The newRequest function for reference 
// to see how EJS builds its XHR object
EJS.newRequest = function(){
   var factories = [
     function() { 
       return new ActiveXObject("Msxml2.XMLHTTP");
     },
     function() { return new XMLHttpRequest(); },
     function() { return new ActiveXObject("Microsoft.XMLHTTP"); }
   ];

   for(var i = 0; i < factories.length; i++) {
        try {
            var request = factories[i]();
            if (request != null)  return request;
        }
        catch(e) { continue;}
   }
}

// And the relevant method in which I added 
// some debug trace to see what was happening
EJS.request = function(path){
   console.log("came in request!", path);

   var request = new EJS.newRequest()
   request.open("GET", path, false);

   try{request.send(null);}
   catch(e){console.log("ERROR in request", e); return null;}

   console.log(request.status);

   if ( request.status == 404 || request.status == 2 ||
        (request.status == 0 && request.responseText == '') ) {
     return null;
   }

   return request.responseText
}

I can run grunt jasmine 6-7 times in a row and have it work seamlessly. Suddenly a try will simply fail, the subsequent try will most likely be back to normal. Until it breaks again. And all this without me touching anything anywhere!

I'm really starting to lose hope on tackling this issue. Here are a few sources I've gone through that did not work :

Anyone got an idea on what could be causing this or a hint on what to do next to try and debug the issue?

PS: I feel my question is a bit overwhelmed with code snippet, please let me know if I could improve the question in any way.

0

There are 0 best solutions below