33var fs = require ( 'fs' ) ;
44var sinon = require ( 'sinon' ) ;
55var JSONReporter = require ( '../../lib/reporters/json' ) ;
6+ var utils = require ( '../../lib/utils' ) ;
67var Mocha = require ( '../../' ) ;
78var Suite = Mocha . Suite ;
89var 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' , / " f u l l T i t l e " : " J S O N s u i t e j s o n t e s t 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