@@ -955,4 +955,42 @@ i -PassThru:$PassThru {
955955 $xmlResult.Validate ({ throw $args [1 ].Exception })
956956 }
957957 }
958+
959+ # Regression test for https://github.com/pester/Pester/issues/2649
960+ # When a test outputs an object whose ToString() throws, the NUnit3 report
961+ # writer (Format-CDataString) would crash, losing the entire report.
962+ # The fix wraps ToString() in try/catch and uses a fallback string.
963+ b " NUnit3 report handles objects with broken ToString()" {
964+ t " should produce valid report when test output object throws on ToString" {
965+ $sb = {
966+ Describe ' Describe' {
967+ It ' Outputs broken object' {
968+ # Output an object whose ToString() will throw
969+ $broken = [PSCustomObject ]@ { Name = ' X' }
970+ $broken | Add-Member - MemberType ScriptMethod - Name ToString - Force - Value {
971+ throw [System.InvalidOperationException ]::new(' ToString failed' )
972+ }
973+ $broken
974+ $true | Should - Be $true
975+ }
976+ }
977+ }
978+
979+ $r = Invoke-Pester - Configuration ([PesterConfiguration ]@ {
980+ Run = @ { ScriptBlock = $sb ; PassThru = $true }
981+ Output = @ { Verbosity = ' None' }
982+ })
983+
984+ # The test itself should pass
985+ $r.Result | Verify- Equal ' Passed'
986+
987+ # Converting to NUnit3 report should NOT throw
988+ $xmlResult = $r | ConvertTo-NUnitReport - Format NUnit3
989+
990+ # The output section should contain the fallback message
991+ $xmlTest = $xmlResult .' test-run' .' test-suite' .' test-suite' .' test-case'
992+ $xmlTest.result | Verify- Equal ' Passed'
993+ $xmlTest.output .' #cdata-section' | Verify- Like ' *ToString() failed*'
994+ }
995+ }
958996}
0 commit comments