|
1 | | -import type { Event, EventType } from '@sentry/core'; |
2 | | -import { getGlobalScope } from '@sentry/core'; |
| 1 | +import type { Integration } from '@sentry/core'; |
3 | 2 | import type { NodeClient } from '@sentry/node'; |
4 | 3 | import * as SentryNode from '@sentry/node'; |
5 | 4 | import { SDK_VERSION } from '@sentry/node'; |
6 | | -import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
7 | | -import { init as reactRouterInit, lowQualityTransactionsFilter } from '../../src/server/sdk'; |
| 5 | +import { afterEach, describe, expect, it, vi } from 'vitest'; |
| 6 | +import * as LowQualityModule from '../../src/server/lowQualityTransactionsFilterIntegration'; |
| 7 | +import { init as reactRouterInit } from '../../src/server/sdk'; |
8 | 8 |
|
9 | 9 | const nodeInit = vi.spyOn(SentryNode, 'init'); |
10 | 10 |
|
@@ -48,91 +48,28 @@ describe('React Router server SDK', () => { |
48 | 48 | expect(client).not.toBeUndefined(); |
49 | 49 | }); |
50 | 50 |
|
51 | | - it('registers the low quality transactions filter', async () => { |
52 | | - const addEventProcessor = vi.spyOn(getGlobalScope(), 'addEventProcessor'); |
53 | | - addEventProcessor.mockClear(); |
| 51 | + it('adds the low quality transactions filter integration by default', () => { |
| 52 | + const filterSpy = vi.spyOn(LowQualityModule, 'lowQualityTransactionsFilterIntegration'); |
54 | 53 |
|
55 | 54 | reactRouterInit({ |
56 | 55 | dsn: 'https://public@dsn.ingest.sentry.io/1337', |
57 | | - }) as NodeClient; |
58 | | - |
59 | | - expect(addEventProcessor).toHaveBeenCalledTimes(1); |
60 | | - const processor = addEventProcessor.mock.calls[0]![0]; |
61 | | - expect(processor?.id).toEqual('ReactRouterLowQualityTransactionsFilter'); |
62 | | - }); |
63 | | - |
64 | | - describe('transaction filtering', () => { |
65 | | - const beforeSendEvent = vi.fn(event => event); |
66 | | - let client: NodeClient; |
67 | | - |
68 | | - beforeEach(() => { |
69 | | - vi.clearAllMocks(); |
70 | | - beforeSendEvent.mockClear(); |
71 | | - SentryNode.getGlobalScope().clear(); |
72 | | - |
73 | | - client = reactRouterInit({ |
74 | | - dsn: 'https://public@dsn.ingest.sentry.io/1337', |
75 | | - }) as NodeClient; |
76 | | - |
77 | | - client.on('beforeSendEvent', beforeSendEvent); |
78 | 56 | }); |
79 | 57 |
|
80 | | - describe('filters out low quality transactions', () => { |
81 | | - it.each(['GET /node_modules/react/index.js', 'GET /favicon.ico', 'GET /@id/package'])( |
82 | | - '%s', |
83 | | - async transaction => { |
84 | | - client.captureEvent({ type: 'transaction', transaction }); |
85 | | - |
86 | | - await client.flush(); |
87 | | - |
88 | | - expect(beforeSendEvent).not.toHaveBeenCalled(); |
89 | | - }, |
90 | | - ); |
91 | | - }); |
| 58 | + expect(filterSpy).toHaveBeenCalled(); |
92 | 59 |
|
93 | | - describe('allows high quality transactions', () => { |
94 | | - it.each(['GET /', 'GET /users', 'POST /api/data', 'GET /projects/123'])('%s', async transaction => { |
95 | | - client.captureEvent({ type: 'transaction', transaction }); |
96 | | - |
97 | | - await client.flush(); |
98 | | - |
99 | | - expect(beforeSendEvent).toHaveBeenCalledWith(expect.objectContaining({ transaction }), expect.any(Object)); |
100 | | - }); |
101 | | - }); |
102 | | - }); |
103 | | - }); |
| 60 | + expect(nodeInit).toHaveBeenCalledTimes(1); |
| 61 | + const initOptions = nodeInit.mock.calls[0]?.[0]; |
104 | 62 |
|
105 | | - describe('lowQualityTransactionsFilter', () => { |
106 | | - describe('filters out low quality transactions', () => { |
107 | | - it.each([ |
108 | | - ['node_modules request', 'GET /node_modules/react/index.js'], |
109 | | - ['favicon.ico request', 'GET /favicon.ico'], |
110 | | - ['@id request', 'GET /@id/package'], |
111 | | - ])('%s', (description, transaction) => { |
112 | | - const filter = lowQualityTransactionsFilter({}); |
113 | | - const event = { |
114 | | - type: 'transaction' as EventType, |
115 | | - transaction, |
116 | | - } as Event; |
| 63 | + expect(initOptions).toBeDefined(); |
117 | 64 |
|
118 | | - expect(filter(event, {})).toBeNull(); |
119 | | - }); |
120 | | - }); |
| 65 | + const defaultIntegrations = initOptions?.defaultIntegrations as Integration[]; |
| 66 | + expect(Array.isArray(defaultIntegrations)).toBe(true); |
121 | 67 |
|
122 | | - describe('does not filter good transactions', () => { |
123 | | - it.each([ |
124 | | - ['normal page request', 'GET /users'], |
125 | | - ['API request', 'POST /api/users'], |
126 | | - ['app route', 'GET /projects/123'], |
127 | | - ])('%s', (description, transaction) => { |
128 | | - const filter = lowQualityTransactionsFilter({}); |
129 | | - const event = { |
130 | | - type: 'transaction' as EventType, |
131 | | - transaction, |
132 | | - } as Event; |
| 68 | + const filterIntegration = defaultIntegrations.find( |
| 69 | + integration => integration.name === 'LowQualityTransactionsFilter' |
| 70 | + ); |
133 | 71 |
|
134 | | - expect(filter(event, {})).toBe(event); |
135 | | - }); |
| 72 | + expect(filterIntegration).toBeDefined(); |
136 | 73 | }); |
137 | 74 | }); |
138 | 75 | }); |
0 commit comments