Skip to content

Commit f25664b

Browse files
authored
test(solidstart): Don't rely on flushing for lowQualityTransactionFilter (#17408)
The test relied on flush and if the network functionality of CI does not work 100%, this test produces a timeout and fails. Similar to this: #17406
1 parent 115c3e6 commit f25664b

File tree

2 files changed

+70
-47
lines changed

2 files changed

+70
-47
lines changed

packages/solidstart/src/server/utils.ts

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,25 +17,32 @@ export function isRedirect(error: unknown): boolean {
1717
return hasValidLocation && hasValidStatus;
1818
}
1919

20+
/**
21+
* Filter function for low quality transactions
22+
*
23+
* Exported only for tests
24+
*/
25+
export function lowQualityTransactionsFilter(options: Options): EventProcessor {
26+
return Object.assign(
27+
(event => {
28+
if (event.type !== 'transaction') {
29+
return event;
30+
}
31+
// Filter out transactions for build assets
32+
if (event.transaction?.match(/^GET \/_build\//)) {
33+
options.debug && debug.log('SolidStartLowQualityTransactionsFilter filtered transaction', event.transaction);
34+
return null;
35+
}
36+
return event;
37+
}) satisfies EventProcessor,
38+
{ id: 'SolidStartLowQualityTransactionsFilter' },
39+
);
40+
}
41+
2042
/**
2143
* Adds an event processor to filter out low quality transactions,
2244
* e.g. to filter out transactions for build assets
2345
*/
2446
export function filterLowQualityTransactions(options: Options): void {
25-
getGlobalScope().addEventProcessor(
26-
Object.assign(
27-
(event => {
28-
if (event.type !== 'transaction') {
29-
return event;
30-
}
31-
// Filter out transactions for build assets
32-
if (event.transaction?.match(/^GET \/_build\//)) {
33-
options.debug && debug.log('SolidStartLowQualityTransactionsFilter filtered transaction', event.transaction);
34-
return null;
35-
}
36-
return event;
37-
}) satisfies EventProcessor,
38-
{ id: 'SolidStartLowQualityTransactionsFilter' },
39-
),
40-
);
47+
getGlobalScope().addEventProcessor(lowQualityTransactionsFilter(options));
4148
}
Lines changed: 47 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import type { NodeClient } from '@sentry/node';
2-
import { SDK_VERSION } from '@sentry/node';
1+
import type { EventProcessor } from '@sentry/core';
2+
import { getGlobalScope, Scope, SDK_VERSION } from '@sentry/node';
33
import * as SentryNode from '@sentry/node';
44
import { beforeEach, describe, expect, it, vi } from 'vitest';
55
import { init as solidStartInit } from '../../src/server';
6+
import { lowQualityTransactionsFilter } from '../../src/server/utils';
67

78
const browserInit = vi.spyOn(SentryNode, 'init');
89

@@ -34,37 +35,52 @@ describe('Initialize Solid Start SDK', () => {
3435
expect(browserInit).toHaveBeenLastCalledWith(expect.objectContaining(expectedMetadata));
3536
});
3637

37-
it('filters out low quality transactions', async () => {
38-
const beforeSendEvent = vi.fn(event => event);
39-
const client = solidStartInit({
40-
dsn: 'https://[email protected]/1337',
41-
}) as NodeClient;
42-
client.on('beforeSendEvent', beforeSendEvent);
38+
describe('lowQualityTransactionsFilter', () => {
39+
const options = { debug: false };
40+
const filter = lowQualityTransactionsFilter(options);
41+
42+
describe('filters out low quality transactions', () => {
43+
it.each(['GET /_build/some_asset.js', 'GET /_build/app.js', 'GET /_build/assets/logo.png'])(
44+
'filters out low quality transaction: (%s)',
45+
transaction => {
46+
const event = { type: 'transaction' as const, transaction };
47+
expect(filter(event, {})).toBeNull();
48+
},
49+
);
50+
});
51+
52+
describe('keeps high quality transactions', () => {
53+
it.each(['GET /', 'POST /_server'])('does not filter out route transactions (%s)', transaction => {
54+
const event = { type: 'transaction' as const, transaction };
55+
expect(filter(event, {})).toEqual(event);
56+
});
57+
});
4358

44-
client.captureEvent({ type: 'transaction', transaction: 'GET /' });
45-
client.captureEvent({ type: 'transaction', transaction: 'GET /_build/some_asset.js' });
46-
client.captureEvent({ type: 'transaction', transaction: 'POST /_server' });
59+
it('does not filter non-transaction events', () => {
60+
const event = { type: 'error' as const, transaction: 'GET /_build/app.js' } as any;
61+
expect(filter(event, {})).toEqual(event);
62+
});
63+
64+
it('handles events without transaction property', () => {
65+
const event = { type: 'transaction' as const };
66+
expect(filter(event, {})).toEqual(event);
67+
});
68+
});
4769

48-
await client!.flush();
70+
it('registers an event processor', () => {
71+
let passedEventProcessors: EventProcessor[] = [];
72+
const addEventProcessor = vi
73+
.spyOn(getGlobalScope(), 'addEventProcessor')
74+
.mockImplementation((eventProcessor: EventProcessor) => {
75+
passedEventProcessors = [...passedEventProcessors, eventProcessor];
76+
return new Scope();
77+
});
78+
79+
solidStartInit({
80+
dsn: 'https://[email protected]/1337',
81+
});
4982

50-
expect(beforeSendEvent).toHaveBeenCalledTimes(2);
51-
expect(beforeSendEvent).toHaveBeenCalledWith(
52-
expect.objectContaining({
53-
transaction: 'GET /',
54-
}),
55-
expect.any(Object),
56-
);
57-
expect(beforeSendEvent).not.toHaveBeenCalledWith(
58-
expect.objectContaining({
59-
transaction: 'GET /_build/some_asset.js',
60-
}),
61-
expect.any(Object),
62-
);
63-
expect(beforeSendEvent).toHaveBeenCalledWith(
64-
expect.objectContaining({
65-
transaction: 'POST /_server',
66-
}),
67-
expect.any(Object),
68-
);
83+
expect(addEventProcessor).toHaveBeenCalledTimes(1);
84+
expect(passedEventProcessors[0]?.id).toEqual('SolidStartLowQualityTransactionsFilter');
6985
});
7086
});

0 commit comments

Comments
 (0)