From f19d07a209dde05bd38b41d5089405f8c24a571c Mon Sep 17 00:00:00 2001 From: Kelvin Luck Date: Tue, 8 Jan 2013 13:09:05 +0000 Subject: [PATCH] Writing the suites out to separate xml files with the option to consolidate into one file per top level suite... --- tasks/jasmine.js | 41 +++++++-- tasks/jasmine/reporters/JUnitDataReporter.js | 89 ++++++++++-------- tasks/jasmine/reporters/PhantomReporter.js | 97 ++++++++++++-------- 3 files changed, 143 insertions(+), 84 deletions(-) diff --git a/tasks/jasmine.js b/tasks/jasmine.js index 5991738..e8f8764 100644 --- a/tasks/jasmine.js +++ b/tasks/jasmine.js @@ -208,16 +208,41 @@ module.exports = function(grunt) { phantomjs.on('jasmine.reportJUnitResults',function(junitData){ if (options.junit && options.junit.path) { - grunt.file.copy(runners.junit, path.join(options.junit.path,'out-TEST.xml'), { - process : function(src) { - return grunt.util._.template( - src, + + if (options.junit.consolidate) { + + grunt.util._(junitData.consolidatedSuites).each( + function(suites) { - testsuites: junitData + grunt.file.copy(runners.junit, path.join(options.junit.path, suites[0].name.replace(/[^\w]/g, '') + '-TEST.xml'), { + process: function(src) { + return grunt.util._.template( + src, + { + testsuites: suites + } + ); + } + }) } - ); - } - }) + ); + } else { + junitData.suites.forEach( + function(suiteData) + { + grunt.file.copy(runners.junit, path.join(options.junit.path, suiteData.name.replace(/[^\w]/g, '') + '-TEST.xml'), { + process: function(src) { + return grunt.util._.template( + src, + { + testsuites: [suiteData] + } + ); + } + }) + } + ); + } } }); diff --git a/tasks/jasmine/reporters/JUnitDataReporter.js b/tasks/jasmine/reporters/JUnitDataReporter.js index d65b3dd..8ca899a 100644 --- a/tasks/jasmine/reporters/JUnitDataReporter.js +++ b/tasks/jasmine/reporters/JUnitDataReporter.js @@ -13,6 +13,16 @@ return names.join(' '); } + function getTopLevelSuiteId(suite) + { + var id; + while (suite) { + id = suite.id; + suite = suite.parentSuite; + } + return id; + } + jasmine.JUnitDataReporter = function() { @@ -22,45 +32,48 @@ reportRunnerStarting: function(runner) { }, reportRunnerResults: function(runner) { - var suites = runner.suites().map( - function(suite) - { - var failures = 0, - data = { - name: getNestedSuiteName(suite), - time: suite.duration / 1000, - timestamp: suite.timestamp, - tests: suite.specs().length, - errors: 0, // TODO: These exist in the JUnit XML but not sure how they map to jasmine things - testcases: suite.specs().map( - function(spec) - { - var failureMessages = []; - if (spec.results().failedCount) { - failures ++; - spec.results().items_.forEach( - function(expectation) - { - if (!expectation.passed()) { - failureMessages.push(expectation.message); - } + var suitesById = {}, + suites = runner.suites().map( + function(suite) + { + var failures = 0, + data = { + topLevelSuiteId: getTopLevelSuiteId(suite), + name: getNestedSuiteName(suite), + time: suite.duration / 1000, + timestamp: suite.timestamp, + tests: suite.specs().length, + errors: 0, // TODO: These exist in the JUnit XML but not sure how they map to jasmine things + testcases: suite.specs().map( + function(spec) + { + var failureMessages = []; + if (spec.results().failedCount) { + failures ++; + spec.results().items_.forEach( + function(expectation) + { + if (!expectation.passed()) { + failureMessages.push(expectation.message); + } + } + ); } - ); - } - return { - assertions: spec.results().items_.length, - className: getNestedSuiteName(spec.suite), - name: spec.description, - time: spec.duration / 1000, - failureMessages: failureMessages - }; - } - ) - }; - data.failures = failures; - return data; - } - ); + return { + assertions: spec.results().items_.length, + className: getNestedSuiteName(spec.suite), + name: spec.description, + time: spec.duration / 1000, + failureMessages: failureMessages + }; + } + ) + }; + data.failures = failures; + suitesById[suite.id] = data; + return data; + } + ); console.log('Suites:', suites); }, reportSuiteResults: function(suite) { diff --git a/tasks/jasmine/reporters/PhantomReporter.js b/tasks/jasmine/reporters/PhantomReporter.js index 266ff9e..d17a6f6 100644 --- a/tasks/jasmine/reporters/PhantomReporter.js +++ b/tasks/jasmine/reporters/PhantomReporter.js @@ -204,46 +204,67 @@ return names.join(' '); } + function getTopLevelSuiteId(suite) + { + var id; + while (suite) { + id = suite.id; + suite = suite.parentSuite; + } + return id; + } + PhantomReporter.prototype.generateJUnitSummary_ = function(runner) { - return runner.suites().map( - function(suite) - { - var failures = 0, - data = { - name: getNestedSuiteName(suite), - time: suite.duration / 1000, - timestamp: suite.timestamp, - tests: suite.specs().length, - errors: 0, // TODO: These exist in the JUnit XML but not sure how they map to jasmine things - testcases: suite.specs().map( - function(spec) - { - var failureMessages = []; - if (spec.results().failedCount) { - failures ++; - spec.results().items_.forEach( - function(expectation) - { - if (!expectation.passed()) { - failureMessages.push(expectation.message); - } + var consolidatedSuites = {}, + suites = runner.suites().map( + function(suite) + { + var failures = 0, + data = { + name: getNestedSuiteName(suite), + time: suite.duration / 1000, + timestamp: suite.timestamp, + tests: suite.specs().length, + errors: 0, // TODO: These exist in the JUnit XML but not sure how they map to jasmine things + testcases: suite.specs().map( + function(spec) + { + var failureMessages = []; + if (spec.results().failedCount) { + failures ++; + spec.results().items_.forEach( + function(expectation) + { + if (!expectation.passed()) { + failureMessages.push(expectation.message); + } + } + ); } - ); - } - return { - assertions: spec.results().items_.length, - className: getNestedSuiteName(spec.suite), - name: spec.description, - time: spec.duration / 1000, - failureMessages: failureMessages - }; - } - ) - }; - data.failures = failures; - return data; - } - ); + return { + assertions: spec.results().items_.length, + className: getNestedSuiteName(spec.suite), + name: spec.description, + time: spec.duration / 1000, + failureMessages: failureMessages + }; + } + ) + }; + data.failures = failures; + if (suite.parentSuite) { + consolidatedSuites[getTopLevelSuiteId(suite)].push(data); + } else { + consolidatedSuites[suite.id] = [data]; + } + return data; + } + ); + + return { + suites: suites, + consolidatedSuites: consolidatedSuites + }; }; jasmine.getEnv().addReporter( new PhantomReporter() );