Skip to content

Commit 438a920

Browse files
committed
fix: Refresh and update all relevant tests
1 parent c64bd36 commit 438a920

File tree

16 files changed

+275
-349
lines changed

16 files changed

+275
-349
lines changed

packages/browser/test/unit/index.test.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ describe('SentryBrowser initialization', () => {
190190
init({ dsn });
191191

192192
// eslint-disable-next-line @typescript-eslint/no-explicit-any
193-
const sdkData = (getCurrentHub().getClient() as any)._backend._transport._api.metadata?.sdk;
193+
const sdkData = (getCurrentHub().getClient() as any)._backend._transport._api.sdkInfo;
194194

195195
expect(sdkData.name).to.equal('sentry.javascript.browser');
196196
expect(sdkData.packages[0].name).to.equal('npm:@sentry/browser');
@@ -202,7 +202,7 @@ describe('SentryBrowser initialization', () => {
202202
const client = new BrowserClient({ dsn });
203203

204204
// eslint-disable-next-line @typescript-eslint/no-explicit-any
205-
const sdkData = (client as any)._backend._transport._api.metadata?.sdk;
205+
const sdkData = (client as any)._backend._transport._api.sdkInfo;
206206

207207
expect(sdkData.name).to.equal('sentry.javascript.browser');
208208
expect(sdkData.packages[0].name).to.equal('npm:@sentry/browser');
@@ -216,7 +216,7 @@ describe('SentryBrowser initialization', () => {
216216
init({
217217
dsn,
218218
// this would normally be set by the wrapper SDK in init()
219-
_metadata: {
219+
_internal: {
220220
sdk: {
221221
name: 'sentry.javascript.angular',
222222
packages: [
@@ -231,7 +231,7 @@ describe('SentryBrowser initialization', () => {
231231
});
232232

233233
// eslint-disable-next-line @typescript-eslint/no-explicit-any
234-
const sdkData = (getCurrentHub().getClient() as any)._backend._transport._api.metadata?.sdk;
234+
const sdkData = (getCurrentHub().getClient() as any)._backend._transport._api.sdkInfo;
235235

236236
expect(sdkData.name).to.equal('sentry.javascript.angular');
237237
expect(sdkData.packages[0].name).to.equal('npm:@sentry/angular');

packages/core/src/baseclient.ts

+9
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,15 @@ export abstract class BaseClient<B extends Backend, O extends Options> implement
8787
protected constructor(backendClass: BackendClass<B, O>, options: O) {
8888
this._backend = new backendClass(options);
8989
this._options = options;
90+
this._options._internal = this._options._internal || {};
91+
92+
// Both `defaultIntegrations` and `discoverIntegrations` should be `boolean`, but we are stuck with this type
93+
// for backwards compatibility at the momement.
94+
if (options.defaultIntegrations !== false) {
95+
this._options._internal.defaultIntegrations = Array.isArray(this._options.defaultIntegrations)
96+
? this._options.defaultIntegrations
97+
: [];
98+
}
9099

91100
if (options.dsn) {
92101
this._dsn = new Dsn(options.dsn);

packages/core/src/integration.ts

+29-14
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,37 @@ export interface IntegrationIndex {
1010
}
1111

1212
/** Gets integration to install */
13-
export function getIntegrationsToSetup(options: Options): Integration[] {
14-
const defaultIntegrations = options._internal?.defaultIntegrations || [];
15-
const discoveredIntegrations = options._internal?.discoveredIntegrations || [];
16-
const userIntegrations = options.integrations || [];
13+
export function getIntegrationsToSetup(integrations: {
14+
defaultIntegrations?: Integration[];
15+
discoveredIntegrations?: Integration[];
16+
userIntegrations?: Integration[] | ((integrations: Integration[]) => Integration[]);
17+
}): Integration[] {
18+
const { discoveredIntegrations = [], userIntegrations = [] } = integrations;
19+
let { defaultIntegrations = [] } = integrations;
20+
21+
// And filter out duplicated default integrations
22+
defaultIntegrations = defaultIntegrations.reduce((acc, defaultIntegration) => {
23+
if (acc.every(accIntegration => defaultIntegration.name !== accIntegration.name)) {
24+
acc.push(defaultIntegration);
25+
}
26+
return acc;
27+
}, [] as Integration[]);
1728

1829
// Filter out default integrations that are also discovered
19-
let integrations: Integration[] = [
30+
let processedIntegrations: Integration[] = [
2031
...defaultIntegrations.filter(defaultIntegration =>
2132
discoveredIntegrations.every(discoveredIntegration => discoveredIntegration.name !== defaultIntegration.name),
2233
),
2334
...discoveredIntegrations,
2435
];
2536

2637
if (Array.isArray(userIntegrations)) {
27-
// Filter out integrations that are also included in user options
28-
integrations = [
29-
...integrations.filter(integrations =>
30-
userIntegrations.every(userIntegration => userIntegration.name !== integrations.name),
38+
// Filter out integrations that are also included in user integrations
39+
processedIntegrations = [
40+
...processedIntegrations.filter(integrations =>
41+
(userIntegrations as Integration[]).every(userIntegration => userIntegration.name !== integrations.name),
3142
),
32-
// And filter out duplicated user options integrations
43+
// And filter out duplicated user integrations
3344
...userIntegrations.reduce((acc, userIntegration) => {
3445
if (acc.every(accIntegration => userIntegration.name !== accIntegration.name)) {
3546
acc.push(userIntegration);
@@ -38,11 +49,11 @@ export function getIntegrationsToSetup(options: Options): Integration[] {
3849
}, [] as Integration[]),
3950
];
4051
} else if (typeof userIntegrations === 'function') {
41-
integrations = userIntegrations(integrations);
42-
integrations = Array.isArray(integrations) ? integrations : [integrations];
52+
processedIntegrations = userIntegrations(processedIntegrations);
53+
processedIntegrations = Array.isArray(processedIntegrations) ? processedIntegrations : [processedIntegrations];
4354
}
4455

45-
return integrations;
56+
return processedIntegrations;
4657
}
4758

4859
/** Setup given integration */
@@ -63,7 +74,11 @@ export function setupIntegration(integration: Integration): void {
6374
*/
6475
export function setupIntegrations(options: Options): IntegrationIndex {
6576
const integrations: IntegrationIndex = {};
66-
getIntegrationsToSetup(options).forEach(integration => {
77+
getIntegrationsToSetup({
78+
defaultIntegrations: options._internal?.defaultIntegrations || [],
79+
discoveredIntegrations: options._internal?.discoveredIntegrations || [],
80+
userIntegrations: options.integrations || [],
81+
}).forEach(integration => {
6782
integrations[integration.name] = integration;
6883
setupIntegration(integration);
6984
});

packages/core/src/request.ts

+8-14
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,6 @@ import { Event, SdkInfo, SentryRequest, Session } from '@sentry/types';
22

33
import { API } from './api';
44

5-
/** Extract sdk info from from the API metadata */
6-
function getSdkInfoForEnvelopeHeader(api: API): SdkInfo | undefined {
7-
if (!api.sdkInfo) {
8-
return;
9-
}
10-
const { name, version } = api.sdkInfo;
11-
return { name, version };
12-
}
13-
145
/**
156
* Apply SdkInfo (name, version, packages, integrations) to the corresponding event key.
167
* Merge with existing data if any.
@@ -29,10 +20,9 @@ function enhanceEventWithSdkInfo(event: Event, sdkInfo?: SdkInfo): Event {
2920

3021
/** Creates a SentryRequest from a Session. */
3122
export function sessionToSentryRequest(session: Session, api: API): SentryRequest {
32-
const sdkInfo = getSdkInfoForEnvelopeHeader(api);
3323
const envelopeHeaders = JSON.stringify({
3424
sent_at: new Date().toISOString(),
35-
...(sdkInfo && { sdk: sdkInfo }),
25+
...(api.sdkInfo && { sdk: api.sdkInfo }),
3626
});
3727
const itemHeaders = JSON.stringify({
3828
type: 'session',
@@ -47,7 +37,6 @@ export function sessionToSentryRequest(session: Session, api: API): SentryReques
4737

4838
/** Creates a SentryRequest from an event. */
4939
export function eventToSentryRequest(event: Event, api: API): SentryRequest {
50-
const sdkInfo = getSdkInfoForEnvelopeHeader(api);
5140
const eventType = event.type || 'event';
5241
const useEnvelope = eventType === 'transaction';
5342

@@ -60,7 +49,7 @@ export function eventToSentryRequest(event: Event, api: API): SentryRequest {
6049
}
6150

6251
const req: SentryRequest = {
63-
body: JSON.stringify(sdkInfo ? enhanceEventWithSdkInfo(event, sdkInfo) : event),
52+
body: JSON.stringify(api.sdkInfo ? enhanceEventWithSdkInfo(event, api.sdkInfo) : event),
6453
type: eventType,
6554
url: useEnvelope ? api.getEnvelopeEndpointWithUrlEncodedAuth() : api.getStoreEndpointWithUrlEncodedAuth(),
6655
};
@@ -75,7 +64,12 @@ export function eventToSentryRequest(event: Event, api: API): SentryRequest {
7564
const envelopeHeaders = JSON.stringify({
7665
event_id: event.event_id,
7766
sent_at: new Date().toISOString(),
78-
...(sdkInfo && { sdk: sdkInfo }),
67+
...(api.sdkInfo && {
68+
sdk: {
69+
name: api.sdkInfo.name,
70+
version: api.sdkInfo.version,
71+
},
72+
}),
7973
});
8074
const itemHeaders = JSON.stringify({
8175
type: event.type,

packages/core/test/lib/integration.test.ts

+8-31
Original file line numberDiff line numberDiff line change
@@ -18,31 +18,31 @@ class MockIntegration implements Integration {
1818
describe('getIntegrationsToSetup', () => {
1919
it('works with empty array', () => {
2020
const integrations = getIntegrationsToSetup({
21-
integrations: [],
21+
userIntegrations: [],
2222
});
2323

2424
expect(integrations.map(i => i.name)).toEqual([]);
2525
});
2626

2727
it('works with single item', () => {
2828
const integrations = getIntegrationsToSetup({
29-
integrations: [new MockIntegration('foo')],
29+
userIntegrations: [new MockIntegration('foo')],
3030
});
3131

3232
expect(integrations.map(i => i.name)).toEqual(['foo']);
3333
});
3434

3535
it('works with multiple items', () => {
3636
const integrations = getIntegrationsToSetup({
37-
integrations: [new MockIntegration('foo'), new MockIntegration('bar')],
37+
userIntegrations: [new MockIntegration('foo'), new MockIntegration('bar')],
3838
});
3939

4040
expect(integrations.map(i => i.name)).toEqual(['foo', 'bar']);
4141
});
4242

4343
it('filter duplicated items', () => {
4444
const integrations = getIntegrationsToSetup({
45-
integrations: [new MockIntegration('foo'), new MockIntegration('foo'), new MockIntegration('bar')],
45+
userIntegrations: [new MockIntegration('foo'), new MockIntegration('foo'), new MockIntegration('bar')],
4646
});
4747

4848
expect(integrations.map(i => i.name)).toEqual(['foo', 'bar']);
@@ -55,7 +55,7 @@ describe('getIntegrationsToSetup', () => {
5555
(second as any).order = 'second';
5656

5757
const integrations = getIntegrationsToSetup({
58-
integrations: [first, second, new MockIntegration('bar')],
58+
userIntegrations: [first, second, new MockIntegration('bar')],
5959
});
6060

6161
expect(integrations.map(i => i.name)).toEqual(['foo', 'bar']);
@@ -89,7 +89,7 @@ describe('getIntegrationsToSetup', () => {
8989
it('work with user integrations and defaults and pick defaults first', () => {
9090
const integrations = getIntegrationsToSetup({
9191
defaultIntegrations: [new MockIntegration('foo')],
92-
integrations: [new MockIntegration('bar')],
92+
userIntegrations: [new MockIntegration('bar')],
9393
});
9494

9595
expect(integrations.map(i => i.name)).toEqual(['foo', 'bar']);
@@ -98,7 +98,7 @@ describe('getIntegrationsToSetup', () => {
9898
it('work with user integrations and defaults and filter duplicates', () => {
9999
const integrations = getIntegrationsToSetup({
100100
defaultIntegrations: [new MockIntegration('foo'), new MockIntegration('foo')],
101-
integrations: [new MockIntegration('bar'), new MockIntegration('bar')],
101+
userIntegrations: [new MockIntegration('bar'), new MockIntegration('bar')],
102102
});
103103

104104
expect(integrations.map(i => i.name)).toEqual(['foo', 'bar']);
@@ -116,34 +116,11 @@ describe('getIntegrationsToSetup', () => {
116116

117117
const integrations = getIntegrationsToSetup({
118118
defaultIntegrations: [firstDefault, secondDefault],
119-
integrations: [firstUser, secondUser],
119+
userIntegrations: [firstUser, secondUser],
120120
});
121121

122122
expect(integrations.map(i => i.name)).toEqual(['foo', 'bar']);
123123
expect((integrations[0] as any).order).toEqual('firstUser');
124124
expect((integrations[1] as any).order).toEqual('secondUser');
125125
});
126-
127-
it('always moves Debug integration to the end of the list', () => {
128-
let integrations = getIntegrationsToSetup({
129-
defaultIntegrations: [new MockIntegration('Debug'), new MockIntegration('foo')],
130-
integrations: [new MockIntegration('bar')],
131-
});
132-
133-
expect(integrations.map(i => i.name)).toEqual(['foo', 'bar', 'Debug']);
134-
135-
integrations = getIntegrationsToSetup({
136-
defaultIntegrations: [new MockIntegration('foo')],
137-
integrations: [new MockIntegration('Debug'), new MockIntegration('bar')],
138-
});
139-
140-
expect(integrations.map(i => i.name)).toEqual(['foo', 'bar', 'Debug']);
141-
142-
integrations = getIntegrationsToSetup({
143-
defaultIntegrations: [new MockIntegration('Debug')],
144-
integrations: [new MockIntegration('foo')],
145-
});
146-
147-
expect(integrations.map(i => i.name)).toEqual(['foo', 'Debug']);
148-
});
149126
});

packages/core/test/lib/request.test.ts

+4-6
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,10 @@ describe('eventToSentryRequest', () => {
1515
}
1616

1717
const api = new API('https://[email protected]/12312012', {
18-
sdk: {
19-
integrations: ['AWSLambda'],
20-
name: 'sentry.javascript.browser',
21-
version: `12.31.12`,
22-
packages: [{ name: 'npm:@sentry/browser', version: `12.31.12` }],
23-
},
18+
integrations: ['AWSLambda'],
19+
name: 'sentry.javascript.browser',
20+
version: `12.31.12`,
21+
packages: [{ name: 'npm:@sentry/browser', version: `12.31.12` }],
2422
});
2523
let event: Event;
2624

packages/gatsby/test/gatsby-browser.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ describe('onClientEntry', () => {
4747
integrations: [],
4848
release: (global as any).__SENTRY_RELEASE__,
4949
autoSessionTracking: true,
50-
_metadata: {
50+
_internal: {
5151
sdk: {
5252
name: 'sentry.javascript.gatsby',
5353
packages: [

0 commit comments

Comments
 (0)