Davis W. FrankDavis W. Frank
Jasmine in Rails 3
edit Posted by Davis W. Frank on Thursday September 23, 2010 at 11:47AM

UPDATE: We've released a new version of the Jasmine Gem and these steps are no longer required. Please see the announcement here


Bad news, everyone! At the moment, the released Jasmine gem is not yet Rails 3 compatible.

Good news, everyone! We now have a short term workaround while we deal with the work needed to work seamlessly with Rails 3.

The Jasmine gem is the easiest way to integrate Jasmine into your web project, including Rails. It adds auto discovery of source & spec files, a tiny Rack app for running them, and a Continuous Integration task (using Selenium) to run your tests on every check in. We use this gem on all of our Rails projects today.

It uses RSpec to gather results from JavaScript making them available at the command line or part of your build results. And RSpec 2, which is required for Rails 3, has changed its interface. So the Jasmine gem is broken with Rails 3. We know what we need to do to fix the this, but until then here's the workaround.

But what if I'm not using Rails?

This fix is only for Rails 3. If you project is not using Rails or does not require RSpec 2, then you can ignore this post.

How the fix works

We're going to use Bundler to install the Jasmine gem from a local repo copy of a branched version of what's on Github. Got it?

The Fix

Once you've already created your Rails 3 project, here's a script that does the right things to vendor the Jasmine gem:

What you see above:

  • making a vendor/gems directory if you don't already have one
  • getting the Jasmine gem code: cloning the Jasmine gem, getting Jasmine itself (as a submodule), then getting & checking out the correct branch
  • copying some files from the Jasmine repo up into the gem's directories (a step Bundler can't do for us)

Once this is done, you need to add this line to your Gemfile, likely in a test group (if you have one):

gem 'jasmine', :path => 'vendor/gems/jasmine'

Run a bundle install to get the Jasmine gem installed.

cd ../../..
bundle install

Now you can run the generator to make your Rails project a Jasmine project.

bundle exec jasmine init

Try running the example Jasmine specs:

rake jasmine

...and hit http://localhost:8888. You should see 5 specs pass.

Now you'll need to edit jasmine.yml and point to your source, spec, and helper files. Delete the example files once you get your specs up and working.

Lastly, you need to check in the Jasmine gem source into your project so that other development machines and your CI box can run your Jasmine specs.

  • Remove the .git directories in vendor/gems/jasmine and vendor/gems/jasmine/jasmine
  • Remove the .gitignore files in vendor/gems/jasmine and vendor/gems/jasmine/jasmine
  • Add vendor/gems/jasmine to your repo directly (git add vendor/gems/jasmine)

That should do it.


UPDATE: There was an issue in the repo branch that has been fixed.

Using these instructions the first time, you should be fine.

If you've already run them, then you need to:

cd vendor/gems/jasmine
git pull
cd ../../..
bundle install

And then rake jasmine:ci should work fine.

Comments

  1. George Anderson George Anderson on September 23, 2010 at 10:14PM

    Check your "Jasmine gem" link. It should point to:

    https://rubygems.org/gems/jasmine

  2. Davis W. Frank Davis W. Frank on September 24, 2010 at 10:37AM

    George: thanks! I've fixed the link.

    I've also fixed the repo so that rake jasmine:ci works - check out the end of the post.

  3. Matt Simpson Matt Simpson on September 29, 2010 at 10:20AM

    you also have a typo

    gem jasmine, :path => 'vendor/gems/jasmine'

    should be

    gem 'jasmine', :path => 'vendor/gems/jasmine'

    missing the quotes. Great article, thanks.

  4. Davis W. Frank Davis W. Frank on September 29, 2010 at 01:38PM

    Fixed! Thanks, Matt.

  5. Andreas Haller Andreas Haller on October 19, 2010 at 08:37AM

    Hi instead of vendoring the gem, i used The Bundler and added this to my Gemfile:

    gem "jasmine",
      :git => "git://github.com/pivotal/jasmine-gem.git",
      :branch => "rspec2-rails3",
      :submodules => true
    

    (I added this in a :test group block.) With todays version of the branch i still had to to copy the examples afterwards: bundle show jasmine cd [the jasmine folder] ruby copy_examples.rb

    Then proceed like you descriped in this article.