-
-
Save baphled/1101963 to your computer and use it in GitHub Desktop.
# Needed if you are not requiring rails/all | |
require 'sprockets/railtie' |
# | |
# Would be nice to have this set within jasmine.yml. | |
# | |
config.assets.precompile += %w( application.js form.js frontend.js *.css ) |
# spec/javascripts/support/jasmine_config.rb | |
# when jasmine starts the server out-of-process, it needs this in order to be able to invoke the asset tasks | |
unless Object.const_defined?(:Rake) | |
require 'rake' | |
load File.expand_path('../../../../Rakefile', __FILE__) | |
end | |
module Jasmine | |
class Config | |
include Rake::DSL | |
def js_files(spec_filter = nil) | |
# remove all generated files | |
generated_files_directory = File.expand_path("../generated", __FILE__) | |
# @TODO actually remove generated specs | |
# | |
# Think we need to include the rake DSL to make use of rm_rf now | |
# | |
rm_rf generated_files_directory, :secure => true | |
precompile_app_assets | |
compile_jasmine_javascripts | |
# this is code from the original jasmine config js_files method - you could also just alias_method_chain it | |
spec_files_to_include = spec_filter.nil? ? spec_files : match_files(spec_dir, [spec_filter]) | |
src_files.collect {|f| "/" + f } + helpers.collect {|f| File.join(spec_path, f) } + spec_files_to_include.collect {|f| File.join(spec_path, f) } | |
end | |
private | |
# this method compiles all the same javascript files your app will | |
def precompile_app_assets | |
puts "Precompiling assets..." | |
# make sure the Rails environment is loaded | |
::Rake.application['environment'].invoke | |
# temporarily set the static assets location from public/assets to our spec directory | |
# | |
# This line is now redundant as sprockets does not use static_root to store assets any more | |
# | |
#::Rails.application.config.assets.static_root = Rails.root.join("spec/javascripts/generated/assets") | |
# Sprockets seems to think that assets always belong in the public directory | |
# Though it's a bit hackish we remove the assets from the public directory after we're done | |
# | |
# Would be nicer if sprockets allowed for a way for us to precompile our assets in the generated directory | |
# Thats something to look into for later. | |
# | |
::Rake.application['assets:clean'].reenable | |
::Rake.application['assets:clean'].invoke | |
# rake won't let you run the same task twice in the same process without re-enabling it | |
# once the assets have been cleared, recompile them into the spec directory | |
::Rake.application['assets:precompile'].reenable | |
::Rake.application['assets:precompile'].invoke | |
end | |
# this method compiles all of the spec files into js files that jasmine can run | |
def compile_jasmine_javascripts | |
puts "Compiling jasmine coffee scripts into javascript..." | |
root = File.expand_path("../../../../spec/javascripts", __FILE__) | |
destination_dir = File.expand_path("../../generated/specs", __FILE__) | |
glob = File.expand_path("**/*.coffee", root) | |
Dir.glob(glob).each do |srcfile| | |
srcfile = Pathname.new(srcfile) | |
destfile = srcfile.sub(root, destination_dir).sub(".coffee", "") | |
FileUtils.mkdir_p(destfile.dirname) | |
File.open(destfile, "w") {|f| f.write(CoffeeScript.compile(File.new(srcfile)))} | |
end | |
end | |
end | |
end | |
# Note - this is necessary for rspec2, which has removed the backtrace | |
module Jasmine | |
class SpecBuilder | |
def declare_spec(parent, spec) | |
me = self | |
example_name = spec["name"] | |
@spec_ids << spec["id"] | |
backtrace = @example_locations[parent.description + " " + example_name] | |
parent.it example_name, {} do | |
me.report_spec(spec["id"]) | |
end | |
end | |
end | |
end |
For final Rails 3.1.0 release, change ::Rails.application.assets.static_root to ::Rails.application.config.assets.static_root.
Cheers for the heads up
Curious if you all have this working on Rails 3.1? I get the following error on this line 'Rake.application['assets:precompile'].invoke':
Don't know how to build task 'jasmine'
On this link (http://pivotallabs.com/users/jdean/blog/articles/1778-writing-and-running-jasmine-specs-with-rails-3-1-and-coffeescript) there is a comment (last one) on 8/30 in response to someone with the same issue, and appears to be a dead end.
Have you experienced this error and gotten past it?
PS: I an run 'rake assets:precompile' fine in the terminal... so I am thinking (probably wrongly as I dont really understand what is going on) that this has something to do with that 'jasmine' is trying to be referenced from within itself??
Hmm, I haven't but I haven't had a chance to upgrade since 3.1.0rc5. When I get the chance to I'll get back to you to confirm this.
I've had a bit of time to upgrade to 3.1.0 and I did run into similar issues. That issue seems to be from a change in the way sprockets rake tasks are stored.
There are a few big changes in sprockets rake tasks that caused me some issues also but I've finally managed to get my specs working again.
There are a few tweaks to the script that I will update shortly but things seem fine now.
No problems, hope it helps, let me know if you run into any more trouble.
Hi, just tried your revised file above and am still getting the dreaded 'Don't know how to build task 'jasmine'' error. This comes up when I run 'rake jasmine' and then load the browser (localhost:8888). Pretty weird.
I hacked a rake task together a few days ago (below) that does work, but I have to run it on its own -- if I add 'Rake::Task["jasmine"].invoke' to the end, I get the same error.
I assume that you are having no problem... so maybe the problem is local for me. This all really makes me want to ditch rails-backbone and go with standard install of backbone as all this hassle is just to get the js templates compiled. By using a different template engine/process I could just use guard to compile the coffeescripts and I guess css as well. Thoughts?
task :load_assets_for_test do
puts 'loading assets for test...'
assets_dir = "#{Rails.root}/public/assets"
spec_assets_dir = "#{Rails.root}/spec/javascripts/generated"
puts 'clearning existing assets directories...'
FileUtils.rm_rf(spec_assets_dir) if File.exists?(assets_dir)
Rake::Task["assets:clean"].invoke
puts 'precompiling assets...'
Rake::Task["assets:precompile"].invoke
puts "copying assets to #{spec_assets_dir}..."
FileUtils.cp_r(assets_dir,spec_assets_dir)
puts 'assets loaded!'
puts ''
end
Hmm, that's annoying, it could be down to the gems you have installed, could you copy your gem list here?
As far as using something other than backbone-rails, I'm not sure, it seems that the issue is more to do with your env not being able to find jasmine for one reason or another. I guess you could skate around the issue, as I did initially and just use icanhazjs to or something similar to deal with your templating issues, but I think you'll find yourself facing this issue again once your views start to build up and get more complex.
There are a few caveats to this code.