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: 3.0-beta-02
Show newest version
var junitXmlReporter;

(function() {
  var resultForSpec = function(reporter, spec){
    var specResults = reporter.specs();
    for (var i=0; i < specResults.length; i++) {
      if (spec.id == specResults[i].id) {
        return specResults[i];
      }
    }
    return {};
  };

  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.specs());

      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, jasmine.getEnv().topSuite().children,'');
      writer.endNode();

      return this.prolog+writer.toString();
    },
    writeChildren: function(reporter, writer, tests,runningName) {
      if (tests) {
        for(var i=0;i 0 ? runningName+' ' : '')+tests[i].description;
          if(tests[i] instanceof jasmine.Spec) {
            var specResult = resultForSpec(reporter, tests[i]);
            this.writeTestcase(writer,specResult,name);
          }
          this.writeChildren(reporter, writer,tests[i].children,name);
        }
      }
    },
    writeTestcase: function(writer,specResult,name) {
      var failure = specResult.status !== '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.failedExpectations || [];
      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