Add json-file reporter #259

Closed
wants to merge 1 commit into
from
View
@@ -106,6 +106,9 @@ while (arg = argv.shift()) {
options[arg] = true;
} else {
switch (arg) {
+ case 'jsonfile':
+ _reporter = require('../lib/vows/reporters/json-file');
+ break;
case 'json':
_reporter = require('../lib/vows/reporters/json');
break;
@@ -0,0 +1,47 @@
+var options = { tail: '\n', raw: true };
+var console = require('../../vows/console');
+var puts = console.puts(options);
+var util = require('util'),
+ fs = require('fs')
+// Console JSON reporter
+//
+this.name = 'json-file';
+this.setStream = function (s) {
+ options.stream = s;
+};
+var bufferReport;
+
+function removeCircularSuite(obj, suite) {
+ var result = {};
+
+ if (typeof obj !== 'object' || obj === null) return obj;
+
+ Object.keys(obj).forEach(function(key) {
+ if (obj[key] === suite) {
+ result[key] = {};
+ } else {
+ result[key] = removeCircularSuite(obj[key], suite || obj.suite);
+ }
+ });
+
+ return result;
+};
+
+this.report = function (obj) {
+ puts(JSON.stringify(removeCircularSuite(obj)));
+ bufferReport += JSON.stringify(removeCircularSuite(obj))
+ if(obj[0]==="finish"){
+ puts("Generating report.json file ...")
+ try {
+ var out = fs.openSync("report.json", "w");
+ fs.writeSync(out, bufferReport);
+ fs.close(out);
+ puts("Generating report.json file complete")
+ } catch (error) {
+ util.print("Error: Unable to write to file report.json\n");
+ return;
+ }
+ }
+};
+
+this.print = function (str) {};