javascriptruby-on-railsunit-testingjammitjasmine

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:

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


Solution

  • 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