@@ -117,21 +117,35 @@ describe("parseXml", () => {
117117
118118 describe ( "error handling" , ( ) => {
119119 it ( "wraps parser errors with a descriptive message" , ( ) => {
120- // Mock XMLParser to simulate a parsing error
121- const originalParser = require ( "fast-xml-parser" ) . XMLParser
122- require ( "fast-xml-parser" ) . XMLParser = jest . fn ( ) . mockImplementation ( ( ) => {
123- return {
124- parse : ( ) => {
125- throw new Error ( "Simulated parsing error" )
126- } ,
127- }
120+ // Use jest.spyOn to mock the XMLParser implementation
121+ const mockParseFn = jest . fn ( ) . mockImplementation ( ( ) => {
122+ throw new Error ( "Simulated parsing error" )
128123 } )
129124
125+ const mockParserInstance = {
126+ parse : mockParseFn ,
127+ }
128+
129+ // Spy on the XMLParser constructor to return our mock
130+ const parserSpy = jest
131+ . spyOn ( require ( "fast-xml-parser" ) , "XMLParser" )
132+ . mockImplementation ( ( ) => mockParserInstance )
133+
130134 // Test that our function wraps the error appropriately
131135 expect ( ( ) => parseXml ( "<root></root>" ) ) . toThrow ( "Failed to parse XML: Simulated parsing error" )
132136
133- // Restore the original parser
134- require ( "fast-xml-parser" ) . XMLParser = originalParser
137+ // Verify the parser was called with the expected options
138+ expect ( parserSpy ) . toHaveBeenCalledWith ( {
139+ ignoreAttributes : false ,
140+ attributeNamePrefix : "@_" ,
141+ parseAttributeValue : false ,
142+ parseTagValue : false ,
143+ trimValues : true ,
144+ stopNodes : [ ] ,
145+ } )
146+
147+ // Cleanup
148+ parserSpy . mockRestore ( )
135149 } )
136150 } )
137151} )
0 commit comments