@@ -64,64 +64,91 @@ function loadFile(filename) {
64
64
/**
65
65
* Get the dateformat format string based on the timestamp option
66
66
*
67
- * @param {string|boolean } timestamp Timestamp option value
67
+ * @param {string|boolean } ts Timestamp option value
68
68
*
69
69
* @return {string } Valid dateformat format string
70
70
*/
71
- function getTimestampFormat ( timestamp ) {
72
- switch ( timestamp ) {
73
- case '' :
74
- case 'true' :
75
- case true :
76
- return 'isoDateTime' ;
77
- default :
78
- return timestamp ;
79
- }
71
+ function getTimestampFormat ( ts ) {
72
+ return ts === '' ||
73
+ ts === true ||
74
+ ts === 'true' ||
75
+ ts === false ||
76
+ ts === 'false'
77
+ ? 'isoDateTime'
78
+ : ts ;
80
79
}
81
80
82
81
/**
83
82
* Construct the path/name of the HTML/JSON file to be saved
84
83
*
85
- * @param {Object } reportOptions Options object
84
+ * @param {object } reportOptions Options object
86
85
* @param {string } reportOptions.reportDir Directory to save report to
87
86
* @param {string } reportOptions.reportFilename Filename to save report to
88
87
* @param {string } reportOptions.timestamp Timestamp format to be appended to the filename
88
+ * @param {object } reportData JSON test data
89
89
*
90
90
* @return {string } Fully resolved path without extension
91
91
*/
92
- function getFilename ( { reportDir, reportFilename = 'mochawesome' , timestamp } ) {
93
- let ts = '' ;
92
+ function getFilename ( { reportDir, reportFilename, timestamp } , reportData ) {
93
+ const DEFAULT_FILENAME = 'mochawesome' ;
94
+ const NAME_REPLACE = '[name]' ;
95
+ const STATUS_REPLACE = '[status]' ;
96
+ const DATETIME_REPLACE = '[datetime]' ;
97
+ const STATUSES = {
98
+ Pass : 'pass' ,
99
+ Fail : 'fail' ,
100
+ } ;
101
+
102
+ let filename = reportFilename || DEFAULT_FILENAME ;
103
+
104
+ const hasDatetimeReplacement = filename . includes ( DATETIME_REPLACE ) ;
105
+ const tsFormat = getTimestampFormat ( timestamp ) ;
106
+ const ts = dateFormat ( new Date ( ) , tsFormat )
107
+ // replace commas, spaces or comma-space combinations with underscores
108
+ . replace ( / ( , \s * ) | , | \s + / g, '_' )
109
+ // replace forward and back slashes with hyphens
110
+ . replace ( / \\ | \/ / g, '-' )
111
+ // remove colons
112
+ . replace ( / : / g, '' ) ;
113
+
94
114
if ( timestamp !== false && timestamp !== 'false' ) {
95
- const format = getTimestampFormat ( timestamp ) ;
96
- ts = `_${ dateFormat ( new Date ( ) , format ) } `
97
- // replace commas, spaces or comma-space combinations with underscores
98
- . replace ( / ( , \s * ) | , | \s + / g, '_' )
99
- // replace forward and back slashes with hyphens
100
- . replace ( / \\ | \/ / g, '-' )
101
- // remove colons
102
- . replace ( / : / g, '' ) ;
115
+ if ( ! hasDatetimeReplacement ) {
116
+ filename = `${ filename } _${ DATETIME_REPLACE } ` ;
117
+ }
103
118
}
104
- const filename = `${ reportFilename . replace ( fileExtRegex , '' ) } ${ ts } ` ;
119
+
120
+ const specFilename = path
121
+ . basename ( reportData . results [ 0 ] . file || '' )
122
+ . replace ( / \. .+ / , '' ) ;
123
+
124
+ const status = reportData . stats . failures > 0 ? STATUSES . Fail : STATUSES . Pass ;
125
+
126
+ filename = filename
127
+ . replace ( NAME_REPLACE , specFilename || DEFAULT_FILENAME )
128
+ . replace ( STATUS_REPLACE , status )
129
+ . replace ( DATETIME_REPLACE , ts ) ;
130
+
105
131
return path . resolve ( process . cwd ( ) , reportDir , filename ) ;
106
132
}
107
133
108
134
/**
109
135
* Get report options by extending base options
110
136
* with user provided options
111
137
*
112
- * @param {Object } opts Report options
138
+ * @param {object } opts Report options
139
+ * @param {object } reportData JSON test data
113
140
*
114
- * @return {Object } User options merged with default options
141
+ * @return {object } User options merged with default options
115
142
*/
116
- function getOptions ( opts ) {
143
+ function getOptions ( opts , reportData ) {
117
144
const mergedOptions = getMergedOptions ( opts || { } ) ;
118
145
119
146
// For saving JSON from mochawesome reporter
120
147
if ( mergedOptions . saveJson ) {
121
- mergedOptions . jsonFile = `${ getFilename ( mergedOptions ) } .json` ;
148
+ mergedOptions . jsonFile = `${ getFilename ( mergedOptions , reportData ) } .json` ;
122
149
}
123
150
124
- mergedOptions . htmlFile = `${ getFilename ( mergedOptions ) } .html` ;
151
+ mergedOptions . htmlFile = `${ getFilename ( mergedOptions , reportData ) } .html` ;
125
152
return mergedOptions ;
126
153
}
127
154
@@ -159,7 +186,7 @@ function _shouldCopyAssets(assetsDir) {
159
186
/**
160
187
* Copy the report assets to the report dir, ignoring inline assets
161
188
*
162
- * @param {Object } opts Report options
189
+ * @param {object } opts Report options
163
190
*/
164
191
function copyAssets ( { assetsDir } ) {
165
192
if ( _shouldCopyAssets ( assetsDir ) ) {
@@ -172,8 +199,8 @@ function copyAssets({ assetsDir }) {
172
199
/**
173
200
* Get the report assets object
174
201
*
175
- * @param {Object } reportOptions Options
176
- * @return {Object } Object with assets props
202
+ * @param {object } reportOptions Options
203
+ * @return {object } Object with assets props
177
204
*/
178
205
function getAssets ( reportOptions ) {
179
206
const { assetsDir, cdn, dev, inlineAssets, reportDir } = reportOptions ;
@@ -220,20 +247,14 @@ function getAssets(reportOptions) {
220
247
/**
221
248
* Prepare options, assets, and html for saving
222
249
*
223
- * @param {string } reportData JSON test data
224
- * @param {Object } opts Report options
250
+ * @param {object } reportData JSON test data
251
+ * @param {object } opts Report options
225
252
*
226
- * @return {Object } Prepared data for saving
253
+ * @return {object } Prepared data for saving
227
254
*/
228
255
function prepare ( reportData , opts ) {
229
- // Stringify the data if needed
230
- let data = reportData ;
231
- if ( typeof data === 'object' ) {
232
- data = JSON . stringify ( reportData ) ;
233
- }
234
-
235
256
// Get the options
236
- const reportOptions = getOptions ( opts ) ;
257
+ const reportOptions = getOptions ( opts , reportData ) ;
237
258
238
259
// Stop here if we're not generating an HTML report
239
260
if ( ! reportOptions . saveHtml ) {
@@ -245,7 +266,7 @@ function prepare(reportData, opts) {
245
266
246
267
// Render basic template to string
247
268
const renderedHtml = renderMainHTML ( {
248
- data,
269
+ data : JSON . stringify ( reportData ) ,
249
270
options : reportOptions ,
250
271
title : reportOptions . reportPageTitle ,
251
272
useInlineAssets : reportOptions . inlineAssets && ! reportOptions . cdn ,
@@ -259,8 +280,8 @@ function prepare(reportData, opts) {
259
280
/**
260
281
* Create the report
261
282
*
262
- * @param {string } data JSON test data
263
- * @param {Object } opts Report options
283
+ * @param {object } data JSON test data
284
+ * @param {object } opts Report options
264
285
*
265
286
* @return {Promise } Resolves if report was created successfully
266
287
*/
@@ -302,8 +323,8 @@ function create(data, opts) {
302
323
/**
303
324
* Create the report synchronously
304
325
*
305
- * @param {string } data JSON test data
306
- * @param {Object } opts Report options
326
+ * @param {object } data JSON test data
327
+ * @param {object } opts Report options
307
328
*
308
329
*/
309
330
function createSync ( data , opts ) {
0 commit comments