Skip to content

Commit

Permalink
try/catch of write output
Browse files Browse the repository at this point in the history
  • Loading branch information
juergba committed Aug 17, 2021
1 parent 6a93d47 commit d5162f3
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
26 changes: 17 additions & 9 deletions lib/reporters/json.js
Expand Up @@ -8,15 +8,16 @@

var Base = require('./base');
var fs = require('fs');
const logSymbols = require('log-symbols');
var path = require('path');
var errors = require('../errors');
var createUnsupportedError = errors.createUnsupportedError;
const createUnsupportedError = require('../errors').createUnsupportedError;
const utils = require('../utils');
var constants = require('../runner').constants;
var EVENT_TEST_PASS = constants.EVENT_TEST_PASS;
var EVENT_TEST_PENDING = constants.EVENT_TEST_PENDING;
var EVENT_TEST_FAIL = constants.EVENT_TEST_FAIL;
var EVENT_TEST_END = constants.EVENT_TEST_END;
var EVENT_RUN_END = constants.EVENT_RUN_END;
var EVENT_TEST_PENDING = constants.EVENT_TEST_PENDING;

/**
* Expose `JSON`.
Expand All @@ -34,7 +35,7 @@ exports = module.exports = JSONReporter;
* @param {Runner} runner - Instance triggers reporter actions.
* @param {Object} [options] - runner options
*/
function JSONReporter(runner, options) {
function JSONReporter(runner, options = {}) {
Base.call(this, runner, options);

var self = this;
Expand All @@ -44,11 +45,11 @@ function JSONReporter(runner, options) {
var passes = [];
var output;

if (options && options.reporterOptions && options.reporterOptions.output) {
if (!fs || !fs.writeFileSync) {
if (options.reporterOption && options.reporterOption.output) {
if (utils.isBrowser()) {
throw createUnsupportedError('file output not supported in browser');
}
output = options.reporterOptions.output;
output = options.reporterOption.output;
}

runner.on(EVENT_TEST_END, function(test) {
Expand Down Expand Up @@ -80,8 +81,15 @@ function JSONReporter(runner, options) {

var json = JSON.stringify(obj, null, 2);
if (output) {
fs.mkdirSync(path.dirname(output), {recursive: true});
fs.writeFileSync(output, json);
try {
fs.mkdirSync(path.dirname(output), {recursive: true});
fs.writeFileSync(output, json);
} catch (err) {
console.error(
`${logSymbols.error} [mocha] writing output to "${output}" failed: ${err.message}\n`
);
process.stdout.write(json);
}
} else {
process.stdout.write(json);
}
Expand Down
7 changes: 4 additions & 3 deletions test/reporters/json.spec.js
Expand Up @@ -3,6 +3,7 @@
var fs = require('fs');
var sinon = require('sinon');
var JSONReporter = require('../../lib/reporters/json');
var utils = require('../../lib/utils');
var Mocha = require('../../');
var Suite = Mocha.Suite;
var Runner = Mocha.Runner;
Expand Down Expand Up @@ -150,11 +151,11 @@ describe('JSON reporter', function() {
});
});

describe("when 'reporterOptions.output' is provided", function() {
describe("when 'reporterOption.output' is provided", function() {
var expectedDirName = 'reports';
var expectedFileName = 'reports/test-results.json';
var options = {
reporterOptions: {
reporterOption: {
output: expectedFileName
}
};
Expand Down Expand Up @@ -192,7 +193,7 @@ describe('JSON reporter', function() {

describe('when run in browser', function() {
it('should throw unsupported error', function() {
sinon.stub(fs, 'writeFileSync').value(false);
sinon.stub(utils, 'isBrowser').callsFake(() => true);
expect(
() => new JSONReporter(runner, options),
'to throw',
Expand Down

0 comments on commit d5162f3

Please sign in to comment.