Skip to content

Instantly share code, notes, and snippets.

@teamon
Last active December 19, 2015 19:08
Show Gist options
  • Save teamon/6003661 to your computer and use it in GitHub Desktop.
Save teamon/6003661 to your computer and use it in GitHub Desktop.

Setup

npm install -g karma
rake test

Odpala na porcie 3333 webricka z apka w envie test a potem testy angular scenarios pod karma gdzie result jest przerzucany do minitesta

# test/frontend/frontend_test.rb
require File.expand_path("../../test_helper", __FILE__)
require "rack/server"
require "net/http"
require "webrick"
class Karma
def initialize(app)
@app = app
@thread = nil
@host = "127.0.0.1"
@port = 3333
end
def call(env)
if env["PATH_INFO"] == "/__identify__"
[200, {}, [@app.object_id.to_s]]
else
begin
@app.call(env)
rescue StandardError => e
@error = e unless @error
raise e
end
end
end
def up?
return false if @thread && @thread.join(0)
res = Net::HTTP.start(@host, @port) { |http| http.get('/__identify__') }
if res.is_a?(Net::HTTPSuccess) or res.is_a?(Net::HTTPRedirection)
return res.body == @app.object_id.to_s
end
rescue SystemCallError
return false
end
def access_log
file = File.open(File.expand_path("../../../log/test.log", __FILE__), "a+")
log = WEBrick::Log.new(file)
[log, WEBrick::AccessLog::COMBINED_LOG_FORMAT]
end
def boot
@thread = Thread.new do
Rack::Server.start(
:Host => @host,
:Port => @port,
:app => self,
:AccessLog => [access_log],
:environment => "test"
)
end
Timeout.timeout(60) { @thread.join(0.1) until up? }
rescue Timeout::Error
raise "Rack application timed out during boot"
else
self
end
def test
system "karma start test/frontend/karma.conf.js --single-run"
end
def shutdown
@thread.kill
end
def run
boot
res = test
shutdown
res
end
end
describe "Frontend" do
it "must pass karma tests" do
ENV["RAILS_ENV"] = "test"
ENV["RACK_ENV"] = "test"
require ::File.expand_path('../../../config/environment', __FILE__)
Karma.new(Rails.application).run.must_equal true
end
end
source 'https://rubygems.org'
source 'http://rails-assets.org'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.0.0'
# Use sqlite3 as the database for Active Record
gem 'sqlite3'
gem 'sass-rails', '~> 4.0.0'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.0.0'
gem "rails-assets-angular"
group :test do
gem "minitest-rails"
end
// test/frontend/karma.conf.js
// Karma configuration
// Generated on Mon Jul 15 2013 21:11:58 GMT+0200 (CEST)
// base path, that will be used to resolve files and exclude
basePath = '../..';
// list of files / patterns to load in the browser
files = [
ANGULAR_SCENARIO,
ANGULAR_SCENARIO_ADAPTER,
'test/frontend/**/*_test.coffee'
];
// list of files to exclude
exclude = [
];
// test results reporter to use
// possible values: 'dots', 'progress', 'junit'
reporters = ['progress'];
// web server port
port = 9876;
// cli runner port
runnerPort = 9100;
// enable / disable colors in the output (reporters and logs)
colors = true;
// level of logging
// possible values: LOG_DISABLE || LOG_ERROR || LOG_WARN || LOG_INFO || LOG_DEBUG
logLevel = LOG_INFO;
// enable / disable watching file and executing tests whenever any file changes
autoWatch = true;
// Start these browsers, currently available:
// - Chrome
// - ChromeCanary
// - Firefox
// - Opera
// - Safari (only Mac)
// - PhantomJS
// - IE (only Windows)
browsers = [
'Chrome',
'Safari',
'Firefox'
];
// If browser does not capture in given timeout [ms], kill it
captureTimeout = 60000;
// Continuous Integration mode
// if true, it capture browsers, run tests and exit
singleRun = false;
proxies = {
'/': 'http://localhost:3333/'
};
urlRoot = '/__karma__/';
# test/frontend/scenarios/main_test.coffee
describe "my app", ->
beforeEach ->
browser().navigateTo "/"
it "should have 4 items", ->
expect(repeater('ul li').count()).toEqual(4)
# lib/tasks/test.rake
require 'rake/testtask'
Rake::TestTask.new do |t|
t.libs.push "lib"
t.test_files = FileList['test/**/*_test.rb']
t.verbose = true
end
task :default => :test
# config/environments/test.rb
config.eager_load = true
# test/test_helper.rb
ENV["RAILS_ENV"] = "test"
require File.expand_path("../../config/environment", __FILE__)
require "rails/test_help"
require "minitest/rails"
# To add Capybara feature tests add `gem "minitest-rails-capybara"`
# to the test group in the Gemfile and uncomment the following:
# require "minitest/rails/capybara"
# Uncomment for awesome colorful output
require "minitest/pride"
class ActiveSupport::TestCase
# Setup all fixtures in test/fixtures/*.(yml|csv) for all tests in alphabetical order.
# fixtures :all
# Add more helper methods to be used by all tests here...
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment