Skip to content

Commit

Permalink
Merge pull request #193 from juanger/topic/reporters
Browse files Browse the repository at this point in the history
Custom Jasmine Reporters
  • Loading branch information
nathanstitt committed Aug 18, 2016
2 parents 09b295d + 54e15d2 commit 62a931b
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 38 deletions.
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,38 @@ You can further customize the log output by implement one of these methods:
In addition, the console can log jQuery collections and outputs the HTML representation of the element by using the
jQuery `html()` method.

### Custom Report options

Guard::Jasmine supports [custom jasmine reporters](http://jasmine.github.io/2.1/custom_reporter.html). In order to use them you have to configure jasmine and make sure that you add the javascript files to the available assets, in this case I use the ones from [larrymyers/jasmine-reporters](https://github.com/larrymyers/jasmine-reporters):

```yaml
reporters:
junit:
- "junit_reporter.js"
- "junit_reporter.boot.js"
```

junit_reporter.boot.js is the file where you'll have to intialize and configure the reporter:

```javascript
(function() {
var reporter = new jasmineReporters.JUnitXmlReporter({
// Here you can set any options that the custom reporter accepts
savePath: 'reports/junit'
});
jasmine.getEnv().addReporter(reporter);
})();
```

Once you got everything in the right place, run guard-jasmine with --reporters

#### Making your own reporter write to the filesystem

If none of the existing reporters work for you, you are writing your own one and you want it to write it's output to the filesystem, you have two options:

1. use window.__phantom_writeFile(filename, text) (the way [larrymyers/jasmine-reporters](https://github.com/larrymyers/jasmine-reporters) works)
2. use window.callPhantom({event: 'writeFile', filename: filename, text: text}) (the way [shepmaster/jasmine-junitreporter](https://github.com/shepmaster/jasmine-junitreporter) works)

### Coverage options

Guard::Jasmine supports coverage reports generated by [Istanbul](https://github.com/gotwarlost/istanbul). You need to
Expand Down
4 changes: 1 addition & 3 deletions lib/guard/jasmine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,7 @@ class Jasmine < Plugin
functions_threshold: 0,
branches_threshold: 0,
lines_threshold: 0,
junit: false,
junit_consolidate: true,
junit_save_path: '',
reporters: nil,
debug: false
}

Expand Down
20 changes: 4 additions & 16 deletions lib/guard/jasmine/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -150,20 +150,10 @@ class CLI < Thor
default: 0,
desc: 'Lines coverage threshold'

method_option :junit,
type: :boolean,
default: false,
desc: 'Whether to save jasmine test results in JUnit-compatible xml files'

method_option :junit_consolidate,
type: :boolean,
default: false,
desc: 'Whether to save nested describes within the same xml file as their parent'

method_option :junit_save_path,
method_option :reporters,
type: :string,
default: '',
desc: 'The directory to save junit xml files into'
default: nil,
desc: 'Comma separated list of jasmine reporters to use'

# Run the Guard::Jasmine::Runner with options from
# the command line.
Expand Down Expand Up @@ -200,9 +190,7 @@ def spec(*paths)
runner_options[:notification] = false
runner_options[:hide_success] = true
runner_options[:max_error_notify] = 0
runner_options[:junit] = options.junit
runner_options[:junit_consolidate] = options.junit_consolidate
runner_options[:junit_save_path] = options.junit_save_path
runner_options[:query_params] = options.reporters ? {reporters: options.reporters} : nil
runner_options[:is_cli] = true

paths = [runner_options[:spec_dir]] if paths.empty?
Expand Down
26 changes: 23 additions & 3 deletions lib/guard/jasmine/phantomjs/guard-jasmine.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
(function() {
var exitError, exitSuccessfully, hasLoggedError, jasmineAvailable, options, page, phantomExit, reportError, reporterMissing, reporterReady, specsDone, specsTimedout, system, waitFor;
var exitError, exitSuccessfully, fs, hasLoggedError, jasmineAvailable, options, page, phantomExit, reportError, reporterMissing, reporterReady, specsDone, specsTimedout, system, waitFor;

system = require('system');

Expand All @@ -10,6 +10,8 @@

page = require('webpage').create();

fs = require('fs');

page.onError = function(message, trace) {
return reportError("Javascript error encountered on Jasmine test page: " + message, trace);
};
Expand All @@ -20,15 +22,25 @@
};

page.onInitialized = function() {
var injectReporter;
page.injectJs('guard-reporter.js');
return page.evaluate(function() {
injectReporter = function(pathSeparator) {
return window.onload = function() {
window.reporter = new GuardReporter();
window.fs_path_separator = "" + pathSeparator;
window.__phantom_writeFile = function(filename, text) {
return window.callPhantom({
event: 'writeFile',
filename: filename,
text: text
});
};
if (window.jasmine) {
return window.jasmine.getEnv().addReporter(window.reporter);
}
};
});
};
return page.evaluate(injectReporter, fs.separator);
};

page.onLoadFinished = function(status) {
Expand All @@ -39,6 +51,14 @@
}
};

page.onCallback = function(data) {
if (data.event === 'writeFile') {
return fs.write(data.filename, data.text, 'w');
} else {
return console.log('unknown event callback: ' + data.event);
}
};

page.open(options.url);

reporterReady = function() {
Expand Down
17 changes: 15 additions & 2 deletions lib/guard/jasmine/phantomjs/src/guard-jasmine.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ options =
# Create the web page.
page = require('webpage').create()

# Define fs to write files for custom reporters
fs = require('fs')

# Catch JavaScript errors
# abort the request and return the error
page.onError = (message, trace) ->
Expand All @@ -22,18 +25,28 @@ page.onResourceError = (error)->
# the GuardReporter class
page.onInitialized = ->
page.injectJs 'guard-reporter.js'
page.evaluate ->
injectReporter = (pathSeparator) ->
window.onload = ->
window.reporter = new GuardReporter()
window.fs_path_separator = "#{pathSeparator}"
window.__phantom_writeFile = (filename, text) ->
window.callPhantom({event: 'writeFile', filename: filename, text: text})
window.jasmine.getEnv().addReporter(window.reporter) if window.jasmine
page.evaluate injectReporter, fs.separator

# Once the page is finished loading
page.onLoadFinished = (status)->
page.onLoadFinished = (status) ->
if status isnt 'success'
reportError "Unable to access Jasmine specs at #{page.reason_url}. #{page.reason}"
else
waitFor reporterReady, jasmineAvailable, options.timeout, reporterMissing

page.onCallback = (data) ->
if data.event is 'writeFile'
fs.write(data.filename, data.text, 'w')
else
console.log('unknown event callback: ' + data.event)

# Open web page, which will kick off the Jasmine test runner
page.open options.url

Expand Down
18 changes: 4 additions & 14 deletions spec/guard/jasmine/cli_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -155,20 +155,10 @@
end
end

context 'for the junit options' do
it 'sets the junit options to false' do
expect(runner).to receive(:new).with(hash_including(junit: true))
cli.start(['spec', '--junit'])
end

it 'sets the junit consolidate option' do
expect(runner).to receive(:new).with(hash_including(junit_consolidate: true))
cli.start(['spec', '--junit-consolidate'])
end

it 'sets the junit save path' do
expect(runner).to receive(:new).with(hash_including(junit_save_path: '/home/user'))
cli.start(['spec', '--junit-save-path', '/home/user'])
context 'for the reports option' do
it 'sets the correct query parameters option' do
expect(runner).to receive(:new).with(hash_including(query_params: {reporters: 'console'}))
cli.start(['spec', '--reporters', 'console'])
end
end
end
Expand Down

0 comments on commit 62a931b

Please sign in to comment.