Skip to content

Instantly share code, notes, and snippets.

@matthijsgroen
Created November 29, 2011 10:35
Show Gist options
  • Save matthijsgroen/1404347 to your computer and use it in GitHub Desktop.
Save matthijsgroen/1404347 to your computer and use it in GitHub Desktop.
Rails 3.1 Jasmine testing
# src_files
#
# Return an array of filepaths relative to src_dir to include before jasmine specs.
# Default: []
#
# EXAMPLE:
#
# src_files:
# - lib/source1.js
# - lib/source2.js
# - dist/**/*.js
#
src_files:
- spec/javascripts/generated/assets/action_screen*.js
# stylesheets
#
# Return an array of stylesheet filepaths relative to src_dir to include before jasmine specs.
# Default: []
#
# EXAMPLE:
#
# stylesheets:
# - css/style.css
# - stylesheets/*.css
#
stylesheets:
- stylesheets/**/*.css
# helpers
#
# Return an array of filepaths relative to spec_dir to include before jasmine specs.
# Default: ["helpers/**/*.js"]
#
# EXAMPLE:
#
# helpers:
# - helpers/**/*.js
#
helpers:
- test_helpers/**/*.js
# spec_files
#
# Return an array of filepaths relative to spec_dir to include.
# Default: ["**/*[sS]pec.js"]
#
# EXAMPLE:
#
# spec_files:
# - **/*[sS]pec.js
#
spec_files:
- '**/*[sS]pec.js'
# src_dir
#
# Source directory path. Your src_files must be returned relative to this path. Will use root if left blank.
# Default: project root
#
# EXAMPLE:
#
# src_dir: public
#
src_dir:
# spec_dir
#
# Spec directory path. Your spec_files must be returned relative to this path.
# Default: spec/javascripts
#
# EXAMPLE:
#
# spec_dir: spec/javascripts
#
spec_dir: spec/javascripts
# 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
def js_files(spec_filter = nil)
# remove all generated files
generated_files_directory = File.expand_path("../../generated", __FILE__)
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..."
ENV["RAILS_GROUPS"] ||= "assets"
ENV["RAILS_ENV"] ||= "test"
# make sure the Rails environment is loaded
#::Rake.application['environment'].reenable
::Rake.application['environment'].invoke
config = Rails.application.config
config.assets.compile = true
config.assets.digest = false
config.assets.digests = {}
env = Rails.application.assets
target = Rails.root.join("spec", "javascripts", "generated", "assets")
compiler = Sprockets::StaticCompiler.new(env,
target,
config.assets.precompile,
:manifest_path => config.assets.manifest,
:digest => config.assets.digest,
:manifest => false)
compiler.compile
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("**/*.js.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