Skip to content

Commit 2275534

Browse files
committed
fix more node unit tests
1 parent 7245e18 commit 2275534

File tree

5 files changed

+61
-35
lines changed

5 files changed

+61
-35
lines changed

packages/core/src/baseclient.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import {
2828
uuid4,
2929
} from '@sentry/utils';
3030

31-
import { initAPIDetails } from './api';
31+
import { APIDetails, initAPIDetails } from './api';
3232
import { IS_DEBUG_BUILD } from './flags';
3333
import { IntegrationIndex, setupIntegrations } from './integration';
3434
import { createEventEnvelope, createSessionEnvelope } from './request';
@@ -106,6 +106,13 @@ export abstract class BaseClient<O extends Options> implements Client<O> {
106106
// TODO(v7): remove old transport
107107
this._transport = transport;
108108
this._newTransport = newTransport;
109+
110+
// TODO(v7): refactor this to keep metadata/api outside of transport. This hack is used to
111+
// satisfy tests until we move to NewTransport where we have to revisit this.
112+
(this._transport as unknown as { _api: Partial<APIDetails> })._api = {
113+
...((this._transport as unknown as { _api: Partial<APIDetails> })._api || {}),
114+
metadata: options._metadata || {},
115+
};
109116
}
110117

111118
/**

packages/node-integration-tests/suites/sessions/errored-session-aggregate/test.ts

+3
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,11 @@ test('should aggregate successful, crashed and erroneous sessions', async () =>
1010
getEnvelopeRequest(`${url}/error_handled`),
1111
getEnvelopeRequest(`${url}/error_unhandled`),
1212
]);
13+
console.log(envelope);
1314

1415
expect(envelope).toHaveLength(3);
16+
console.log(envelope[0]);
17+
1518
expect(envelope[0]).toMatchObject({
1619
sent_at: expect.any(String),
1720
sdk: {

packages/node/test/handlers.test.ts

+21-10
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
tracingHandler,
1919
} from '../src/handlers';
2020
import * as SDK from '../src/sdk';
21+
import { setupNodeTransport } from '../src/transports';
2122

2223
describe('parseRequest', () => {
2324
let mockReq: { [key: string]: any };
@@ -223,7 +224,8 @@ describe('requestHandler', () => {
223224
});
224225

225226
it('autoSessionTracking is enabled, sets requestSession status to ok, when handling a request', () => {
226-
client = new NodeClient({ autoSessionTracking: true, release: '1.2' });
227+
const options = { autoSessionTracking: true, release: '1.2' };
228+
client = new NodeClient(options, setupNodeTransport(options).transport);
227229
const hub = new Hub(client);
228230

229231
jest.spyOn(sentryCore, 'getCurrentHub').mockReturnValue(hub);
@@ -235,7 +237,8 @@ describe('requestHandler', () => {
235237
});
236238

237239
it('autoSessionTracking is disabled, does not set requestSession, when handling a request', () => {
238-
client = new NodeClient({ autoSessionTracking: false, release: '1.2' });
240+
const options = { autoSessionTracking: false, release: '1.2' };
241+
client = new NodeClient(options, setupNodeTransport(options).transport);
239242
const hub = new Hub(client);
240243

241244
jest.spyOn(sentryCore, 'getCurrentHub').mockReturnValue(hub);
@@ -247,7 +250,8 @@ describe('requestHandler', () => {
247250
});
248251

249252
it('autoSessionTracking is enabled, calls _captureRequestSession, on response finish', done => {
250-
client = new NodeClient({ autoSessionTracking: true, release: '1.2' });
253+
const options = { autoSessionTracking: true, release: '1.2' };
254+
client = new NodeClient(options, setupNodeTransport(options).transport);
251255
const hub = new Hub(client);
252256

253257
jest.spyOn(sentryCore, 'getCurrentHub').mockReturnValue(hub);
@@ -267,7 +271,8 @@ describe('requestHandler', () => {
267271
});
268272

269273
it('autoSessionTracking is disabled, does not call _captureRequestSession, on response finish', done => {
270-
client = new NodeClient({ autoSessionTracking: false, release: '1.2' });
274+
const options = { autoSessionTracking: false, release: '1.2' };
275+
client = new NodeClient(options, setupNodeTransport(options).transport);
271276
const hub = new Hub(client);
272277
jest.spyOn(sentryCore, 'getCurrentHub').mockReturnValue(hub);
273278

@@ -367,7 +372,8 @@ describe('tracingHandler', () => {
367372

368373
it('extracts request data for sampling context', () => {
369374
const tracesSampler = jest.fn();
370-
const hub = new Hub(new NodeClient({ tracesSampler }));
375+
const options = { tracesSampler };
376+
const hub = new Hub(new NodeClient(options, setupNodeTransport(options).transport));
371377
// we need to mock both of these because the tracing handler relies on `@sentry/core` while the sampler relies on
372378
// `@sentry/hub`, and mocking breaks the link between the two
373379
jest.spyOn(sentryCore, 'getCurrentHub').mockReturnValue(hub);
@@ -389,7 +395,8 @@ describe('tracingHandler', () => {
389395
});
390396

391397
it('puts its transaction on the scope', () => {
392-
const hub = new Hub(new NodeClient({ tracesSampleRate: 1.0 }));
398+
const options = { tracesSampleRate: 1.0 };
399+
const hub = new Hub(new NodeClient(options, setupNodeTransport(options).transport));
393400
// we need to mock both of these because the tracing handler relies on `@sentry/core` while the sampler relies on
394401
// `@sentry/hub`, and mocking breaks the link between the two
395402
jest.spyOn(sentryCore, 'getCurrentHub').mockReturnValue(hub);
@@ -720,7 +727,8 @@ describe('errorHandler()', () => {
720727
jest.restoreAllMocks();
721728
});
722729
it('when autoSessionTracking is disabled, does not set requestSession status on Crash', () => {
723-
client = new NodeClient({ autoSessionTracking: false, release: '3.3' });
730+
const options = { autoSessionTracking: false, release: '3.3' };
731+
client = new NodeClient(options, setupNodeTransport(options).transport);
724732
// It is required to initialise SessionFlusher to capture Session Aggregates (it is usually initialised
725733
// by the`requestHandler`)
726734
client.initSessionFlusher();
@@ -739,7 +747,8 @@ describe('errorHandler()', () => {
739747
});
740748

741749
it('autoSessionTracking is enabled + requestHandler is not used -> does not set requestSession status on Crash', () => {
742-
client = new NodeClient({ autoSessionTracking: false, release: '3.3' });
750+
const options = { autoSessionTracking: false, release: '3.3' };
751+
client = new NodeClient(options, setupNodeTransport(options).transport);
743752

744753
const scope = sentryCore.getCurrentHub().getScope();
745754
const hub = new Hub(client);
@@ -755,7 +764,8 @@ describe('errorHandler()', () => {
755764
});
756765

757766
it('when autoSessionTracking is enabled, should set requestSession status to Crashed when an unhandled error occurs within the bounds of a request', () => {
758-
client = new NodeClient({ autoSessionTracking: true, release: '1.1' });
767+
const options = { autoSessionTracking: true, release: '1.1' };
768+
client = new NodeClient(options, setupNodeTransport(options).transport);
759769
// It is required to initialise SessionFlusher to capture Session Aggregates (it is usually initialised
760770
// by the`requestHandler`)
761771
client.initSessionFlusher();
@@ -773,7 +783,8 @@ describe('errorHandler()', () => {
773783
});
774784

775785
it('when autoSessionTracking is enabled, should not set requestSession status on Crash when it occurs outside the bounds of a request', () => {
776-
client = new NodeClient({ autoSessionTracking: true, release: '2.2' });
786+
const options = { autoSessionTracking: true, release: '2.2' };
787+
client = new NodeClient(options, setupNodeTransport(options).transport);
777788
// It is required to initialise SessionFlusher to capture Session Aggregates (it is usually initialised
778789
// by the`requestHandler`)
779790
client.initSessionFlusher();

packages/node/test/integrations/http.test.ts

+18-19
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,18 @@ import * as nock from 'nock';
1111
import { Breadcrumb } from '../../src';
1212
import { NodeClient } from '../../src/client';
1313
import { Http as HttpIntegration } from '../../src/integrations/http';
14+
import { setupNodeTransport } from '../../src/transports';
1415

1516
const NODE_VERSION = parseSemver(process.versions.node);
1617

1718
describe('tracing', () => {
1819
function createTransactionOnScope() {
19-
const hub = new Hub(
20-
new NodeClient({
21-
dsn: 'https://[email protected]/12312012',
22-
tracesSampleRate: 1.0,
23-
integrations: [new HttpIntegration({ tracing: true })],
24-
}),
25-
);
20+
const options = {
21+
dsn: 'https://[email protected]/12312012',
22+
tracesSampleRate: 1.0,
23+
integrations: [new HttpIntegration({ tracing: true })],
24+
};
25+
const hub = new Hub(new NodeClient(options, setupNodeTransport(options).transport));
2626
addExtensionMethods();
2727

2828
// we need to mock both of these because the tracing handler relies on `@sentry/core` while the sampler relies on
@@ -97,18 +97,17 @@ describe('default protocols', () => {
9797
const p = new Promise<Breadcrumb>(r => {
9898
resolve = r;
9999
});
100-
hub.bindClient(
101-
new NodeClient({
102-
dsn: 'https://[email protected]/12312012',
103-
integrations: [new HttpIntegration({ breadcrumbs: true })],
104-
beforeBreadcrumb: (b: Breadcrumb) => {
105-
if ((b.data?.url as string).includes(key)) {
106-
resolve(b);
107-
}
108-
return b;
109-
},
110-
}),
111-
);
100+
const options = {
101+
dsn: 'https://[email protected]/12312012',
102+
integrations: [new HttpIntegration({ breadcrumbs: true })],
103+
beforeBreadcrumb: (b: Breadcrumb) => {
104+
if ((b.data?.url as string).includes(key)) {
105+
resolve(b);
106+
}
107+
return b;
108+
},
109+
};
110+
hub.bindClient(new NodeClient(options, setupNodeTransport(options).transport));
112111

113112
return p;
114113
}

packages/node/test/integrations/linkederrors.test.ts

+11-5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { createStackParser } from '@sentry/utils';
44
import { Event, NodeClient } from '../../src';
55
import { LinkedErrors } from '../../src/integrations/linkederrors';
66
import { nodeStackParser } from '../../src/stack-parser';
7+
import { setupNodeTransport } from '../../src/transports';
78

89
const stackParser = createStackParser(nodeStackParser);
910

@@ -31,7 +32,8 @@ describe('LinkedErrors', () => {
3132
expect.assertions(2);
3233
const spy = jest.spyOn(linkedErrors, '_walkErrorTree');
3334
const one = new Error('originalException');
34-
const client = new NodeClient({ stackParser });
35+
const options = { stackParser };
36+
const client = new NodeClient(options, setupNodeTransport(options).transport);
3537
let event: Event | undefined;
3638
return client
3739
.eventFromException(one)
@@ -54,7 +56,8 @@ describe('LinkedErrors', () => {
5456
}),
5557
);
5658
const one = new Error('originalException');
57-
const client = new NodeClient({ stackParser });
59+
const options = { stackParser };
60+
const client = new NodeClient(options, setupNodeTransport(options).transport);
5861
return client.eventFromException(one).then(event =>
5962
linkedErrors
6063
._handler(stackParser, event, {
@@ -74,7 +77,8 @@ describe('LinkedErrors', () => {
7477
one.cause = two;
7578
two.cause = three;
7679

77-
const client = new NodeClient({ stackParser });
80+
const options = { stackParser };
81+
const client = new NodeClient(options, setupNodeTransport(options).transport);
7882
return client.eventFromException(one).then(event =>
7983
linkedErrors
8084
._handler(stackParser, event, {
@@ -107,7 +111,8 @@ describe('LinkedErrors', () => {
107111
one.reason = two;
108112
two.reason = three;
109113

110-
const client = new NodeClient({ stackParser });
114+
const options = { stackParser };
115+
const client = new NodeClient(options, setupNodeTransport(options).transport);
111116
return client.eventFromException(one).then(event =>
112117
linkedErrors
113118
._handler(stackParser, event, {
@@ -140,7 +145,8 @@ describe('LinkedErrors', () => {
140145
one.cause = two;
141146
two.cause = three;
142147

143-
const client = new NodeClient({ stackParser });
148+
const options = { stackParser };
149+
const client = new NodeClient(options, setupNodeTransport(options).transport);
144150
return client.eventFromException(one).then(event =>
145151
linkedErrors
146152
._handler(stackParser, event, {

0 commit comments

Comments
 (0)