RequireJS + Rails + Jasmine
This repository is a tutorial and straw-man design proposal for integrating
Jasmine test support into a Rails app using RequireJS
requirejs-rails gem. My ultimate goal is to see all of this wrapped
up so that it's as easy as adding a gem to your app's Gemfile.
it should play nicely with the Asset Pipeline
Rails' Asset Pipeline provides useful conventions, plugins, workflow and tools. As much as possible, I'd like to preserve that while bringing the benefits of RequireJS and Jasmine into play.
A suite of AMD source code in a Rails app makes its dependencies
define() calls. The Jasmine configuration
should not make the developer reiterate this knowledge.
One approach by Michael Kessler uses guard to transpile and assemble the test suite from sources and specs. This is handy for general Ruby apps without Sprockets. For a Rails app, it fails to be DRY and doesn't respect the configuration and processing built into the Asset Pipeline.
Scott Burch's jasmine-require
helper is another integration approach. I honestly never did get it
configured and working with
requirejs-rails, but that's probably a (lack of)
documentation issue. Setup aside, Scott's
requireStubs() call is a nice
idea worth keeping in mind.
My personal bias is that I would prefer a "native-AMD" module style rather than a Jasmine helper.
This section explains the changes made to enable Jasmine support with
requirejs-rails as described in the Introduction.
Based on the above discussion, here are some guidelines this template adheres to:
- Asset path compatibility: Specs should reference source files using the same path structure as the source files themselves use.
- Specs live in
- Helpers live in
Add these lines to your
Gemfile, then run
gem 'requirejs-rails', '~> 0.8.0' group :development, :test do gem 'jasmine', :git => 'git://github.com/pivotal/jasmine-gem.git' end
Run the jasmine generator
rake g jasmine:install
Update development asset paths
Add this line to
Add files from this repository
You'll need to add these files, in these relative locations, to your Rails app:
Create your specs in
in that directory. To run it, start the jasmine server via the usual
rake jasmine and navigate to
A spec in CoffeeScript would look like this:
define (require) -> PlaylistModel = require('models/playlist_model') describe 'existence', -> it 'should be defined', -> expect(PlaylistModel).toBeDefined()