Using Jammit with Jasmine

At SupportBee, we are big fans of testing our javascript code using Jasmine. We also use jammit for managing assets (js, css etc) in our rails app and we wanted to avoid including js files manually in jasmine.yml file. The solution is to mount Jammit as a rack middleware in Jasmine and let it handle requests to /assets. First of all, you need to use this port of Jammit. To do so, put this in your Gemfile

gem ‘jammit’, :git => ‘git@github.com:prateekdayal/jammit.git’, :branch => ‘rack_middleware’

Now you need a way to tell jasmine to use assets defined in assets.yml. We will add a new lib_files section for this. So our jasmine.yml file will look like

common, app_assets and app are namespaces I have defined in my assets.yml file. Also notice that some files are duplicated in the src_files folder to make sure tests work in your continuous integration environment (where you don’t have/need this hack).

To make this work, you have to make changes in two files in your installed jasmine gem. You can find the location of gem with bundle show jasmine. The changes can be found in this commit. Everytime you upgrade jasmine, you will have to redo this step. Unfortunately, until there is an official way to extend jasmine for such use cases, we have to resort to this ugly hack.

Once you are done, remember to delete compiled assets in public/assets folder and restart your jasmine server. If everything works, you should see ‘Mounting Jammit for Assets’ in the output of the rake command that runs jasmine’s web server. <div id="_mcePaste">% bundle exec rake jasmine </div> <div id="_mcePaste">(in /Users/prateekdayal/work/supportbee/app)</div> <div id="_mcePaste">your tests are here:</div> <div id="_mcePaste">http://localhost:8888/</div> <div id="_mcePaste">Mounting Jammit for Assets</div> <div id="_mcePaste">[2011-07-05 10:48:59] INFO WEBrick 1.3.1</div> <div id="_mcePaste">[2011-07-05 10:48:59] INFO ruby 1.9.2 (2011-02-18) [x86_64-darwin10.7.0]</div> <div id="_mcePaste">[2011-07-05 10:48:59] INFO WEBrick::HTTPServer#start: pid=14761 port=8888</div>



blog comments powered by Disqus
Hana Mohan
Hana Mohan