Skip to content

Commit eff4daa

Browse files
committed
try/catch plus test
1 parent 6a93d47 commit eff4daa

File tree

5 files changed

+70
-34
lines changed

5 files changed

+70
-34
lines changed

example/config/.mocharc.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ module.exports = {
3333
parallel: false,
3434
recursive: false,
3535
reporter: 'spec',
36-
'reporter-option': ['foo=bar', 'baz=quux'],
36+
'reporter-option': ['foo=bar', 'baz=quux'], // array, not object
3737
require: '@babel/register',
3838
retries: 1,
3939
slow: '75',

example/config/.mocharc.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ package: './package.json'
3535
parallel: false
3636
recursive: false
3737
reporter: 'spec'
38-
reporter-option:
38+
reporter-option: # array, not object
3939
- 'foo=bar'
4040
- 'baz=quux'
4141
require: '@babel/register'

lib/reporters/base.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ exports.colors = {
9090

9191
exports.symbols = {
9292
ok: symbols.success,
93-
err: symbols.err,
93+
err: symbols.error,
9494
dot: '.',
9595
comma: ',',
9696
bang: '!'

lib/reporters/json.js

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@
99
var Base = require('./base');
1010
var fs = require('fs');
1111
var path = require('path');
12-
var errors = require('../errors');
13-
var createUnsupportedError = errors.createUnsupportedError;
12+
const createUnsupportedError = require('../errors').createUnsupportedError;
13+
const utils = require('../utils');
1414
var constants = require('../runner').constants;
1515
var EVENT_TEST_PASS = constants.EVENT_TEST_PASS;
16+
var EVENT_TEST_PENDING = constants.EVENT_TEST_PENDING;
1617
var EVENT_TEST_FAIL = constants.EVENT_TEST_FAIL;
1718
var EVENT_TEST_END = constants.EVENT_TEST_END;
1819
var EVENT_RUN_END = constants.EVENT_RUN_END;
19-
var EVENT_TEST_PENDING = constants.EVENT_TEST_PENDING;
2020

2121
/**
2222
* Expose `JSON`.
@@ -34,7 +34,7 @@ exports = module.exports = JSONReporter;
3434
* @param {Runner} runner - Instance triggers reporter actions.
3535
* @param {Object} [options] - runner options
3636
*/
37-
function JSONReporter(runner, options) {
37+
function JSONReporter(runner, options = {}) {
3838
Base.call(this, runner, options);
3939

4040
var self = this;
@@ -44,11 +44,11 @@ function JSONReporter(runner, options) {
4444
var passes = [];
4545
var output;
4646

47-
if (options && options.reporterOptions && options.reporterOptions.output) {
48-
if (!fs || !fs.writeFileSync) {
47+
if (options.reporterOption && options.reporterOption.output) {
48+
if (utils.isBrowser()) {
4949
throw createUnsupportedError('file output not supported in browser');
5050
}
51-
output = options.reporterOptions.output;
51+
output = options.reporterOption.output;
5252
}
5353

5454
runner.on(EVENT_TEST_END, function(test) {
@@ -80,8 +80,15 @@ function JSONReporter(runner, options) {
8080

8181
var json = JSON.stringify(obj, null, 2);
8282
if (output) {
83-
fs.mkdirSync(path.dirname(output), {recursive: true});
84-
fs.writeFileSync(output, json);
83+
try {
84+
fs.mkdirSync(path.dirname(output), {recursive: true});
85+
fs.writeFileSync(output, json);
86+
} catch (err) {
87+
console.error(
88+
`${Base.symbols.err} [mocha] writing output to "${output}" failed: ${err.message}\n`
89+
);
90+
process.stdout.write(json);
91+
}
8592
} else {
8693
process.stdout.write(json);
8794
}

test/reporters/json.spec.js

Lines changed: 51 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
var fs = require('fs');
44
var sinon = require('sinon');
55
var JSONReporter = require('../../lib/reporters/json');
6+
var utils = require('../../lib/utils');
67
var Mocha = require('../../');
78
var Suite = Mocha.Suite;
89
var Runner = Mocha.Runner;
@@ -98,6 +99,7 @@ describe('JSON reporter', function() {
9899
suite.addTest(test);
99100

100101
runner.run(function(failureCount) {
102+
sinon.restore();
101103
expect(runner, 'to satisfy', {
102104
testResults: {
103105
passes: [
@@ -150,11 +152,11 @@ describe('JSON reporter', function() {
150152
});
151153
});
152154

153-
describe("when 'reporterOptions.output' is provided", function() {
155+
describe('when "reporterOption.output" is provided', function() {
154156
var expectedDirName = 'reports';
155157
var expectedFileName = 'reports/test-results.json';
156158
var options = {
157-
reporterOptions: {
159+
reporterOption: {
158160
output: expectedFileName
159161
}
160162
};
@@ -171,34 +173,61 @@ describe('JSON reporter', function() {
171173
suite.addTest(test);
172174
});
173175

174-
describe('when file can be created', function() {
175-
it('should write test results to file', function(done) {
176-
var fsMkdirSync = sinon.stub(fs, 'mkdirSync');
177-
var fsWriteFileSync = sinon.stub(fs, 'writeFileSync');
176+
it('should write test results to file', function(done) {
177+
const fsMkdirSync = sinon.stub(fs, 'mkdirSync');
178+
const fsWriteFileSync = sinon.stub(fs, 'writeFileSync');
178179

179-
fsWriteFileSync.callsFake(function(filename, content) {
180-
var expectedJson = JSON.stringify(runner.testResults, null, 2);
181-
expect(expectedFileName, 'to be', filename);
182-
expect(content, 'to be', expectedJson);
183-
});
180+
fsWriteFileSync.callsFake(function(filename, content) {
181+
const expectedJson = JSON.stringify(runner.testResults, null, 2);
182+
expect(expectedFileName, 'to be', filename);
183+
expect(content, 'to be', expectedJson);
184+
});
184185

185-
runner.run(function() {
186-
fsMkdirSync.calledWith(expectedDirName, {recursive: true});
187-
expect(fsWriteFileSync.calledOnce, 'to be true');
188-
done();
189-
});
186+
runner.run(function() {
187+
expect(
188+
fsMkdirSync.calledWith(expectedDirName, {recursive: true}),
189+
'to be true'
190+
);
191+
expect(fsWriteFileSync.calledOnce, 'to be true');
192+
done();
190193
});
191194
});
192195

193-
describe('when run in browser', function() {
194-
it('should throw unsupported error', function() {
195-
sinon.stub(fs, 'writeFileSync').value(false);
196+
it('should warn and write test results to console', function(done) {
197+
const fsMkdirSync = sinon.stub(fs, 'mkdirSync');
198+
const fsWriteFileSync = sinon.stub(fs, 'writeFileSync');
199+
200+
fsWriteFileSync.throws('unable to write file');
201+
202+
const outLog = [];
203+
const fake = chunk => outLog.push(chunk);
204+
sinon.stub(process.stderr, 'write').callsFake(fake);
205+
sinon.stub(process.stdout, 'write').callsFake(fake);
206+
207+
runner.run(function() {
208+
sinon.restore();
209+
expect(
210+
fsMkdirSync.calledWith(expectedDirName, {recursive: true}),
211+
'to be true'
212+
);
213+
expect(fsWriteFileSync.calledOnce, 'to be true');
196214
expect(
197-
() => new JSONReporter(runner, options),
198-
'to throw',
199-
'file output not supported in browser'
215+
outLog[0],
216+
'to contain',
217+
`[mocha] writing output to "${expectedFileName}" failed:`
200218
);
219+
expect(outLog[1], 'to match', /"fullTitle": "JSON suite json test 1"/);
220+
done();
201221
});
202222
});
223+
224+
it('should throw "unsupported error" in browser', function() {
225+
sinon.stub(utils, 'isBrowser').callsFake(() => true);
226+
expect(
227+
() => new JSONReporter(runner, options),
228+
'to throw',
229+
'file output not supported in browser'
230+
);
231+
});
203232
});
204233
});

0 commit comments

Comments
 (0)