-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathwrapServerAction.test.ts
More file actions
110 lines (87 loc) · 3.8 KB
/
wrapServerAction.test.ts
File metadata and controls
110 lines (87 loc) · 3.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import * as core from '@sentry/core';
import type { ActionFunctionArgs } from 'react-router';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { wrapServerAction } from '../../src/server/wrapServerAction';
vi.mock('@sentry/core', async () => {
const actual = await vi.importActual('@sentry/core');
return {
...actual,
startSpan: vi.fn(),
flushIfServerless: vi.fn(),
};
});
describe('wrapServerAction', () => {
beforeEach(() => {
vi.clearAllMocks();
});
it('should wrap an action function with default options', async () => {
const mockActionFn = vi.fn().mockResolvedValue('result');
const mockArgs = { request: new Request('http://test.com') } as ActionFunctionArgs;
(core.startSpan as any).mockImplementation((_: any, fn: any) => fn());
const wrappedAction = wrapServerAction({}, mockActionFn);
await wrappedAction(mockArgs);
expect(core.startSpan).toHaveBeenCalledWith(
{
name: 'Executing Server Action',
attributes: {
[core.SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.http.react_router.action',
[core.SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'function.react_router.action',
},
},
expect.any(Function),
);
expect(mockActionFn).toHaveBeenCalledWith(mockArgs);
expect(core.flushIfServerless).toHaveBeenCalled();
});
it('should wrap an action function with custom options', async () => {
const customOptions = {
name: 'Custom Action',
attributes: {
'sentry.custom': 'value',
},
};
const mockActionFn = vi.fn().mockResolvedValue('result');
const mockArgs = { request: new Request('http://test.com') } as ActionFunctionArgs;
(core.startSpan as any).mockImplementation((_: any, fn: any) => fn());
const wrappedAction = wrapServerAction(customOptions, mockActionFn);
await wrappedAction(mockArgs);
expect(core.startSpan).toHaveBeenCalledWith(
{
name: 'Custom Action',
attributes: {
[core.SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.http.react_router.action',
[core.SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'function.react_router.action',
'sentry.custom': 'value',
},
},
expect.any(Function),
);
expect(mockActionFn).toHaveBeenCalledWith(mockArgs);
expect(core.flushIfServerless).toHaveBeenCalled();
});
it('should call flushIfServerless on successful execution', async () => {
const mockActionFn = vi.fn().mockResolvedValue('result');
const mockArgs = { request: new Request('http://test.com') } as ActionFunctionArgs;
(core.startSpan as any).mockImplementation((_: any, fn: any) => fn());
const wrappedAction = wrapServerAction({}, mockActionFn);
await wrappedAction(mockArgs);
expect(core.flushIfServerless).toHaveBeenCalled();
});
it('should call flushIfServerless even when action throws an error', async () => {
const mockError = new Error('Action failed');
const mockActionFn = vi.fn().mockRejectedValue(mockError);
const mockArgs = { request: new Request('http://test.com') } as ActionFunctionArgs;
(core.startSpan as any).mockImplementation((_: any, fn: any) => fn());
const wrappedAction = wrapServerAction({}, mockActionFn);
await expect(wrappedAction(mockArgs)).rejects.toThrow('Action failed');
expect(core.flushIfServerless).toHaveBeenCalled();
});
it('should propagate errors from action function', async () => {
const mockError = new Error('Test error');
const mockActionFn = vi.fn().mockRejectedValue(mockError);
const mockArgs = { request: new Request('http://test.com') } as ActionFunctionArgs;
(core.startSpan as any).mockImplementation((_: any, fn: any) => fn());
const wrappedAction = wrapServerAction({}, mockActionFn);
await expect(wrappedAction(mockArgs)).rejects.toBe(mockError);
});
});