Skip to content

Instantly share code, notes, and snippets.

@r10r
Forked from lamikae/jhw-assets.md
Created May 16, 2012 17:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save r10r/2712515 to your computer and use it in GitHub Desktop.
Save r10r/2712515 to your computer and use it in GitHub Desktop.
Jasmine headless testing on Rails 3 asset pipeline

Jasmine headless testing on Rails 3 asset pipeline

  • assets and specs can be CoffeeScript
  • 3rd party JS can be placed to vendor/assets/javascripts or vendor gems
  • detects gem-vendored JS via application.js require
  • uses jasmine-headless-webkit to run the suite outside the browser
    • this requires Qt4.7 installation

Gemfile

group :development, :test do
  # jasmine 1.2.0.rc2 and jhw 0.9.0.rc1
  gem 'jasmine', :git => "git://github.com/pivotal/jasmine-gem.git"
  gem 'jasmine-headless-webkit', :git => "git://github.com/johnbintz/jasmine-headless-webkit.git"
end

# add gems that vendor 3rd party JS
gem "jquery-rails"

app/assets/javascripts/application.js

//= require jquery

Jasmine init

$ bundle install
$ bundle exec rails g jasmine:install

spec/javascripts/support/jasmine.yml

# src_files
#
# An array of filepaths relative to asset_paths to include.
#
src_files:
  - application.js   # require gem vendored assets (such as jQuery) in application.js
  - some.plugin.js   # loaded from vendor/assets/javascripts  

  - app/assets/javascripts/*.js.coffee   # load all custom CS files
  - my.custom.js.coffee                  # ..or individually without path prefix

# asset_paths
#
# Paths you would like to be served by the Sprockets asset pipeline.
#
# If you include your spec_dir (eg: - spec/javascripts ) here,
# Jasmine will use the Sprockets asset pipeline to build your spec files.
#
asset_paths:
  - app/assets/javascripts
  - vendor/assets/javascripts

spec_dir: spec/javascripts

spec_files:
  - "**/*[sS]pec.js.coffee"

# helpers
#
# Spec helpers relative to spec_dir to include.
#
helpers:
  - helpers/**/*.js.coffee

spec/javascripts/example_spec.js.coffee

describe 'example', ->
    it 'should have jQuery', ->
        expect(jQuery).toBeDefined()

Run

$ bundle exec rake jasmine:headless
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment