diff --git a/README.md b/README.md index 32ed329..cff651d 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/lib/guard/jasmine.rb b/lib/guard/jasmine.rb index 6f463e4..45264a7 100644 --- a/lib/guard/jasmine.rb +++ b/lib/guard/jasmine.rb @@ -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 } diff --git a/lib/guard/jasmine/cli.rb b/lib/guard/jasmine/cli.rb index bbdcabe..780f7f4 100644 --- a/lib/guard/jasmine/cli.rb +++ b/lib/guard/jasmine/cli.rb @@ -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. @@ -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? diff --git a/lib/guard/jasmine/phantomjs/guard-jasmine.js b/lib/guard/jasmine/phantomjs/guard-jasmine.js index 6cecfea..1deffdf 100644 --- a/lib/guard/jasmine/phantomjs/guard-jasmine.js +++ b/lib/guard/jasmine/phantomjs/guard-jasmine.js @@ -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'); @@ -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); }; @@ -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) { @@ -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() { diff --git a/lib/guard/jasmine/phantomjs/src/guard-jasmine.coffee b/lib/guard/jasmine/phantomjs/src/guard-jasmine.coffee index 87b50b8..80dde58 100644 --- a/lib/guard/jasmine/phantomjs/src/guard-jasmine.coffee +++ b/lib/guard/jasmine/phantomjs/src/guard-jasmine.coffee @@ -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) -> @@ -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 diff --git a/spec/guard/jasmine/cli_spec.rb b/spec/guard/jasmine/cli_spec.rb index 2b4f78b..c434b67 100644 --- a/spec/guard/jasmine/cli_spec.rb +++ b/spec/guard/jasmine/cli_spec.rb @@ -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