Using Jasmine to test CoffeeScript in a Rails 3.1 App

May 12, 2011 Mike Gehard

Lately I’ve had the opportunity to use Jasmine to test drive a whole bunch of Javascript and am loving it. If you haven’t had a chance to take Jasmine for a spin, I recommend you take some time to do so.

When I heard that Rails 3.1 was going to include CoffeeScript I decided to work to figure out how I could write both my production code as well as my specs in Coffeescript.

Using this gist as a guide, I came up with these detailed instructions:

Add Guard and Guard-Coffeescript to your Gemfile and run bundle.

Run guard init to create a Guardfile and edit it to contain the following:

guard 'coffeescript', :output => 'public/javascripts/compiled' do
  watch(/^app/assets/javascripts/(.*).coffee/)
end

guard 'coffeescript', :output => 'spec/javascripts/compiled' do
  watch(/^spec/javascripts/(.*).coffee/)
end

Edit your spec/javascripts/support/jasmine.yml to have at least the following entries:

src_dir: public/javascripts/compiled
src_files:
  - **/*.js

spec_dir: spec/javascripts/compiled
spec_files:
  - **/*_spec.js

Start up the Jasmine server using rake jasmine and point your browser to http://localhost:8888. You should see 0 specs, 0 failures. Since you have changed the location that Jasmine looks for spec files, you aren’t picking up the example Jasmine specs any longer.

Create a new spec file in spec/javascripts/math_spec.coffee with the following contents:

describe 'Math:', ->
  describe 'fib()', ->
    it 'should calculate the numbers correctly up to fib(16)', ->
      fib = [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987]
      expect(Math.fib(i)).toEqual fib[i] for i in [0..16]

Refresh your browser, you should see one failing spec. Guard has compiled your .coffee file into a .js file that Jasmine will use.

Create a new implementation file in app/assets/javascripts/math.coffee with the following contents:

Math.fib = (n) ->
    s = 0
    return s if n == 0
    if n == 1
      s += 1
    else
      Math.fib(n - 1) + Math.fib(n - 2)

Refresh your Jasmine browser window and you should see 1 passing spec. Again, Guard has compiled your implementation file and Jasmine uses it to satisfy the spec.

At some point, it would be nice to figure out a way to run Jasmine specs directly from the .coffee files and against the implementation .coffee files without having to use Guard to compile them. With the above steps, you still have to check in the compiled files into source control so your tests can be run in the CI environment. If you miss one of the compiled specs or one of the compiled implementation files, your CI environment may report improper results.

If anyone has any ideas, I’d love to hear them.

About the Author

Biography

Previous
RubyGems Warningitis Outbreak
RubyGems Warningitis Outbreak

Have you upgraded RubyGems lately? Is your console suddenly filled with warnings like this? NOTE: Gem::Spe...

Next
New in Pivotal Tracker: Story tasks now draggable, enabled by default
New in Pivotal Tracker: Story tasks now draggable, enabled by default

We've made a few improvements to how story tasks work in Pivotal Tracker. They're now turned on by default ...

×

Subscribe to our Newsletter

!
Thank you!
Error - something went wrong!