Skip to content
This repository has been archived by the owner on Sep 2, 2022. It is now read-only.

Commit

Permalink
jasmine 2.0 support and junit formatter for legacy version , also upd…
Browse files Browse the repository at this point in the history
…ated reporters to add more info and also the console output to ressemble more with Rspec
  • Loading branch information
bogdanRada committed Aug 12, 2016
1 parent d0036f9 commit c0f1081
Show file tree
Hide file tree
Showing 17 changed files with 758 additions and 156 deletions.
6 changes: 6 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,9 @@ source "http://rubygems.org"

# Specify your gem's dependencies in jasminerice-runner.gemspec
gemspec

if RUBY_VERSION >= '2.2.0'
gem 'rack', '>= 1.0', '>= 1.0'
else
gem 'rack', '>= 1.0', '< 2.0'
end
88 changes: 79 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,98 @@
jasminerice-runner
==================

**The original repo hasn't been updated in 4 years , this fork though tries to keep up-to-date with latest updates.**

Uses capybara to run jasmine specs with jasminerice and rails asset pipeline

Usage
-----
Requirements
------------

1. [capybara >= 2.0](https://github.com/celluloid/celluloid)
2. [jasminerice >= 0.0.10](https://github.com/jwo/celluloid-pmap)
3. [jasmine >= 1.1](https://github.com/bogdanRada/celluloid_pubsub)
4. [nokogiri >= 1.6](https://github.com/swoop-inc/composable_state_machine)
5. [colorize >= 0.8](https://github.com/tj/terminal-table)
6. [coffee-script >= 2.0](https://github.com/fazibear/colorize)

Compatibility
-------------

Rails 4.0 or greater ( currently this was tested only with Rails), but i can accept pull requests for other rack-based applications
[MRI >= 2.0](http://www.ruby-lang.org)

Rubinius, Jruby, MRI 1.8, MRI 1.9 are not officially supported.

Installation Instructions
-------------------------

Add it to your Gemfile

gem "jasminerice-runner"

Add this to your spec.js:

Usage
-----

For Jasmine <2.0 add this to your spec.js:

```coffee
#= require jasminerice_runner_base
#= require jasminerice_legacy_reporter
```

For Jasmine > 2.0 add this to your spec.js:

```coffee
#= require jasminerice_runner_base
#= require jasminerice_reporter

```


Then, run the rake task

```sh
rake jasminerice:run
```
To switch drivers, in a config/initializer

Jasminerice::Runner.capybara_driver = :webkit

Default driver is :selenium
```ruby
Jasminerice::Runner.configure do |config|
# you can use any other driver (phantomjs, or other drivers)
config.capybara_driver = :webkit

# For Jasmine < 2.0, this gem also provides a Junit XML formatter ( for Jasmine > 2.0 this will have no effect )
# If you want to enable the JUNIT XML Formatter you need to specify `:junit_xml`
# By Default this is set to nil, however if the environment variable CI is present and has as value 'true',
# and you are using Jasmine < 2.0, this will be automatically set to :junit_xml
# unless of course this is not overidden in a initializer
config.formatters = nil

# For Jasmine < 2.0, this gem also provides a Junit XML formatter ( for Jasmine > 2.0 this will have no effect )
config.junit_xml_path = File.join(Dir.pwd, 'spec', 'reports')
end
```


**Default driver is :selenium***


Using other formatters for Jasmine > 2.0
-----------------------------------

Starting with version 2.0 , Jasmine supports custom formatters by using this configuration:


```ruby

Jasmine.configure do |config|
config.formatters << Jasmine::Formatters::JunitXml
end
```

Put this configuration in a file located in **config/initializers**.

But in order for this to work , you will have to add the gem **jasmine_junitxml_formatter** to your Gemfile


Using Multiple Jasmine Environments
-----------------------------------
Expand Down
53 changes: 52 additions & 1 deletion Rakefile
Original file line number Diff line number Diff line change
@@ -1 +1,52 @@
require "bundler/gem_tasks"
require 'bundler/setup'
require 'bundler/gem_tasks'
require 'appraisal'
require 'rspec/core/rake_task'
require 'coveralls/rake/task'
require 'yard'
Coveralls::RakeTask.new

RSpec::Core::RakeTask.new(:spec) do |spec|
default_options = ['--colour']
default_options.concat(['--backtrace', '--fail-fast']) if ENV['DEBUG']
spec.rspec_opts = default_options
spec.verbose = true
end

YARD::Config.options[:load_plugins] = true
YARD::Config.load_plugins

# dirty hack for YardocTask
::Rake.application.class.class_eval do
alias_method :last_comment, :last_description
end

YARD::Rake::YardocTask.new do |t|
t.files = ['lib/**/*.rb', 'spec/**/*_spec.rb'] # optional
t.options = ['--any', '--extra', '--opts', '--markup-provider=redcarpet', '--markup=markdown', '--debug'] # optional
t.stats_options = ['--list-undoc'] # optional
end

desc 'Default: run the unit tests.'
task default: [:all]

desc 'Test the plugin under all supported Rails versions.'
task :all do |_t|
if ENV['TRAVIS']
# require 'json'
# puts JSON.pretty_generate(ENV.to_hash)
if ENV['BUNDLE_GEMFILE'] =~ /gemfiles/
appraisal_name = ENV['BUNDLE_GEMFILE'].scan(/rails\_(.*)\.gemfile/).flatten.first
command_prefix = "appraisal rails-#{appraisal_name}"
exec("#{command_prefix} bundle install && #{command_prefix} bundle exec rspec && bundle exec rake coveralls:push ")
else
exec(' bundle exec appraisal install && bundle exec rake appraisal spec && bundle exec rake coveralls:push')
end
else
exec('bundle exec appraisal install && bundle exec rake appraisal spec')
end
end

task :docs do
exec('bundle exec inch --pedantic && bundle exec yard --list-undoc')
end
62 changes: 62 additions & 0 deletions app/assets/javascripts/jasminerice_legacy_reporter.js.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#= require ./jasminerice_runner_base
class JasminericeLegacyReporter extends window.JasminericeRunnerBase
constructor : ->
super

reportRunnerStarting: (runner) ->
super(runner)

reportRunnerResults: (runner) ->
@finished = true

reportSpecResults: (spec) ->
spec_result = if spec.results().failedCount > 0 then "failed" else "passed"
spec_id = spec.id
@totalCount++
@results_[spec_id] = {
'status': spec_result
'messages': spec.results().getItems(),
'id': spec_id,
'description': spec.description,
'suite': spec.suite,
'full_name': spec.getFullName()
}
@final_results_[spec_id] = @.summarizeResult_(spec, @results_[spec_id])
if spec_result == 'failed'
@failedCount++
@failedSpecs.push(@final_results_[spec_id])

resultsForSpecs : (specIds) ->
results = {}
for i in [0...specIds.length]
specId = specIds[i]
results[specId] = @.summarizeResult_(specIds[i], @results_[specId])
results

summarizeResult_ : (spec, result) ->
summaryMessages = []
for messageIndex in [0...result.messages.length]
resultMessage = result.messages[messageIndex]
summaryMessages.push({
'status': if resultMessage.passed then resultMessage.passed() else true,
'message': resultMessage.message,
'matcherName': resultMessage.matcherName,
'expected': ''+resultMessage.expected,
'actual': ''+resultMessage.actual,
'trace': {
'stack': if resultMessage.passed && !resultMessage.passed() then resultMessage.trace.stack else jasmine.undefined
}
})
{
'id': result.id,
'status': result.status
'name': result.full_name,
'description': result.description
'messages': summaryMessages,
'suite_name': @.getFullDescOfSuite(result.suite),
}

# make sure this exists so we don't have timing issue
# when capybara hits us before the onload function has run
window.jasmineRiceReporter = new JasminericeLegacyReporter()
window.jasmineRiceReporter.register()
69 changes: 69 additions & 0 deletions app/assets/javascripts/jasminerice_reporter.js.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#= require ./jasminerice_runner_base
class JasminericeReporter extends window.JasminericeRunnerBase
constructor : ->
super
@runner_results = []

jasmineStarted: (suiteInfo) ->
@started = true
@failedSpecs = []
@totalCount = 0
@failedCount = 0
#@.log('Running suite with ' + suiteInfo.totalSpecsDefined)

suiteStarted: (result) ->
@suites_[suite.id] = @.summarize_(result)
#@.log('Suite started: ' + result.description + ' whose full description is: ' + result.fullName)

specStarted: (result) ->
#@.log('Spec started: ' + result.description + ' whose full description is: ' + result.fullName)

specDone: (spec) ->
spec_result = if spec.failedExpectations.length > 0 then "failed" else "passed"
spec_id = spec.id
@totalCount++
@results_[spec_id] = {
'status': spec_result
'messages': spec.failedExpectations,
'id': spec_id,
'description': spec.description,
'full_name': spec.fullName
}
@final_results_[spec_id] = @.summarizeResult_(spec, @results_[spec_id])
if spec_result == 'failed'
@failedCount++
@failedSpecs.push(@final_results_[spec_id])

summarizeResult_ : (spec, result) ->
summaryMessages = []
for messageIndex in [0...result.messages.length]
resultMessage = result.messages[messageIndex]
summaryMessages.push({
'status': result.status,
'message': resultMessage.message,
'matcherName': resultMessage.matcherName,
'expected': ''+resultMessage.expected,
'actual': ''+resultMessage.actual,
'trace': {
'stack': if !resultMessage.passed then resultMessage.stack else jasmine.undefined
}
})
{
'id': result.id,
'status': result.status
'name': result.full_name,
'description': result.description
'messages': summaryMessages,
}

suiteDone: (result) ->
#The result here is the same object as in suiteStarted but with the addition of a status and a list of failedExpectations.
@.log('Suite: ' + result.description + ' was ' + result.status);

jasmineDone: ->
@finished = true

# make sure this exists so we don't have timing issue
# when capybara hits us before the onload function has run
window.jasmineRiceReporter = new JasminericeReporter()
window.jasmineRiceReporter.register()
90 changes: 90 additions & 0 deletions app/assets/javascripts/jasminerice_runner_base.js.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
class JasminericeRunnerBase
constructor: ->
@started = false
@finished = false
@failedSpecs = []
@totalCount = 0
@failedCount = 0
@failure_message = 'Failure/Error: '
# additional data ( optional )
@suites_ = {}
@results_ = {}
@final_results_ = {}

results: ->
@results_

all_summarized_results: ->
summarized_results = []
for key, value of @final_results_
summarized_results.push(value)
summarized_results

suites: ->
@suites_

extend_object: (object, properties) ->
for key, val of properties
object[key] = val
object

resultsForSpec: (specId) ->
@results_[specId]

getFullDescOfSuite: (suite) ->
desc = ""
while(suite.parentSuite)
desc = suite.description + " " + desc
suite = suite.parentSuite
desc = suite.description + " " + desc
desc

reportRunnerStarting: (runner) ->
@started = true
suites = runner.topLevelSuites()
for i in [0...suites.length]
suite = suites[i]
@suites_[suite.id] = @.summarize_(suite)

summarize_ : (suiteOrSpec) ->
isSuite = suiteOrSpec instanceof jasmine.Suite
summary = {
id: suiteOrSpec.id,
name: suiteOrSpec.description,
type: if isSuite then 'suite' else 'spec',
children: []
}
if (isSuite && suiteOrSpec.hasOwnProperty('children'))
children = suiteOrSpec.children()
for i in [0...children.length]
summary.children.push(@.summarize_(children[i]))
summary

inspect: ->
{
'started': @started,
'finished': @finished,
'totalCount': @totalCount,
'failedCount': @failedCount
'failedSpecs': @failedSpecs
}

log: (str) ->
console.log(str)

bindEvent: (eventHandler) ->
eventName = 'DOMContentLoaded'
if (document.addEventListener)
document.addEventListener(eventName, eventHandler, false)
else if (document.attachEvent)
document.attachEvent('on'+eventName, eventHandler)

register: ->
@.bindEvent( =>
@.registerIntoJasmine()
)

registerIntoJasmine: ->
jasmine.getEnv().addReporter(window.jasmineRiceReporter)

window.JasminericeRunnerBase = JasminericeRunnerBase
Loading

0 comments on commit c0f1081

Please sign in to comment.