Skip to content

Commit ef814f8

Browse files
committed
fix tests
1 parent c316241 commit ef814f8

File tree

6 files changed

+88
-69
lines changed

6 files changed

+88
-69
lines changed

packages/react-router/test/server/instrumentation/reactRouterServer.test.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { Span } from '@sentry/core';
1+
import type { Span, SpanJSON } from '@sentry/core';
22
import * as SentryCore from '@sentry/core';
33
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
44
import { ReactRouterInstrumentation } from '../../../src/server/instrumentation/reactRouter';
@@ -8,6 +8,8 @@ vi.mock('@sentry/core', async () => {
88
return {
99
getActiveSpan: vi.fn(),
1010
getRootSpan: vi.fn(),
11+
spanToJSON: vi.fn(),
12+
updateSpanName: vi.fn(),
1113
logger: {
1214
debug: vi.fn(),
1315
},
@@ -90,6 +92,7 @@ describe('ReactRouterInstrumentation', () => {
9092
vi.spyOn(Util, 'isDataRequest').mockReturnValue(true);
9193
vi.spyOn(SentryCore, 'getActiveSpan').mockReturnValue(mockSpan as Span);
9294
vi.spyOn(SentryCore, 'getRootSpan').mockReturnValue(mockSpan as Span);
95+
vi.spyOn(SentryCore, 'spanToJSON').mockReturnValue({ data: {} } as SpanJSON);
9396
vi.spyOn(Util, 'getSpanName').mockImplementation((pathname, method) => `span:${pathname}:${method}`);
9497
vi.spyOn(SentryCore, 'startSpan').mockImplementation((_opts, fn) => fn(mockSpan as Span));
9598

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import * as SentryNode from '@sentry/node';
2+
import { beforeEach, describe, expect, it, vi } from 'vitest';
3+
import { ReactRouterInstrumentation } from '../../../src/server/instrumentation/reactRouter';
4+
import { reactRouterServerIntegration } from '../../../src/server/integration/reactRouterServer';
5+
6+
vi.mock('../../../src/server/instrumentation/reactRouter', () => {
7+
return {
8+
ReactRouterInstrumentation: vi.fn(),
9+
};
10+
});
11+
12+
vi.mock('@sentry/node', () => {
13+
return {
14+
generateInstrumentOnce: vi.fn((_name: string, callback: () => any) => {
15+
return Object.assign(callback, { id: 'test' });
16+
}),
17+
NODE_VERSION: {
18+
major: 0,
19+
minor: 0,
20+
patch: 0,
21+
},
22+
};
23+
});
24+
25+
describe('reactRouterServerIntegration', () => {
26+
beforeEach(() => {
27+
vi.clearAllMocks();
28+
});
29+
30+
it('sets up ReactRouterInstrumentation for Node 20.18', () => {
31+
vi.spyOn(SentryNode, 'NODE_VERSION', 'get').mockReturnValue({ major: 20, minor: 18, patch: 0 });
32+
33+
const integration = reactRouterServerIntegration();
34+
integration.setupOnce!();
35+
36+
expect(ReactRouterInstrumentation).toHaveBeenCalled();
37+
});
38+
39+
it('sets up ReactRouterInstrumentationfor Node.js 22.11', () => {
40+
vi.spyOn(SentryNode, 'NODE_VERSION', 'get').mockReturnValue({ major: 22, minor: 11, patch: 0 });
41+
42+
const integration = reactRouterServerIntegration();
43+
integration.setupOnce!();
44+
45+
expect(ReactRouterInstrumentation).toHaveBeenCalled();
46+
});
47+
48+
it('does not set up ReactRouterInstrumentation for Node.js 20.19', () => {
49+
vi.spyOn(SentryNode, 'NODE_VERSION', 'get').mockReturnValue({ major: 20, minor: 19, patch: 0 });
50+
51+
const integration = reactRouterServerIntegration();
52+
integration.setupOnce!();
53+
54+
expect(ReactRouterInstrumentation).not.toHaveBeenCalled();
55+
});
56+
57+
it('does not set up ReactRouterInstrumentation for Node.js 22.12', () => {
58+
vi.spyOn(SentryNode, 'NODE_VERSION', 'get').mockReturnValue({ major: 22, minor: 12, patch: 0 });
59+
60+
const integration = reactRouterServerIntegration();
61+
integration.setupOnce!();
62+
63+
expect(ReactRouterInstrumentation).not.toHaveBeenCalled();
64+
});
65+
});

packages/react-router/test/server/sdk.test.ts

Lines changed: 1 addition & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -72,27 +72,7 @@ describe('React Router server SDK', () => {
7272
expect(filterIntegration).toBeDefined();
7373
});
7474

75-
it('adds reactRouterServer integration for Node.js 20.18', () => {
76-
vi.spyOn(SentryNode, 'NODE_VERSION', 'get').mockReturnValue({ major: 20, minor: 18, patch: 0 });
77-
78-
reactRouterInit({
79-
dsn: 'https://[email protected]/1337',
80-
});
81-
82-
expect(nodeInit).toHaveBeenCalledTimes(1);
83-
const initOptions = nodeInit.mock.calls[0]?.[0];
84-
const defaultIntegrations = initOptions?.defaultIntegrations as Integration[];
85-
86-
const reactRouterServerIntegration = defaultIntegrations.find(
87-
integration => integration.name === 'ReactRouterServer',
88-
);
89-
90-
expect(reactRouterServerIntegration).toBeDefined();
91-
});
92-
93-
it('adds reactRouterServer integration for Node.js 22.11', () => {
94-
vi.spyOn(SentryNode, 'NODE_VERSION', 'get').mockReturnValue({ major: 22, minor: 11, patch: 0 });
95-
75+
it('adds reactRouterServer integration by default', () => {
9676
reactRouterInit({
9777
dsn: 'https://[email protected]/1337',
9878
});
@@ -107,41 +87,5 @@ describe('React Router server SDK', () => {
10787

10888
expect(reactRouterServerIntegration).toBeDefined();
10989
});
110-
111-
it('does not add reactRouterServer integration for Node.js 20.19', () => {
112-
vi.spyOn(SentryNode, 'NODE_VERSION', 'get').mockReturnValue({ major: 20, minor: 19, patch: 0 });
113-
114-
reactRouterInit({
115-
dsn: 'https://[email protected]/1337',
116-
});
117-
118-
expect(nodeInit).toHaveBeenCalledTimes(1);
119-
const initOptions = nodeInit.mock.calls[0]?.[0];
120-
const defaultIntegrations = initOptions?.defaultIntegrations as Integration[];
121-
122-
const reactRouterServerIntegration = defaultIntegrations.find(
123-
integration => integration.name === 'ReactRouterServer',
124-
);
125-
126-
expect(reactRouterServerIntegration).toBeUndefined();
127-
});
128-
129-
it('does not add reactRouterServer integration for Node.js 22.12', () => {
130-
vi.spyOn(SentryNode, 'NODE_VERSION', 'get').mockReturnValue({ major: 22, minor: 12, patch: 0 });
131-
132-
reactRouterInit({
133-
dsn: 'https://[email protected]/1337',
134-
});
135-
136-
expect(nodeInit).toHaveBeenCalledTimes(1);
137-
const initOptions = nodeInit.mock.calls[0]?.[0];
138-
const defaultIntegrations = initOptions?.defaultIntegrations as Integration[];
139-
140-
const reactRouterServerIntegration = defaultIntegrations.find(
141-
integration => integration.name === 'ReactRouterServer',
142-
);
143-
144-
expect(reactRouterServerIntegration).toBeUndefined();
145-
});
14690
});
14791
});

packages/react-router/test/server/wrapSentryHandleRequest.test.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ vi.mock('@opentelemetry/core', () => ({
1818

1919
vi.mock('@sentry/core', () => ({
2020
SEMANTIC_ATTRIBUTE_SENTRY_SOURCE: 'sentry.source',
21-
SEMANTIC_ATTRIBUTE_SENTRY_CUSTOM_SPAN_NAME: 'sentry.custom-span-name',
2221
SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN: 'sentry.origin',
2322
getActiveSpan: vi.fn(),
2423
getRootSpan: vi.fn(),
@@ -56,7 +55,7 @@ describe('wrapSentryHandleRequest', () => {
5655
const originalHandler = vi.fn().mockResolvedValue('test');
5756
const wrappedHandler = wrapSentryHandleRequest(originalHandler);
5857

59-
const mockActiveSpan = { setAttribute: vi.fn() };
58+
const mockActiveSpan = {};
6059
const mockRootSpan = { setAttributes: vi.fn() };
6160
const mockRpcMetadata = { type: RPCType.HTTP, route: '/some-path' };
6261

@@ -73,8 +72,6 @@ describe('wrapSentryHandleRequest', () => {
7372

7473
await wrappedHandler(new Request('https://nacho.queso'), 200, new Headers(), routerContext, {} as any);
7574

76-
expect(getActiveSpan).toHaveBeenCalled();
77-
expect(getRootSpan).toHaveBeenCalledWith(mockActiveSpan);
7875
expect(mockRootSpan.setAttributes).toHaveBeenCalledWith({
7976
[ATTR_HTTP_ROUTE]: '/some-path',
8077
[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'route',
@@ -84,6 +81,12 @@ describe('wrapSentryHandleRequest', () => {
8481
});
8582

8683
test('should not set span attributes when parameterized path does not exist', async () => {
84+
const mockActiveSpan = {};
85+
const mockRootSpan = { setAttributes: vi.fn() };
86+
87+
(getActiveSpan as unknown as ReturnType<typeof vi.fn>).mockReturnValue(mockActiveSpan);
88+
(getRootSpan as unknown as ReturnType<typeof vi.fn>).mockReturnValue(mockRootSpan);
89+
8790
const originalHandler = vi.fn().mockResolvedValue('test');
8891
const wrappedHandler = wrapSentryHandleRequest(originalHandler);
8992

@@ -95,15 +98,20 @@ describe('wrapSentryHandleRequest', () => {
9598

9699
await wrappedHandler(new Request('https://guapo.chulo'), 200, new Headers(), routerContext, {} as any);
97100

98-
expect(getActiveSpan).not.toHaveBeenCalled();
101+
expect(mockRootSpan.setAttributes).not.toHaveBeenCalled();
99102
});
100103

101104
test('should not set span attributes when active span does not exist', async () => {
102105
const originalHandler = vi.fn().mockResolvedValue('test');
103106
const wrappedHandler = wrapSentryHandleRequest(originalHandler);
104107

108+
const mockRpcMetadata = { type: RPCType.HTTP, route: '/some-path' };
109+
105110
(getActiveSpan as unknown as ReturnType<typeof vi.fn>).mockReturnValue(null);
106111

112+
const getRPCMetadata = vi.fn().mockReturnValue(mockRpcMetadata);
113+
vi.mocked(vi.importActual('@opentelemetry/core')).getRPCMetadata = getRPCMetadata;
114+
107115
const routerContext = {
108116
staticHandlerContext: {
109117
matches: [{ route: { path: 'some-path' } }],
@@ -112,8 +120,7 @@ describe('wrapSentryHandleRequest', () => {
112120

113121
await wrappedHandler(new Request('https://tio.pepe'), 200, new Headers(), routerContext, {} as any);
114122

115-
expect(getActiveSpan).toHaveBeenCalled();
116-
expect(getRootSpan).not.toHaveBeenCalled();
123+
expect(getRPCMetadata).not.toHaveBeenCalled();
117124
});
118125
});
119126

packages/react-router/test/server/wrapServerAction.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ describe('wrapServerAction', () => {
2020
{
2121
name: 'Executing Server Action',
2222
attributes: {
23-
[core.SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.http.react-router',
23+
[core.SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.http.react-router.action',
2424
[core.SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'function.react-router.action',
2525
},
2626
},
@@ -48,7 +48,7 @@ describe('wrapServerAction', () => {
4848
{
4949
name: 'Custom Action',
5050
attributes: {
51-
[core.SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.http.react-router',
51+
[core.SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.http.react-router.action',
5252
[core.SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'function.react-router.action',
5353
'sentry.custom': 'value',
5454
},

packages/react-router/test/server/wrapServerLoader.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ describe('wrapServerLoader', () => {
2020
{
2121
name: 'Executing Server Loader',
2222
attributes: {
23-
[core.SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.http.react-router',
23+
[core.SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.http.react-router.loader',
2424
[core.SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'function.react-router.loader',
2525
},
2626
},
@@ -48,7 +48,7 @@ describe('wrapServerLoader', () => {
4848
{
4949
name: 'Custom Loader',
5050
attributes: {
51-
[core.SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.http.react-router',
51+
[core.SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.http.react-router.loader',
5252
[core.SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'function.react-router.loader',
5353
'sentry.custom': 'value',
5454
},

0 commit comments

Comments
 (0)