@@ -796,7 +796,7 @@ describe('getAnonymizedState', () => {
796
796
797
797
it ( 'reports thrown error when deriving state' , ( ) => {
798
798
const captureException = jest . fn ( ) ;
799
- const persistentState = getAnonymizedState (
799
+ const anonymizedState = getAnonymizedState (
800
800
{
801
801
extraState : 'extraState' ,
802
802
privateKey : '123' ,
@@ -820,7 +820,7 @@ describe('getAnonymizedState', () => {
820
820
captureException ,
821
821
) ;
822
822
823
- expect ( persistentState ) . toStrictEqual ( {
823
+ expect ( anonymizedState ) . toStrictEqual ( {
824
824
privateKey : '123' ,
825
825
} ) ;
826
826
expect ( captureException ) . toHaveBeenCalledTimes ( 1 ) ;
@@ -829,11 +829,58 @@ describe('getAnonymizedState', () => {
829
829
) ;
830
830
} ) ;
831
831
832
+ it ( 'logs thrown error and captureException error to console if captureException throws' , ( ) => {
833
+ const consoleError = jest . fn ( ) ;
834
+ const testError = new Error ( 'Test error' ) ;
835
+ const captureException = jest . fn ( ) . mockImplementation ( ( ) => {
836
+ throw testError ;
837
+ } ) ;
838
+ jest . spyOn ( console , 'error' ) . mockImplementation ( consoleError ) ;
839
+ const anonymizedState = getAnonymizedState (
840
+ {
841
+ extraState : 'extraState' ,
842
+ privateKey : '123' ,
843
+ network : 'mainnet' ,
844
+ } ,
845
+ // @ts -expect-error Intentionally testing invalid state
846
+ {
847
+ privateKey : {
848
+ anonymous : true ,
849
+ includeInStateLogs : false ,
850
+ persist : false ,
851
+ usedInUi : false ,
852
+ } ,
853
+ network : {
854
+ anonymous : false ,
855
+ includeInStateLogs : false ,
856
+ persist : false ,
857
+ usedInUi : false ,
858
+ } ,
859
+ } ,
860
+ captureException ,
861
+ ) ;
862
+
863
+ expect ( anonymizedState ) . toStrictEqual ( {
864
+ privateKey : '123' ,
865
+ } ) ;
866
+
867
+ expect ( consoleError ) . toHaveBeenCalledTimes ( 2 ) ;
868
+ expect ( consoleError ) . toHaveBeenNthCalledWith (
869
+ 1 ,
870
+ new Error ( `Error thrown when calling 'captureException'` ) ,
871
+ testError ,
872
+ ) ;
873
+ expect ( consoleError ) . toHaveBeenNthCalledWith (
874
+ 2 ,
875
+ new Error ( `No metadata found for 'extraState'` ) ,
876
+ ) ;
877
+ } ) ;
878
+
832
879
it ( 'logs thrown error to console when deriving state if no captureException function is given' , ( ) => {
833
880
const consoleError = jest . fn ( ) ;
834
881
jest . spyOn ( console , 'error' ) . mockImplementation ( consoleError ) ;
835
882
836
- const persistentState = getAnonymizedState (
883
+ const anonymizedState = getAnonymizedState (
837
884
{
838
885
extraState : 'extraState' ,
839
886
privateKey : '123' ,
@@ -856,7 +903,7 @@ describe('getAnonymizedState', () => {
856
903
} ,
857
904
) ;
858
905
859
- expect ( persistentState ) . toStrictEqual ( {
906
+ expect ( anonymizedState ) . toStrictEqual ( {
860
907
privateKey : '123' ,
861
908
} ) ;
862
909
expect ( consoleError ) . toHaveBeenCalledTimes ( 1 ) ;
@@ -1073,6 +1120,53 @@ describe('getPersistentState', () => {
1073
1120
) ;
1074
1121
} ) ;
1075
1122
1123
+ it ( 'logs thrown error and captureException error to console if captureException throws' , ( ) => {
1124
+ const consoleError = jest . fn ( ) ;
1125
+ const testError = new Error ( 'Test error' ) ;
1126
+ const captureException = jest . fn ( ) . mockImplementation ( ( ) => {
1127
+ throw testError ;
1128
+ } ) ;
1129
+ jest . spyOn ( console , 'error' ) . mockImplementation ( consoleError ) ;
1130
+ const persistentState = getPersistentState (
1131
+ {
1132
+ extraState : 'extraState' ,
1133
+ privateKey : '123' ,
1134
+ network : 'mainnet' ,
1135
+ } ,
1136
+ // @ts -expect-error Intentionally testing invalid state
1137
+ {
1138
+ privateKey : {
1139
+ anonymous : false ,
1140
+ includeInStateLogs : false ,
1141
+ persist : true ,
1142
+ usedInUi : false ,
1143
+ } ,
1144
+ network : {
1145
+ anonymous : false ,
1146
+ includeInStateLogs : false ,
1147
+ persist : false ,
1148
+ usedInUi : false ,
1149
+ } ,
1150
+ } ,
1151
+ captureException ,
1152
+ ) ;
1153
+
1154
+ expect ( persistentState ) . toStrictEqual ( {
1155
+ privateKey : '123' ,
1156
+ } ) ;
1157
+
1158
+ expect ( consoleError ) . toHaveBeenCalledTimes ( 2 ) ;
1159
+ expect ( consoleError ) . toHaveBeenNthCalledWith (
1160
+ 1 ,
1161
+ new Error ( `Error thrown when calling 'captureException'` ) ,
1162
+ testError ,
1163
+ ) ;
1164
+ expect ( consoleError ) . toHaveBeenNthCalledWith (
1165
+ 2 ,
1166
+ new Error ( `No metadata found for 'extraState'` ) ,
1167
+ ) ;
1168
+ } ) ;
1169
+
1076
1170
it ( 'logs thrown error to console when deriving state if no captureException function is given' , ( ) => {
1077
1171
const consoleError = jest . fn ( ) ;
1078
1172
jest . spyOn ( console , 'error' ) . mockImplementation ( consoleError ) ;
@@ -1411,6 +1505,56 @@ describe('deriveStateFromMetadata', () => {
1411
1505
expect ( captureException ) . toHaveBeenCalledWith ( new Error ( testException ) ) ;
1412
1506
} ) ;
1413
1507
1508
+ it ( 'logs thrown error and captureException error to console if captureException throws' , ( ) => {
1509
+ const consoleError = jest . fn ( ) ;
1510
+ const testError = new Error ( 'Test error' ) ;
1511
+ const captureException = jest . fn ( ) . mockImplementation ( ( ) => {
1512
+ throw testError ;
1513
+ } ) ;
1514
+ jest . spyOn ( console , 'error' ) . mockImplementation ( consoleError ) ;
1515
+ const derivedState = deriveStateFromMetadata (
1516
+ {
1517
+ extraState : 'extraState' ,
1518
+ privateKey : '123' ,
1519
+ network : 'mainnet' ,
1520
+ } ,
1521
+ // @ts -expect-error Intentionally testing invalid state
1522
+ {
1523
+ privateKey : {
1524
+ anonymous : false ,
1525
+ includeInStateLogs : false ,
1526
+ persist : false ,
1527
+ usedInUi : false ,
1528
+ [ property ] : true ,
1529
+ } ,
1530
+ network : {
1531
+ anonymous : false ,
1532
+ includeInStateLogs : false ,
1533
+ persist : false ,
1534
+ usedInUi : false ,
1535
+ [ property ] : false ,
1536
+ } ,
1537
+ } ,
1538
+ property ,
1539
+ captureException ,
1540
+ ) ;
1541
+
1542
+ expect ( derivedState ) . toStrictEqual ( {
1543
+ privateKey : '123' ,
1544
+ } ) ;
1545
+
1546
+ expect ( consoleError ) . toHaveBeenCalledTimes ( 2 ) ;
1547
+ expect ( consoleError ) . toHaveBeenNthCalledWith (
1548
+ 1 ,
1549
+ new Error ( `Error thrown when calling 'captureException'` ) ,
1550
+ testError ,
1551
+ ) ;
1552
+ expect ( consoleError ) . toHaveBeenNthCalledWith (
1553
+ 2 ,
1554
+ new Error ( `No metadata found for 'extraState'` ) ,
1555
+ ) ;
1556
+ } ) ;
1557
+
1414
1558
it ( 'logs thrown error to console when deriving state if no captureException function is given' , ( ) => {
1415
1559
const consoleError = jest . fn ( ) ;
1416
1560
jest . spyOn ( console , 'error' ) . mockImplementation ( consoleError ) ;
0 commit comments