Skip to content

Instantly share code, notes, and snippets.

@joselo
Forked from baphled/application.rb
Created April 13, 2012 17:53
Show Gist options
  • Save joselo/2378795 to your computer and use it in GitHub Desktop.
Save joselo/2378795 to your computer and use it in GitHub Desktop.
Jasmine config to compile coffeescript and copy assets files for testing
# 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment