Skip to content

Commit 07b1a96

Browse files
committed
fix(node): Ensure node options are correctly used in init
1 parent ecc95e7 commit 07b1a96

File tree

3 files changed

+128
-87
lines changed

3 files changed

+128
-87
lines changed

packages/node/src/sdk/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,12 +113,12 @@ export function initWithoutDefaultIntegrations(options: NodeOptions | undefined
113113
* Initialize Sentry for Node, without performance instrumentation.
114114
*/
115115
function _init(
116-
options: NodeOptions | undefined = {},
116+
_options: NodeOptions | undefined = {},
117117
getDefaultIntegrationsImpl: (options: Options) => Integration[],
118118
): NodeClient {
119-
const clientOptions = getClientOptions(options, getDefaultIntegrationsImpl);
119+
const options = getClientOptions(_options, getDefaultIntegrationsImpl);
120120

121-
if (clientOptions.debug === true) {
121+
if (options.debug === true) {
122122
if (DEBUG_BUILD) {
123123
logger.enable();
124124
} else {
@@ -139,7 +139,7 @@ function _init(
139139
const scope = getCurrentScope();
140140
scope.update(options.initialScope);
141141

142-
const client = new NodeClient(clientOptions);
142+
const client = new NodeClient(options);
143143
// The client is on the current scope, from where it generally is inherited
144144
getCurrentScope().setClient(client);
145145

packages/node/test/integrations/express.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ describe('expressErrorHandler()', () => {
7474
});
7575

7676
it('autoSessionTracking is enabled + requestHandler is not used -> does not set requestSession status on Crash', done => {
77-
const options = getDefaultNodeClientOptions({ autoSessionTracking: false, release: '3.3' });
77+
const options = getDefaultNodeClientOptions({ autoSessionTracking: true, release: '3.3' });
7878
client = new NodeClient(options);
7979
setCurrentClient(client);
8080

packages/node/test/sdk/init.test.ts

Lines changed: 123 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { Integration } from '@sentry/types';
22

3-
import { getClient } from '../../src/';
3+
import { getClient, getIsolationScope } from '../../src/';
44
import * as auto from '../../src/integrations/tracing';
55
import { init } from '../../src/sdk';
66
import { NodeClient } from '../../src/sdk/client';
@@ -34,112 +34,153 @@ describe('init()', () => {
3434
jest.clearAllMocks();
3535
});
3636

37-
it("doesn't install default integrations if told not to", () => {
38-
init({ dsn: PUBLIC_DSN, defaultIntegrations: false });
37+
describe('integrations', () => {
38+
it("doesn't install default integrations if told not to", () => {
39+
init({ dsn: PUBLIC_DSN, defaultIntegrations: false });
3940

40-
const client = getClient();
41+
const client = getClient();
4142

42-
expect(client?.getOptions()).toEqual(
43-
expect.objectContaining({
44-
integrations: [],
45-
}),
46-
);
43+
expect(client?.getOptions()).toEqual(
44+
expect.objectContaining({
45+
integrations: [],
46+
}),
47+
);
4748

48-
expect(mockAutoPerformanceIntegrations).toHaveBeenCalledTimes(0);
49-
});
49+
expect(mockAutoPerformanceIntegrations).toHaveBeenCalledTimes(0);
50+
});
5051

51-
it('installs merged default integrations, with overrides provided through options', () => {
52-
const mockDefaultIntegrations = [
53-
new MockIntegration('Some mock integration 2.1'),
54-
new MockIntegration('Some mock integration 2.2'),
55-
];
52+
it('installs merged default integrations, with overrides provided through options', () => {
53+
const mockDefaultIntegrations = [
54+
new MockIntegration('Some mock integration 2.1'),
55+
new MockIntegration('Some mock integration 2.2'),
56+
];
5657

57-
const mockIntegrations = [
58-
new MockIntegration('Some mock integration 2.1'),
59-
new MockIntegration('Some mock integration 2.3'),
60-
];
58+
const mockIntegrations = [
59+
new MockIntegration('Some mock integration 2.1'),
60+
new MockIntegration('Some mock integration 2.3'),
61+
];
6162

62-
init({ dsn: PUBLIC_DSN, integrations: mockIntegrations, defaultIntegrations: mockDefaultIntegrations });
63+
init({ dsn: PUBLIC_DSN, integrations: mockIntegrations, defaultIntegrations: mockDefaultIntegrations });
6364

64-
expect(mockDefaultIntegrations[0]?.setupOnce as jest.Mock).toHaveBeenCalledTimes(0);
65-
expect(mockDefaultIntegrations[1]?.setupOnce as jest.Mock).toHaveBeenCalledTimes(1);
66-
expect(mockIntegrations[0]?.setupOnce as jest.Mock).toHaveBeenCalledTimes(1);
67-
expect(mockIntegrations[1]?.setupOnce as jest.Mock).toHaveBeenCalledTimes(1);
68-
expect(mockAutoPerformanceIntegrations).toHaveBeenCalledTimes(0);
69-
});
65+
expect(mockDefaultIntegrations[0]?.setupOnce as jest.Mock).toHaveBeenCalledTimes(0);
66+
expect(mockDefaultIntegrations[1]?.setupOnce as jest.Mock).toHaveBeenCalledTimes(1);
67+
expect(mockIntegrations[0]?.setupOnce as jest.Mock).toHaveBeenCalledTimes(1);
68+
expect(mockIntegrations[1]?.setupOnce as jest.Mock).toHaveBeenCalledTimes(1);
69+
expect(mockAutoPerformanceIntegrations).toHaveBeenCalledTimes(0);
70+
});
7071

71-
it('installs integrations returned from a callback function', () => {
72-
const mockDefaultIntegrations = [
73-
new MockIntegration('Some mock integration 3.1'),
74-
new MockIntegration('Some mock integration 3.2'),
75-
];
76-
77-
const newIntegration = new MockIntegration('Some mock integration 3.3');
78-
79-
init({
80-
dsn: PUBLIC_DSN,
81-
defaultIntegrations: mockDefaultIntegrations,
82-
integrations: integrations => {
83-
const newIntegrations = [...integrations];
84-
newIntegrations[1] = newIntegration;
85-
return newIntegrations;
86-
},
72+
it('installs integrations returned from a callback function', () => {
73+
const mockDefaultIntegrations = [
74+
new MockIntegration('Some mock integration 3.1'),
75+
new MockIntegration('Some mock integration 3.2'),
76+
];
77+
78+
const newIntegration = new MockIntegration('Some mock integration 3.3');
79+
80+
init({
81+
dsn: PUBLIC_DSN,
82+
defaultIntegrations: mockDefaultIntegrations,
83+
integrations: integrations => {
84+
const newIntegrations = [...integrations];
85+
newIntegrations[1] = newIntegration;
86+
return newIntegrations;
87+
},
88+
});
89+
90+
expect(mockDefaultIntegrations[0]?.setupOnce as jest.Mock).toHaveBeenCalledTimes(1);
91+
expect(mockDefaultIntegrations[1]?.setupOnce as jest.Mock).toHaveBeenCalledTimes(0);
92+
expect(newIntegration.setupOnce as jest.Mock).toHaveBeenCalledTimes(1);
93+
expect(mockAutoPerformanceIntegrations).toHaveBeenCalledTimes(0);
8794
});
8895

89-
expect(mockDefaultIntegrations[0]?.setupOnce as jest.Mock).toHaveBeenCalledTimes(1);
90-
expect(mockDefaultIntegrations[1]?.setupOnce as jest.Mock).toHaveBeenCalledTimes(0);
91-
expect(newIntegration.setupOnce as jest.Mock).toHaveBeenCalledTimes(1);
92-
expect(mockAutoPerformanceIntegrations).toHaveBeenCalledTimes(0);
96+
it('installs performance default instrumentations if tracing is enabled', () => {
97+
const autoPerformanceIntegration = new MockIntegration('Some mock integration 4.4');
98+
99+
mockAutoPerformanceIntegrations.mockReset().mockImplementation(() => [autoPerformanceIntegration]);
100+
101+
const mockIntegrations = [
102+
new MockIntegration('Some mock integration 4.1'),
103+
new MockIntegration('Some mock integration 4.3'),
104+
];
105+
106+
init({
107+
dsn: PUBLIC_DSN,
108+
integrations: mockIntegrations,
109+
enableTracing: true,
110+
});
111+
112+
expect(mockIntegrations[0]?.setupOnce as jest.Mock).toHaveBeenCalledTimes(1);
113+
expect(mockIntegrations[1]?.setupOnce as jest.Mock).toHaveBeenCalledTimes(1);
114+
expect(autoPerformanceIntegration.setupOnce as jest.Mock).toHaveBeenCalledTimes(1);
115+
expect(mockAutoPerformanceIntegrations).toHaveBeenCalledTimes(1);
116+
117+
const client = getClient();
118+
expect(client?.getOptions()).toEqual(
119+
expect.objectContaining({
120+
integrations: expect.arrayContaining([mockIntegrations[0], mockIntegrations[1], autoPerformanceIntegration]),
121+
}),
122+
);
123+
});
93124
});
94125

95-
it('installs performance default instrumentations if tracing is enabled', () => {
96-
const autoPerformanceIntegration = new MockIntegration('Some mock integration 4.4');
126+
describe('OpenTelemetry', () => {
127+
it('sets up OpenTelemetry by default', () => {
128+
init({ dsn: PUBLIC_DSN });
129+
130+
const client = getClient<NodeClient>();
97131

98-
mockAutoPerformanceIntegrations.mockReset().mockImplementation(() => [autoPerformanceIntegration]);
132+
expect(client?.traceProvider).toBeDefined();
133+
});
99134

100-
const mockIntegrations = [
101-
new MockIntegration('Some mock integration 4.1'),
102-
new MockIntegration('Some mock integration 4.3'),
103-
];
135+
it('allows to opt-out of OpenTelemetry setup', () => {
136+
init({ dsn: PUBLIC_DSN, skipOpenTelemetrySetup: true });
104137

105-
init({
106-
dsn: PUBLIC_DSN,
107-
integrations: mockIntegrations,
108-
enableTracing: true,
138+
const client = getClient<NodeClient>();
139+
140+
expect(client?.traceProvider).not.toBeDefined();
109141
});
142+
});
143+
144+
it('returns intiated client', () => {
145+
const client = init({ dsn: PUBLIC_DSN, skipOpenTelemetrySetup: true });
110146

111-
expect(mockIntegrations[0]?.setupOnce as jest.Mock).toHaveBeenCalledTimes(1);
112-
expect(mockIntegrations[1]?.setupOnce as jest.Mock).toHaveBeenCalledTimes(1);
113-
expect(autoPerformanceIntegration.setupOnce as jest.Mock).toHaveBeenCalledTimes(1);
114-
expect(mockAutoPerformanceIntegrations).toHaveBeenCalledTimes(1);
115-
116-
const client = getClient();
117-
expect(client?.getOptions()).toEqual(
118-
expect.objectContaining({
119-
integrations: expect.arrayContaining([mockIntegrations[0], mockIntegrations[1], autoPerformanceIntegration]),
120-
}),
121-
);
147+
expect(client).toBeInstanceOf(NodeClient);
122148
});
123149

124-
it('sets up OpenTelemetry by default', () => {
125-
init({ dsn: PUBLIC_DSN });
150+
describe('autoSessionTracking', () => {
151+
it('does not track session by default if no release is set', () => {
152+
init({ dsn: PUBLIC_DSN });
126153

127-
const client = getClient<NodeClient>();
154+
const session = getIsolationScope().getSession();
155+
expect(session).toBeUndefined();
156+
});
128157

129-
expect(client?.traceProvider).toBeDefined();
130-
});
158+
it('tracks session by default if release is set', () => {
159+
init({ dsn: PUBLIC_DSN, release: '1.2.3' });
131160

132-
it('allows to opt-out of OpenTelemetry setup', () => {
133-
init({ dsn: PUBLIC_DSN, skipOpenTelemetrySetup: true });
161+
const session = getIsolationScope().getSession();
162+
expect(session).toBeDefined();
163+
});
134164

135-
const client = getClient<NodeClient>();
165+
it('does not track session if no release is set even if autoSessionTracking=true', () => {
166+
init({ dsn: PUBLIC_DSN, autoSessionTracking: true });
136167

137-
expect(client?.traceProvider).not.toBeDefined();
138-
});
168+
const session = getIsolationScope().getSession();
169+
expect(session).toBeUndefined();
170+
});
139171

140-
it('returns intiated client', () => {
141-
const client = init({ dsn: PUBLIC_DSN, skipOpenTelemetrySetup: true });
172+
it('does not track session if autoSessionTracking=false', () => {
173+
init({ dsn: PUBLIC_DSN, autoSessionTracking: false, release: '1.2.3' });
142174

143-
expect(client).toBeInstanceOf(NodeClient);
175+
const session = getIsolationScope().getSession();
176+
expect(session).toBeUndefined();
177+
});
178+
179+
it('tracks session by default if autoSessionTracking=true & release is set', () => {
180+
init({ dsn: PUBLIC_DSN, release: '1.2.3', autoSessionTracking: true });
181+
182+
const session = getIsolationScope().getSession();
183+
expect(session).toBeDefined();
184+
});
144185
});
145186
});

0 commit comments

Comments
 (0)