Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@
"diff": "^5.2.0",
"diff-match-patch": "^1.0.5",
"fast-deep-equal": "^3.1.3",
"fast-xml-parser": "^4.5.1",
"fast-xml-parser": "^5.0.0",
"fastest-levenshtein": "^1.0.16",
"fzf": "^0.5.2",
"get-folder-size": "^5.0.0",
Expand Down
50 changes: 31 additions & 19 deletions src/utils/__tests__/xml.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,35 +117,47 @@ describe("parseXml", () => {

describe("error handling", () => {
it("wraps parser errors with a descriptive message", () => {
// Use jest.spyOn to mock the XMLParser implementation
// Create a mock implementation that throws an error
const mockParseFn = jest.fn().mockImplementation(() => {
throw new Error("Simulated parsing error")
})

// Create a mock parser instance
const mockParserInstance = {
parse: mockParseFn,
}

// Spy on the XMLParser constructor to return our mock
const parserSpy = jest
.spyOn(require("fast-xml-parser"), "XMLParser")
.mockImplementation(() => mockParserInstance)

// Test that our function wraps the error appropriately
expect(() => parseXml("<root></root>")).toThrow("Failed to parse XML: Simulated parsing error")

// Verify the parser was called with the expected options
expect(parserSpy).toHaveBeenCalledWith({
ignoreAttributes: false,
attributeNamePrefix: "@_",
parseAttributeValue: false,
parseTagValue: false,
trimValues: true,
stopNodes: [],
// Create a mock constructor function
const MockXMLParser = jest.fn().mockImplementation(() => mockParserInstance)

// Save the original XMLParser
const { XMLParser } = jest.requireActual("fast-xml-parser")

// Replace the XMLParser with our mock
jest.doMock("fast-xml-parser", () => ({
XMLParser: MockXMLParser,
}))

// Import the module with our mocked dependency
jest.isolateModules(() => {
const { parseXml } = require("../xml")

// Test that our function wraps the error appropriately
expect(() => parseXml("<root></root>")).toThrow("Failed to parse XML: Simulated parsing error")

// Verify the parser was called with the expected options
expect(MockXMLParser).toHaveBeenCalledWith({
ignoreAttributes: false,
attributeNamePrefix: "@_",
parseAttributeValue: false,
parseTagValue: false,
trimValues: true,
stopNodes: [],
})
})

// Cleanup
parserSpy.mockRestore()
// Restore the original module
jest.dontMock("fast-xml-parser")
})
})
})
Loading