@@ -3,6 +3,15 @@ import type { ActionFunctionArgs } from 'react-router';
33import { beforeEach , describe , expect , it , vi } from 'vitest' ;
44import { wrapServerAction } from '../../src/server/wrapServerAction' ;
55
6+ vi . mock ( '@sentry/core' , async ( ) => {
7+ const actual = await vi . importActual ( '@sentry/core' ) ;
8+ return {
9+ ...actual ,
10+ startSpan : vi . fn ( ) ,
11+ flushIfServerless : vi . fn ( ) ,
12+ } ;
13+ } ) ;
14+
615describe ( 'wrapServerAction' , ( ) => {
716 beforeEach ( ( ) => {
817 vi . clearAllMocks ( ) ;
@@ -12,11 +21,12 @@ describe('wrapServerAction', () => {
1221 const mockActionFn = vi . fn ( ) . mockResolvedValue ( 'result' ) ;
1322 const mockArgs = { request : new Request ( 'http://test.com' ) } as ActionFunctionArgs ;
1423
15- const spy = vi . spyOn ( core , 'startSpan' ) ;
24+ ( core . startSpan as any ) . mockImplementation ( ( _ : any , fn : any ) => fn ( ) ) ;
25+
1626 const wrappedAction = wrapServerAction ( { } , mockActionFn ) ;
1727 await wrappedAction ( mockArgs ) ;
1828
19- expect ( spy ) . toHaveBeenCalledWith (
29+ expect ( core . startSpan ) . toHaveBeenCalledWith (
2030 {
2131 name : 'Executing Server Action' ,
2232 attributes : {
@@ -27,6 +37,7 @@ describe('wrapServerAction', () => {
2737 expect . any ( Function ) ,
2838 ) ;
2939 expect ( mockActionFn ) . toHaveBeenCalledWith ( mockArgs ) ;
40+ expect ( core . flushIfServerless ) . toHaveBeenCalled ( ) ;
3041 } ) ;
3142
3243 it ( 'should wrap an action function with custom options' , async ( ) => {
@@ -40,11 +51,12 @@ describe('wrapServerAction', () => {
4051 const mockActionFn = vi . fn ( ) . mockResolvedValue ( 'result' ) ;
4152 const mockArgs = { request : new Request ( 'http://test.com' ) } as ActionFunctionArgs ;
4253
43- const spy = vi . spyOn ( core , 'startSpan' ) ;
54+ ( core . startSpan as any ) . mockImplementation ( ( _ : any , fn : any ) => fn ( ) ) ;
55+
4456 const wrappedAction = wrapServerAction ( customOptions , mockActionFn ) ;
4557 await wrappedAction ( mockArgs ) ;
4658
47- expect ( spy ) . toHaveBeenCalledWith (
59+ expect ( core . startSpan ) . toHaveBeenCalledWith (
4860 {
4961 name : 'Custom Action' ,
5062 attributes : {
@@ -56,5 +68,43 @@ describe('wrapServerAction', () => {
5668 expect . any ( Function ) ,
5769 ) ;
5870 expect ( mockActionFn ) . toHaveBeenCalledWith ( mockArgs ) ;
71+ expect ( core . flushIfServerless ) . toHaveBeenCalled ( ) ;
72+ } ) ;
73+
74+ it ( 'should call flushIfServerless on successful execution' , async ( ) => {
75+ const mockActionFn = vi . fn ( ) . mockResolvedValue ( 'result' ) ;
76+ const mockArgs = { request : new Request ( 'http://test.com' ) } as ActionFunctionArgs ;
77+
78+ ( core . startSpan as any ) . mockImplementation ( ( _ : any , fn : any ) => fn ( ) ) ;
79+
80+ const wrappedAction = wrapServerAction ( { } , mockActionFn ) ;
81+ await wrappedAction ( mockArgs ) ;
82+
83+ expect ( core . flushIfServerless ) . toHaveBeenCalled ( ) ;
84+ } ) ;
85+
86+ it ( 'should call flushIfServerless even when action throws an error' , async ( ) => {
87+ const mockError = new Error ( 'Action failed' ) ;
88+ const mockActionFn = vi . fn ( ) . mockRejectedValue ( mockError ) ;
89+ const mockArgs = { request : new Request ( 'http://test.com' ) } as ActionFunctionArgs ;
90+
91+ ( core . startSpan as any ) . mockImplementation ( ( _ : any , fn : any ) => fn ( ) ) ;
92+
93+ const wrappedAction = wrapServerAction ( { } , mockActionFn ) ;
94+
95+ await expect ( wrappedAction ( mockArgs ) ) . rejects . toThrow ( 'Action failed' ) ;
96+ expect ( core . flushIfServerless ) . toHaveBeenCalled ( ) ;
97+ } ) ;
98+
99+ it ( 'should propagate errors from action function' , async ( ) => {
100+ const mockError = new Error ( 'Test error' ) ;
101+ const mockActionFn = vi . fn ( ) . mockRejectedValue ( mockError ) ;
102+ const mockArgs = { request : new Request ( 'http://test.com' ) } as ActionFunctionArgs ;
103+
104+ ( core . startSpan as any ) . mockImplementation ( ( _ : any , fn : any ) => fn ( ) ) ;
105+
106+ const wrappedAction = wrapServerAction ( { } , mockActionFn ) ;
107+
108+ await expect ( wrappedAction ( mockArgs ) ) . rejects . toBe ( mockError ) ;
59109 } ) ;
60110} ) ;
0 commit comments