@@ -36,43 +36,49 @@ class HTMLReporter {
3636 this . jestConfig ? this . jestConfig . rootDir : "" ,
3737 this . getConfigValue ( "outputPath" ) as string
3838 ) ;
39-
4039 await mkdirp ( path . dirname ( outputPath ) ) ;
40+
41+ let writeFullReport = true ;
4142 if ( this . getConfigValue ( "append" ) as boolean ) {
42- await this . appendToFile ( outputPath , report . toString ( ) ) ;
43- } else {
44- fs . writeFileSync ( outputPath , report . toString ( ) ) ;
43+ const fileExists = fs . existsSync ( outputPath ) ;
44+ if ( fileExists ) {
45+ await this . appendToFile ( outputPath , report . content . toString ( ) ) ;
46+ writeFullReport = false ;
47+ }
48+ }
49+ if ( writeFullReport ) {
50+ fs . writeFileSync ( outputPath , report . fullHtml . toString ( ) ) ;
4551 }
4652
4753 this . logMessage ( "success" , `Report generated (${ outputPath } )` ) ;
48- return report ;
54+ return report . fullHtml ;
4955 } catch ( e ) {
5056 this . logMessage ( "error" , e ) ;
5157 }
5258 }
5359
54- public async renderTestReport ( ) {
55- // Generate the content of the test report
60+ public async renderTestReport ( ) : Promise < {
61+ fullHtml : string ;
62+ content : string ;
63+ } > {
5664 const reportContent = await this . renderTestReportContent ( ) ;
5765
58- // --
59-
6066 // Boilerplate Option
61- if ( ! ! this . getConfigValue ( "boilerplate" ) ) {
67+ if ( this . getConfigValue ( "boilerplate" ) ) {
6268 const boilerplatePath = this . replaceRootDirInPath (
6369 this . jestConfig ? this . jestConfig . rootDir : "" ,
6470 this . getConfigValue ( "boilerplate" ) as string
6571 ) ;
66-
6772 const boilerplateContent = fs . readFileSync ( boilerplatePath , "utf8" ) ;
68- return boilerplateContent . replace (
69- "{jesthtmlreporter-content}" ,
70- reportContent && reportContent . toString ( )
71- ) ;
73+ return {
74+ content : reportContent . toString ( ) ,
75+ fullHtml : boilerplateContent . replace (
76+ "{jesthtmlreporter-content}" ,
77+ reportContent && reportContent . toString ( )
78+ ) ,
79+ } ;
7280 }
7381
74- // --
75-
7682 // Create HTML and apply reporter content
7783 const report = xmlbuilder . create ( { html : { } } ) ;
7884 const headTag = report . ele ( "head" ) ;
@@ -115,7 +121,10 @@ class HTMLReporter {
115121 `<script src="${ this . getConfigValue ( "customScriptPath" ) } "></script>`
116122 ) ;
117123 }
118- return report ;
124+ return {
125+ fullHtml : report . toString ( ) ,
126+ content : reportContent . toString ( ) ,
127+ } ;
119128 }
120129
121130 public renderTestSuiteInfo ( parent : XMLElement , suite : TestResult ) {
@@ -178,7 +187,7 @@ class HTMLReporter {
178187
179188 // HTML Body
180189 const reportBody : XMLElement = xmlbuilder . begin ( ) . element ( "div" , {
181- id : "jesthtml-content" ,
190+ class : "jesthtml-content" ,
182191 } ) ;
183192
184193 /**
@@ -668,32 +677,26 @@ class HTMLReporter {
668677 * @param filePath
669678 * @param content
670679 */
671- public async appendToFile ( filePath : string , content : any ) {
680+ public async appendToFile ( filePath : string , content : string ) {
672681 let parsedContent = content ;
673- // Check if the file exists or not
674- const fileExists = fs . existsSync ( filePath ) ;
675- // The file exists - we need to strip all unnecessary html
676- if ( fileExists ) {
677- const fileToAppend = fs . readFileSync ( filePath , "utf8" ) ;
678- const contentSearch = / < b o d y > ( .* ?) < \/ b o d y > / gm. exec ( content ) ;
679- if ( contentSearch ) {
680- const [ strippedContent ] = contentSearch ;
681- parsedContent = strippedContent ;
682- }
683- // Then we need to add the stripped content just before the </body> tag
684- let newContent = fileToAppend ;
685- const closingBodyTag = / < \/ b o d y > / gm. exec ( fileToAppend ) ;
686- const indexOfClosingBodyTag = closingBodyTag ? closingBodyTag . index : 0 ;
687-
688- newContent = [
689- fileToAppend . slice ( 0 , indexOfClosingBodyTag ) ,
690- parsedContent ,
691- fileToAppend . slice ( indexOfClosingBodyTag ) ,
692- ] . join ( "" ) ;
693-
694- return fs . writeFileSync ( filePath , newContent ) ;
682+ const fileToAppend = fs . readFileSync ( filePath , "utf8" ) ;
683+ const contentSearch = / < b o d y > ( .* ?) < \/ b o d y > / gm. exec ( content ) ;
684+ if ( contentSearch ) {
685+ const [ strippedContent ] = contentSearch ;
686+ parsedContent = strippedContent ;
695687 }
696- return fs . appendFileSync ( filePath , parsedContent ) ;
688+ // Then we need to add the stripped content just before the </body> tag
689+ let newContent = fileToAppend ;
690+ const closingBodyTag = / < \/ b o d y > / gm. exec ( fileToAppend ) ;
691+ const indexOfClosingBodyTag = closingBodyTag ? closingBodyTag . index : 0 ;
692+
693+ newContent = [
694+ fileToAppend . slice ( 0 , indexOfClosingBodyTag ) ,
695+ parsedContent ,
696+ fileToAppend . slice ( indexOfClosingBodyTag ) ,
697+ ] . join ( "" ) ;
698+
699+ return fs . writeFileSync ( filePath , newContent ) ;
697700 }
698701
699702 /**
@@ -703,8 +706,8 @@ class HTMLReporter {
703706 * @param filePath
704707 */
705708 public replaceRootDirInPath (
706- rootDir : Config . GlobalConfig [ ' rootDir' ] ,
707- filePath : Config . GlobalConfig [ ' testPathPattern' ]
709+ rootDir : Config . GlobalConfig [ " rootDir" ] ,
710+ filePath : Config . GlobalConfig [ " testPathPattern" ]
708711 ) : string {
709712 if ( ! / ^ < r o o t D i r > / . test ( filePath ) ) {
710713 return filePath ;
0 commit comments