Skip to content

Instantly share code, notes, and snippets.

@TorstenRobitzki
Created November 24, 2012 12:36
Show Gist options
  • Save TorstenRobitzki/4139498 to your computer and use it in GitHub Desktop.
Save TorstenRobitzki/4139498 to your computer and use it in GitHub Desktop.
test:scripts rake task to unit-test scripts with jasmine in a rails application
module JasminPhantomHelper
def self.manifest_file_name
@cached_name ||= if Pland::Application.config.assets.manifest
path = File.join(cPland::Application.onfig.assets.manifest, "manifest.yml")
else
path = File.join(Rails.public_path, Pland::Application.config.assets.prefix, "manifest.yml")
end
@cached_name
end
def self.read_manifest filter
YAML::load( File.open( manifest_file_name ) ).keys.keep_if{ | f | !( f =~ filter ).nil? }
end
def self.script_files
read_manifest /.*\.js$/
end
def self.css_files
read_manifest /.*\.css$/
end
def self.full_public_path file
File.join(Rails.public_path, Pland::Application.config.assets.prefix, file)
end
end
namespace :test do
TMP_DIR = Rails.root.join( 'tmp' ).join( 'jasmin' )
SCRIPT_DIR = Rails.root.join( 'test' ).join( 'scripts' )
SCRIPT_FILE_NAME = TMP_DIR.join( 'run_tests.js' )
HTML_FILE_NAME = TMP_DIR.join( 'run_tests.html' )
PAGE_HEIGHT = 800
PAGE_WIDTH = 1000
ASSETS_EXCLUDED = %w( admin.js )
TEST_FILES = FileList[ SCRIPT_DIR.join( '*.coffee' ).to_s, SCRIPT_DIR.join( '*.js' ).to_s ]
directory TMP_DIR.to_s
file JasminPhantomHelper.manifest_file_name
file HTML_FILE_NAME => [ TMP_DIR, JasminPhantomHelper.manifest_file_name ] do
@css_files = JasminPhantomHelper.css_files
skeleton = ERB.new(<<-'END_OF_SEKELTON').result( binding )
<!DOCTYPE html>
<html lang="en-US">
<head>
<% @css_files.each do | file | %>
<% unless ASSETS_EXCLUDED.member? file %>
<link href="file://localhost<%= JasminPhantomHelper.full_public_path( file ) %>" media="all" rel="stylesheet" type="text/css" />
<% end %>
<% end %>
</head>
<body>
</body>
</html>
END_OF_SEKELTON
File.open( HTML_FILE_NAME, 'w+' ) do | outfile |
outfile.write skeleton
end
end
file SCRIPT_FILE_NAME => [ TMP_DIR, TEST_FILES, SCRIPT_DIR, __FILE__, JasminPhantomHelper.manifest_file_name ].flatten do
@jasmin_dir = File.expand_path( Gem::Specification.find_by_name("jasmine-core").gem_dir() + '/lib/jasmine-core' )
@support_dir = File.expand_path( File.dirname(__FILE__) + '../../jasmine' )
@script_files = JasminPhantomHelper.script_files
script = ERB.new(<<-'END_OF_SCRIPT').result( binding )
var page = require('webpage').create();
page.viewportSize = { width: <%= PAGE_WIDTH %>, height: <%= PAGE_HEIGHT %> };
phantom.viewportSize = { width: <%= PAGE_WIDTH %>, height: <%= PAGE_HEIGHT %> };
page.open( "file://localhost<%= HTML_FILE_NAME %>", function( status ) {
if ( status != 'success' ) {
console.log( 'error opening ' + <%= HTML_FILE_NAME %> );
phantom.exit( 1 );
}
( function() {
var load = function( name ) {
if ( !page.injectJs( name ) ) throw ( "error loading " + name );
}
load( "<%= @jasmin_dir %>/jasmine.js" );
load( "<%= @support_dir %>/console-runner.js" );
<% JasminPhantomHelper.script_files.each do | script_file | %>
<% unless ASSETS_EXCLUDED.member? script_file %>
load( "<%= JasminPhantomHelper.full_public_path( script_file ) %>" );
<% end %>
<% end %>
<% TEST_FILES.to_ary.each do | test_file | %>
console.log("loading: <%= test_file %>" );
load( "<%= test_file %>" );
<% end %>
} )();
var result = page.evaluate( function() {
reporter = new jasmine.ConsoleReporter();
try {
/* Hack, to let the Test queue execute elements */
jasmine.Env.prototype.setTimeout = function(f){ f(); }
jasmine.getEnv().addReporter( reporter );
jasmine.getEnv().execute();
return { 'status': reporter.status, 'output': reporter.output };
}
catch (err)
{
reporter.log(err, "red");
return { 'result': 'error', 'output': reporter.output };
}
} );
console.log( result.output.join( "\n" ) );
phantom.exit( result.status == "success" ? 0 : 1 );
} );
END_OF_SCRIPT
File.open( SCRIPT_FILE_NAME, 'w+' ) do | outfile |
outfile.write script
end
end
# as long as not all prerequisite for the asset files are known, the file must be build each time
task :build_asset_file => [ TMP_DIR, 'assets:precompile', SCRIPT_FILE_NAME, HTML_FILE_NAME ]
namespace :scripts do
task :cached do
sh "phantomjs #{SCRIPT_FILE_NAME}"
end
end
desc "run all jasmine test with phantomjs (use tests:scripts:cached if no asset was changed)"
task :scripts => [ :build_asset_file, 'test:scripts:cached' ]
end
# adding test:script to the list of all tests
task :test => 'test:scripts'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment