Testing javascript that uses haml_coffee_assets (*.hamlc templates) with jasmine-headless-webkit
*.hamlc templates are great. jasmine-headless-webkit (JHW) is great. But getting the two working together is a pain.
There's a good chance you see something like window.HAML is undefined.
# spec/javascripts/helpers/spec_helper.rb require 'sprockets' require 'haml_coffee_assets' require 'haml_coffee_assets/tilt/template_handler' # JHW does not support ERB templates. However, haml_coffee_assets uses ERB # templates for its config. This pre-renders the ERB templates and passes # them to JHW. # Borrowed heavily from here: https://raw.github.com/dzello/headless_hamlc/master/spec/javascripts/helpers/spec_helper.rb Jasmine::Headless.register_engine '.hamlc', ::HamlCoffeeAssets::Tilt::TemplateHandler # this routine compiles and runs the hamlcoffee.js.coffee.erb and makes # the result available to JHW def add_haml_coffee_compiled_asset_path(asset_paths) # find the gem asset path that contains the hamlcoffee.js.coffee.erb haml_coffee_gem_asset_path = asset_paths.find { |path| path =~ /haml_coffee_assets/ } # compile the erb file into hamlcoffee.js.coffee compiled_haml_coffee_template = ERB.new( File.read(File.join(haml_coffee_gem_asset_path, "hamlcoffee.js.coffee.erb"))).result(binding) # create a tmp directory to put the compiled code in # this assumes you are running out of your project root! FileUtils.mkdir_p (tmp_haml_coffee_assets_path = File.expand_path("../../../../tmp/haml_coffee_assets/javascripts", __FILE__)) File.open(File.join(tmp_haml_coffee_assets_path, "hamlcoffee.js.coffee"), 'w') do |f| f.write compiled_haml_coffee_template end # add the new asset path containing the compiled file asset_paths << tmp_haml_coffee_assets_path # remove the original asset path from the gem, let you get the 'Skipping File' warning for the erb asset_paths.reject! { |path| path == haml_coffee_gem_asset_path } end # load any haml coffee configuration settings #require File.expand_path("../../../../config/initializers/haml_coffee_assets.rb", __FILE__) # now update the files list class to use these paths instead of the original updated_asset_paths = add_haml_coffee_compiled_asset_path(Sprockets.find_gem_vendor_paths(:for => 'javascripts')) Jasmine::Headless::FilesList.instance_variable_set(:"@asset_paths", updated_asset_paths)
Make sure you require hamlcoffee in your javascript/coffeescript.
My gem versions
jasmine-headless-webkit (0.9.0.rc.2)
haml_coffee_assets (1.14.0)














