Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reporters/json*: Add results to clean() #47

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions lib/reporters/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -461,3 +461,40 @@ function sameType(a, b) {
b = Object.prototype.toString.call(b);
return a == b;
}

/**
* Extract own properties from an Object.
* @param {Object} obj
* @return {Object}
*/

function ownProperties(obj) {
var res = {};
Object.getOwnPropertyNames(obj).forEach(function(key) {
res[key] = obj[key];
}, obj);
return res;
}

/**
* Return a plain-object representation of `test`
* free of cyclic properties etc.
*
* @param {Object} test
* @return {Object}
* @api private
*/

var clean = exports.clean = function(test) {
var data = {
title: test.title,
fullTitle: test.fullTitle(),
duration: test.duration,
err: ownProperties(test.err || {}),
result: ownProperties(test.result || {})
};
if (data.result.name !== undefined) {
delete data.result.name; // already covered by data.title
}
return data;
}
19 changes: 1 addition & 18 deletions lib/reporters/json-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/

var Base = require('./base')
, clean = Base.clean
, color = Base.color;

/**
Expand Down Expand Up @@ -35,28 +36,10 @@ function List(runner) {

runner.on('fail', function(test, err){
test = clean(test);
test.err = err.message;
console.log(JSON.stringify(['fail', test]));
});

runner.on('end', function(){
process.stdout.write(JSON.stringify(['end', self.stats]));
});
}

/**
* Return a plain-object representation of `test`
* free of cyclic properties etc.
*
* @param {Object} test
* @return {Object}
* @api private
*/

function clean(test) {
return {
title: test.title
, fullTitle: test.fullTitle()
, duration: test.duration
}
}
33 changes: 1 addition & 32 deletions lib/reporters/json.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/

var Base = require('./base')
, clean = Base.clean
, cursor = Base.cursor
, color = Base.color;

Expand Down Expand Up @@ -58,35 +59,3 @@ function JSONReporter(runner) {
process.stdout.write(JSON.stringify(obj, null, 2));
});
}

/**
* Return a plain-object representation of `test`
* free of cyclic properties etc.
*
* @param {Object} test
* @return {Object}
* @api private
*/

function clean(test) {
return {
title: test.title,
fullTitle: test.fullTitle(),
duration: test.duration,
err: errorJSON(test.err || {})
}
}

/**
* Transform `error` into a JSON object.
* @param {Error} err
* @return {Object}
*/

function errorJSON(err) {
var res = {};
Object.getOwnPropertyNames(err).forEach(function(key) {
res[key] = err[key];
}, err);
return res;
}