Jasmine in Rails 3

September 23, 2010 Davis W. Frank

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.

About the Author

Biography

More Content by Davis W. Frank
Previous
Honk looking for Rails Developers in San Francisco
Honk looking for Rails Developers in San Francisco

At Pivotal Labs, one of the services we provide our clients is helping them interview and hire. Pivotal Lab...

Next
New Tech Talk: Semantic Logging with Splunk
New Tech Talk: Semantic Logging with Splunk

Most events are written by developers to help them debug server side functionality. Semantic events are wri...

Enter curious. Exit smarter.

Learn More