Skip to content

Instantly share code, notes, and snippets.

@lamikae
Created February 5, 2012 12:28
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save lamikae/1745214 to your computer and use it in GitHub Desktop.
Save lamikae/1745214 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
@johnbintz
Copy link

Great! I think the only change is that the asset paths key in jasmine.yml should be asset_paths instead of asset_pipeline_paths.

@lamikae
Copy link
Author

lamikae commented Feb 5, 2012

You are absolutely correct. :)

Any idea why jquery will not be loaded from vendored gem if required in src_files as "jquery.js", but can be found if required in application.js?

@johnbintz
Copy link

Do you have jquery-rails in your Gemfile, too?

@lamikae
Copy link
Author

lamikae commented Feb 5, 2012

Yes, added some comments about it.

@johnbintz
Copy link

What does jasmine-headless-webkit -l return? Is jquery-rails in the paths returned?

@lamikae
Copy link
Author

lamikae commented Feb 6, 2012

Yes, jquery-rails assets incl. jquery.js is reported by the -l switch.
Still, jquery.js will not load if given explicitly in src_files, it needs to be required through application.js.

@johnbintz
Copy link

That sounds like a bug more than anything, probably a good one for me to squash before 0.9 release. Can you write up a quick issue on the JHW project? I'll look at it later today.

@lamikae
Copy link
Author

lamikae commented Feb 6, 2012

@javadoug
Copy link

I replaced headless gem with this one. It fixed an issue I was having with jasmine referencing jquery-rails installed jquery. Thank you for these instructions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment