4
4
5
5
var Base = require ( './base' )
6
6
, utils = require ( '../utils' )
7
+ , fs = require ( 'fs' )
7
8
, escape = utils . escape ;
8
9
9
10
/**
@@ -29,12 +30,19 @@ exports = module.exports = XUnit;
29
30
* @api public
30
31
*/
31
32
32
- function XUnit ( runner ) {
33
+ function XUnit ( runner , options ) {
33
34
Base . call ( this , runner ) ;
34
35
var stats = this . stats
35
36
, tests = [ ]
36
37
, self = this ;
37
38
39
+ if ( options . reporterOptions && options . reporterOptions . output ) {
40
+ if ( ! fs . createWriteStream ) {
41
+ throw new Error ( 'file output not supported in browser' ) ;
42
+ }
43
+ self . fileStream = fs . createWriteStream ( options . reporterOptions . output ) ;
44
+ }
45
+
38
46
runner . on ( 'pending' , function ( test ) {
39
47
tests . push ( test ) ;
40
48
} ) ;
@@ -48,7 +56,7 @@ function XUnit(runner) {
48
56
} ) ;
49
57
50
58
runner . on ( 'end' , function ( ) {
51
- console . log ( tag ( 'testsuite' , {
59
+ self . write ( tag ( 'testsuite' , {
52
60
name : 'Mocha Tests'
53
61
, tests : stats . tests
54
62
, failures : stats . failures
@@ -58,22 +66,46 @@ function XUnit(runner) {
58
66
, time : ( stats . duration / 1000 ) || 0
59
67
} , false ) ) ;
60
68
61
- tests . forEach ( test ) ;
62
- console . log ( '</testsuite>' ) ;
69
+ tests . forEach ( function ( t ) { self . test ( t ) ; } ) ;
70
+ self . write ( '</testsuite>' ) ;
63
71
} ) ;
64
72
}
65
73
74
+ /**
75
+ * Override done to close the stream (if it's a file).
76
+ */
77
+ XUnit . prototype . done = function ( failures , fn ) {
78
+ if ( this . fileStream ) {
79
+ this . fileStream . end ( function ( ) {
80
+ fn ( failures ) ;
81
+ } ) ;
82
+ } else {
83
+ fn ( failures ) ;
84
+ }
85
+ } ;
86
+
66
87
/**
67
88
* Inherit from `Base.prototype`.
68
89
*/
69
90
70
91
XUnit . prototype . __proto__ = Base . prototype ;
71
92
93
+ /**
94
+ * Write out the given line
95
+ */
96
+ XUnit . prototype . write = function ( line ) {
97
+ if ( this . fileStream ) {
98
+ this . fileStream . write ( line + '\n' ) ;
99
+ } else {
100
+ console . log ( line ) ;
101
+ }
102
+ } ;
103
+
72
104
/**
73
105
* Output tag for the given `test.`
74
106
*/
75
107
76
- function test ( test ) {
108
+ XUnit . prototype . test = function ( test , ostream ) {
77
109
var attrs = {
78
110
classname : test . parent . fullTitle ( )
79
111
, name : test . title
@@ -82,13 +114,13 @@ function test(test) {
82
114
83
115
if ( 'failed' == test . state ) {
84
116
var err = test . err ;
85
- console . log ( tag ( 'testcase' , attrs , false , tag ( 'failure' , { } , false , cdata ( escape ( err . message ) + "\n" + err . stack ) ) ) ) ;
117
+ this . write ( tag ( 'testcase' , attrs , false , tag ( 'failure' , { } , false , cdata ( escape ( err . message ) + "\n" + err . stack ) ) ) ) ;
86
118
} else if ( test . pending ) {
87
- console . log ( tag ( 'testcase' , attrs , false , tag ( 'skipped' , { } , true ) ) ) ;
119
+ this . write ( tag ( 'testcase' , attrs , false , tag ( 'skipped' , { } , true ) ) ) ;
88
120
} else {
89
- console . log ( tag ( 'testcase' , attrs , true ) ) ;
121
+ this . write ( tag ( 'testcase' , attrs , true ) ) ;
90
122
}
91
- }
123
+ } ;
92
124
93
125
/**
94
126
* HTML tag helper.
0 commit comments