Rails 3.1 & Jasmine: Including JavaScripts from gems
-
19-04-2021 - |
Question
I have a rails 3.1 project that I'd like to run some jasmine specs on. Problem is, my rails project requires some javascript files that are sourced from gems (most particularly backbone-rails), not my /assets directory.
Looking at the generated jasmine file's HTML, it's successfully including all assets from the JavaScripts /assets directory (compiled from coffeescript), but no javascript files generated by gems. Consequently I'm getting a bunch of 'class not found' errors.
So, any ideas on getting jasmine to require Rails 3.1 JavaScript files from gems, not just /assets? Current jasmine.yml included below. Any ideas appreciated
src_files:
- public/javascripts/prototype.js
- public/javascripts/effects.js
- public/javascripts/controls.js
- public/javascripts/dragdrop.js
- public/javascripts/application.js
- public/javascripts/**/*.js
- spec/**/*_spec.js
- "vendor/**/*.{js, coffee}"
- "lib/**/*.{js, coffee}"
- "app/**/*.{js, coffee}"
stylesheets:
- stylesheets/**/*.css
helpers:
- helpers/**/*.js
spec_files:
- '**/*[sS]pec.{js, coffee}'
src_dir: assets
spec_dir: spec/javascripts
Solution
I highly recommend using jasmine-headless-webkit
to do jasmine testing in rails 3.1 apps. Here's my jasmine.yml
file:
src_dir: app/assets/javascripts
asset_paths:
- app/assets/templates
- lib/assets/javascripts
- vendor/assets/javascripts
src_files:
- [ 'application.js' ]
spec_dir: spec/javascripts
spec_files:
- "**/*[Ss]pec.*"
helpers:
- "helpers/**/*"
I'm using that with jasmine-headless-webkit
to run my tests on the command-line (using watchr
to run them automatically when a file is changed)
It's a wonderful way to test.
OTHER TIPS
Fairly recent commits to jasmine master enable it to source javascripts from the asset pipeline. It works great.