All Downloads are FREE. Search and download functionalities are using the official Maven repository.

lib.createJunitXml.js Maven / Gradle / Ivy

Go to download

A JavaScript unit test plugin that processes JavaScript sources and Jasmine specs, prepares test runner HTML files, executes Jasmine specs headlessly with HtmlUnit, and produces JUnit XML reports

There is a newer version: 1.1.2
Show newest version
var junitXmlReporter;

(function() {
	junitXmlReporter = {
		prolog: '',
		report: function(reporter,debug) {
			if (!reporter)
				throw 'Jasmine JS API Reporter must not be null.';
			if (reporter.finished !== true && !debug)
				throw 'Jasmine runner is not finished!';

			var results = this.crunchResults(reporter.results());
			
			var writer = new XmlWriter();
			writer.beginNode('testsuite');
			writer.attrib('errors','0');
			writer.attrib('name','jasmine.specs');
			writer.attrib('tests',results.tests);
			writer.attrib('failures',results.failures);
			writer.attrib('skipped',results.skipped);
			writer.attrib('hostname','localhost');
			writer.attrib('time', '0.0');
			writer.attrib('timestamp',this.currentTimestamp());
			this.writeChildren(reporter, writer, reporter.suites(),'');
			writer.endNode();
			
			return this.prolog+writer.toString();
		},
		writeChildren: function(reporter, writer, tests,runningName) {
			for(var i=0;i 0 ? runningName+' ' : '')+tests[i].name;
				if(tests[i].type === 'spec') {
					var specResult = reporter.results()[tests[i].id] || {};
					this.writeTestcase(writer,specResult,name);
				}
				this.writeChildren(reporter, writer,tests[i].children,name);
			}
		},
		writeTestcase: function(writer,specResult,name) {
			var failure = specResult.result !== 'passed';
			writer.beginNode('testcase');
			writer.attrib('classname','jasmine');
			writer.attrib('name',name);
			writer.attrib('time','0.0');
			writer.attrib('failure',failure+'');
			if(failure) {
				this.writeError(writer,specResult);
			}
			writer.endNode();
		},
		writeError: function(writer,specResult) {
			writer.beginNode('error');
			var message = '';
			var type = '';
			var messages = specResult.messages || [];
			for(var j=0;j 0 ? (1+parseInt(last)-count).toString() : "0"
			};
		},
		currentTimestamp: function() {
			var f = function(n) {
		        // Format integers to have at least two digits.
		        return n < 10 ? '0' + n : n;
		    }

			var date = new Date();
			
	        return date.getUTCFullYear()   + '-' +
	             f(date.getUTCMonth() + 1) + '-' +
	             f(date.getUTCDate())      + 'T' +
	             f(date.getUTCHours())     + ':' +
	             f(date.getUTCMinutes())   + ':' +
	             f(date.getUTCSeconds());
		}
	};

	//From here: http://www.codeproject.com/KB/ajax/XMLWriter.aspx
	function XmlWriter() {
		this.XML = [];
		this.nodes = [];
		this.State = "";
		this.formatXml = function(Str) {
			if (Str)
				return Str.replace(/&/g, "&").replace(/\"/g, """)
						.replace(//g, ">");
			return ""
		}
		this.beginNode = function(Name) {
			if (!Name)
				return;
			if (this.State == "beg")
				this.XML.push(">");
			this.State = "beg";
			this.nodes.push(Name);
			this.XML.push("<" + Name);
		}
		this.endNode = function() {
			if (this.State == "beg") {
				this.XML.push("/>");
				this.nodes.pop();
			} else if (this.nodes.length > 0)
				this.XML.push("");
			this.State = "";
		}
		this.attrib = function(Name, Value) {
			if (this.State != "beg" || !Name)
				return;
			this.XML.push(" " + Name + "=\"" + this.formatXml(Value) + "\"");
		}
		this.writeString = function(Value) {
			if (this.State == "beg")
				this.XML.push(">");
			this.XML.push(this.formatXml(Value));
			this.State = "";
		}
		this.node = function(Name, Value) {
			if (!Name)
				return;
			if (this.State == "beg")
				this.XML.push(">");
			this.XML.push((Value == "" || !Value) ? "<" + Name + "/>" : "<"
					+ Name + ">" + this.formatXml(Value) + "");
			this.State = "";
		}
		this.close = function() {
			while (this.nodes.length > 0)
				this.endNode();
			this.State = "closed";
		}
		this.toString = function() {
			return this.XML.join("");
		}
	}

})();




© 2015 - 2024 Weber Informatics LLC | Privacy Policy