Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I effectively use Jasmine to test javascript assets packaged via Jammit?

I have a rails app that is combining javascript assets using Jammit, and I'd like to use Jasmine for BDD-style testing of my javascript. I'm wondering if anyone has any advice on accessing the Jammit-generated 'pacakges' from within Jasmine?

The issue is that Jasmine is configured by defining a list of JS files on disk to test, and it then includes those files within its own test runner page which is loaded and run in a browser.

I could reference each of the individual JS files inside of the jasmine.yml config file before they're packaged with Jammit... however, Jammit is already dealing with dependencies between files for me, and, more importantly, I also need access to the compiled javascript templates that Jammit produces.

I could also manually run Jammit to generate the compiled assets first and then run Jasmine, but I'd wind up having to re-generate the assets by hand before each test run in order to test changes, which would seriously cramp a fast test-driven type workflow.

I'm wondering if I could somehow:

  • Mount the Jammit controller from within Jasmine's rack server so it could serve out the packages from Jasmine? This would basically work the same way as Jammit already does from within Rails' development env.
  • Hook into Jasmine somehow to package the assets on every page load before the tests are executed? This would be slower, but would save me a step and ensure things were up to date.

Any suggestions? I'm just getting started with this, so I could be going about it all wrong. Any advice would be greatly appreciated. :-)

Thanks! -John

like image 717
John Reilly Avatar asked Apr 29 '11 15:04

John Reilly


2 Answers

Here's the magic combo you're looking for:

  1. Use the guard gem along with the guard-jammit gem to watch for changes to your project's assets
  2. Install the LiveReload plugin in the browser of your choice and install the guard-livereload gem so you can have your browser auto reload whenever your specs or assets change.
  3. Fire up guard, then rake jasmine, then load your jasmine specs in your browser, then press the live-reload button to connect to the live-reload server that guard started
  4. Change your files. Watch the magic happen as guard runs jammit and instructs your browser to refresh the jasmine specs.

Here's an example Guardfile to get you started:

guard 'jammit' do
  watch(%r{public/javascripts/(.*)\.js})
  watch(%r{app/views/jst/(.*)\.jst})
  watch(%r{public/stylesheets/(.*)\.css})
end

guard 'livereload', :apply_js_live => false do
  watch(%r{app/.+\.(erb|haml)})
  watch(%r{app/helpers/.+\.rb})
  watch(%r{public/.+\.(css|js|html)})
  watch(%r{config/locales/.+\.yml})
  watch(%r{spec/javascripts/.+\.js})
end
like image 115
Gabe Hollombe Avatar answered Sep 21 '22 23:09

Gabe Hollombe


I've come up with an OK solution: force Jammit to reload and package when jasmine starts. To do this, you need to edit the jasmine_config.rb file:

require 'jammit'
module Jasmine
  class Config

    Jammit.reload!
    Jammit.package!

   end
end

I wrote a bit more detailed post about it here: http://www.rebeccamiller-webster.com/2011/05/jammit-jasmine-bdd/

like image 22
rmw Avatar answered Sep 20 '22 23:09

rmw