@@ -27,8 +27,22 @@ const path = require('path');
2727const testResultProcessor = require ( '../' ) ;
2828
2929describe ( 'jest-junit' , ( ) => {
30+ beforeEach ( ( ) => {
31+ const foundKeys = Object . keys ( process . env ) . filter ( k => k . startsWith ( 'JEST_JUNIT' ) ) ;
32+ if ( foundKeys . length > 0 ) {
33+ throw new Error ( `process.env should not have JEST_JUNIT keys set. Found: ${ foundKeys . join ( ', ' ) } ` ) ;
34+ }
35+ } ) ;
36+
37+ afterEach ( ( ) => {
38+ jest . clearAllMocks ( ) ;
3039
31- afterEach ( ( ) => jest . clearAllMocks ( ) )
40+ for ( let key in process . env ) {
41+ if ( key . startsWith ( 'JEST_JUNIT' ) ) {
42+ delete process . env [ key ] ;
43+ }
44+ }
45+ } ) ;
3246
3347 it ( 'should generate valid xml with default name' , ( ) => {
3448 const noFailingTestsReport = require ( '../__mocks__/no-failing-tests.json' ) ;
@@ -67,6 +81,20 @@ describe('jest-junit', () => {
6781 expect ( xmlDoc ) . toBeTruthy ( ) ;
6882 } ) ;
6983
84+ it ( 'should generate valid xml despite illegal characters' , ( ) => {
85+ const failingTestsWithEscReport = require ( '../__mocks__/failing-tests-with-esc.json' ) ;
86+ testResultProcessor ( failingTestsWithEscReport ) ;
87+
88+ // Ensure fs.writeFileSync is called
89+ expect ( fs . writeFileSync ) . toHaveBeenCalledTimes ( 1 ) ;
90+
91+ // Ensure file would have been generated
92+ expect ( fs . writeFileSync ) . toHaveBeenLastCalledWith ( path . resolve ( 'junit.xml' ) , expect . any ( String ) ) ;
93+
94+ // Ensure generated file is valid xml
95+ const xmlDoc = libxmljs . parseXml ( fs . writeFileSync . mock . calls [ 0 ] [ 1 ] ) ;
96+ expect ( xmlDoc ) . toBeTruthy ( ) ;
97+ } ) ;
7098
7199 it ( 'should generate xml at the output filepath defined by JEST_JUNIT_OUTPUT_FILE' , ( ) => {
72100 process . env . JEST_JUNIT_OUTPUT_FILE = 'path_to_output/output_name.xml'
@@ -83,6 +111,7 @@ describe('jest-junit', () => {
83111 } ) ;
84112
85113 it ( 'should generate xml at the output filepath defined by outputFile config' , ( ) => {
114+ process . env . JEST_JUNIT_OUTPUT_FILE = 'path_to_output/output_name.xml'
86115 const noFailingTestsReport = require ( '../__mocks__/no-failing-tests.json' ) ;
87116 testResultProcessor ( noFailingTestsReport , { outputFile : 'path_to_output/output_name.xml' } ) ;
88117
0 commit comments