@@ -783,6 +783,83 @@ describe('useMutation', () => {
783783 consoleMock . mockRestore ( )
784784 } )
785785
786+ it ( 'should not throw an error when throwOnError is set to false' , async ( ) => {
787+ function Page ( ) {
788+ const { mutate, error } = useMutation < string , Error > ( {
789+ mutationFn : ( ) =>
790+ sleep ( 10 ) . then ( ( ) => {
791+ throw new Error ( 'Expected mock error' )
792+ } ) ,
793+ throwOnError : false ,
794+ } )
795+
796+ return (
797+ < div >
798+ < button onClick = { ( ) => mutate ( ) } > mutate</ button >
799+ < div > error: { error ?. message ?? 'null' } </ div >
800+ </ div >
801+ )
802+ }
803+
804+ const rendered = renderWithClient ( queryClient , < Page /> )
805+
806+ fireEvent . click ( rendered . getByRole ( 'button' , { name : / m u t a t e / i } ) )
807+ await vi . advanceTimersByTimeAsync ( 11 )
808+
809+ expect ( rendered . getByText ( 'error: Expected mock error' ) ) . toBeInTheDocument ( )
810+ } )
811+
812+ it ( 'should not throw an error when throwOnError is a function that returns false' , async ( ) => {
813+ function Page ( ) {
814+ const { mutate, error } = useMutation < string , Error > ( {
815+ mutationFn : ( ) =>
816+ sleep ( 10 ) . then ( ( ) => {
817+ throw new Error ( 'Expected mock error' )
818+ } ) ,
819+ throwOnError : ( ) => false ,
820+ } )
821+
822+ return (
823+ < div >
824+ < button onClick = { ( ) => mutate ( ) } > mutate</ button >
825+ < div > error: { error ?. message ?? 'null' } </ div >
826+ </ div >
827+ )
828+ }
829+
830+ const rendered = renderWithClient ( queryClient , < Page /> )
831+
832+ fireEvent . click ( rendered . getByRole ( 'button' , { name : / m u t a t e / i } ) )
833+ await vi . advanceTimersByTimeAsync ( 11 )
834+
835+ expect ( rendered . getByText ( 'error: Expected mock error' ) ) . toBeInTheDocument ( )
836+ } )
837+
838+ it ( 'should not throw an error when throwOnError is not set' , async ( ) => {
839+ function Page ( ) {
840+ const { mutate, error } = useMutation < string , Error > ( {
841+ mutationFn : ( ) =>
842+ sleep ( 10 ) . then ( ( ) => {
843+ throw new Error ( 'Expected mock error' )
844+ } ) ,
845+ } )
846+
847+ return (
848+ < div >
849+ < button onClick = { ( ) => mutate ( ) } > mutate</ button >
850+ < div > error: { error ?. message ?? 'null' } </ div >
851+ </ div >
852+ )
853+ }
854+
855+ const rendered = renderWithClient ( queryClient , < Page /> )
856+
857+ fireEvent . click ( rendered . getByRole ( 'button' , { name : / m u t a t e / i } ) )
858+ await vi . advanceTimersByTimeAsync ( 11 )
859+
860+ expect ( rendered . getByText ( 'error: Expected mock error' ) ) . toBeInTheDocument ( )
861+ } )
862+
786863 it ( 'should pass meta to mutation' , async ( ) => {
787864 const errorMock = vi . fn ( )
788865 const successMock = vi . fn ( )
0 commit comments