Keeping the Console Clean in Jasmine

I like running Jasmine with the console open. It gives you insight into various errors that you may not have tests around but may still show up in the console messages. It can detect anything from poorly written tests that fail to run at all to accidentally checked-in console logs.

This approach only works if the console isn’t flooded with useless error messages. If your html has images in it that are normally served by your web server, the images will be fetched and cause 404 messages. You can sometimes get around this by serving up the images in Jasmine or relying entirely on css for your images, but this only works in simple situations. The surefire way to kill these errors is to intercept the image requests and ignore them. If you are using the Jasmine gem, this can be done by adding the following code to your jasmine_helper.rb (not yet tested in Jasmine 2.0)

The code will specifically not interfere with any requests for javascript files, but it will respond to most images with 200, content type of “image/jpeg” and no actual data. It is in a premature case statement because you may find images aren’t the only thing causing this trouble (see the Chorus version).

Try adding the middleware to your jasmine_helper and possibly tweaking it to block out any unnecessary server requests. Now your Jasmine console should be nice and clean. If it isn’t, hopefully that is telling you something useful about a fragile part of your JavaScript.