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

Add JSON Reporter output to object similar to #2651 #4822

Closed
Closed
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
10 changes: 9 additions & 1 deletion lib/reporters/json.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ exports = module.exports = JSONReporter;
* @memberof Mocha.reporters
* @extends Mocha.reporters.Base
* @param {Runner} runner - Instance triggers reporter actions.
* @param {Object} [options] - runner options
* @param {Object} [options] - runner options - the property 'output' defines a target file to write to
* the property 'targetObject' an object into which the results of the tests will be written
*/
function JSONReporter(runner, options = {}) {
Base.call(this, runner, options);
Expand All @@ -43,6 +44,7 @@ function JSONReporter(runner, options = {}) {
var failures = [];
var passes = [];
var output;
var targetObject;

if (options.reporterOption && options.reporterOption.output) {
if (utils.isBrowser()) {
Expand All @@ -51,6 +53,10 @@ function JSONReporter(runner, options = {}) {
output = options.reporterOption.output;
}

if (options.reporterOption && 'targetObject' in options.reporterOption) {
targetObject = options.reporterOption.targetObject;
}

runner.on(EVENT_TEST_END, function (test) {
tests.push(test);
});
Expand Down Expand Up @@ -89,6 +95,8 @@ function JSONReporter(runner, options = {}) {
);
process.stdout.write(json);
}
} else if (targetObject !== undefined) {
Object.assign(targetObject, obj);
} else {
process.stdout.write(json);
}
Expand Down
29 changes: 29 additions & 0 deletions test/reporters/json.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,4 +230,33 @@ describe('JSON reporter', function () {
);
});
});

describe('when "reporterOption.targetObject" is provided', function () {
var theObject = {};

var options = {
reporterOption: {
targetObject: theObject
}
};

beforeEach(function () {
/* eslint no-unused-vars: off */
var mochaReporter = new mocha._reporter(runner, options);
});

beforeEach(function () {
// Add one test to suite to avoid assertions against empty test results
var test = new Test(testTitle, () => {});
test.file = testFile;
suite.addTest(test);
});

it('should write test results to the object', function (done) {
runner.run(function () {
expect(theObject, 'not to be empty');
done();
});
});
});
});
Loading