Skip to content

Commit 5bbcc5c

Browse files
committed
round 1 of fixing tests
1 parent e3d1681 commit 5bbcc5c

File tree

11 files changed

+146
-68
lines changed

11 files changed

+146
-68
lines changed

packages/browser/test/unit/integrations/linkederrors.test.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
11
import { Event as SentryEvent, Exception, ExtendedError } from '@sentry/types';
22
import { createStackParser } from '@sentry/utils';
33

4-
import { BrowserClient } from '../../../src/client';
4+
import { BrowserClient, BrowserClientOptions } from '../../../src/client';
55
import * as LinkedErrorsModule from '../../../src/integrations/linkederrors';
66
import { defaultStackParsers } from '../../../src/stack-parsers';
77
import { setupBrowserTransport } from '../../../src/transports';
8+
import { NoopTransport } from '@sentry/core/src/transports/noop';
9+
10+
function getDefaultBrowserOptions(options: Partial<BrowserClientOptions> = {}): BrowserClientOptions {
11+
return {
12+
integrations: [],
13+
transport: NoopTransport,
14+
stackParser: () => [],
15+
...options,
16+
};
17+
}
818

919
const parser = createStackParser(...defaultStackParsers);
1020

@@ -45,7 +55,7 @@ describe('LinkedErrors', () => {
4555
one.cause = two;
4656

4757
const originalException = one;
48-
const options = { stackParser: parser };
58+
const options = getDefaultBrowserOptions({ stackParser: parser });
4959
const client = new BrowserClient(options, setupBrowserTransport(options).transport);
5060
return client.eventFromException(originalException).then(event => {
5161
const result = LinkedErrorsModule._handler(parser, 'cause', 5, event, {
@@ -76,7 +86,7 @@ describe('LinkedErrors', () => {
7686
one.reason = two;
7787

7888
const originalException = one;
79-
const options = { stackParser: parser };
89+
const options = getDefaultBrowserOptions({ stackParser: parser });
8090
const client = new BrowserClient(options, setupBrowserTransport(options).transport);
8191
return client.eventFromException(originalException).then(event => {
8292
const result = LinkedErrorsModule._handler(parser, 'reason', 5, event, {
@@ -103,7 +113,7 @@ describe('LinkedErrors', () => {
103113
one.cause = two;
104114
two.cause = three;
105115

106-
const options = { stackParser: parser };
116+
const options = getDefaultBrowserOptions({ stackParser: parser });
107117
const client = new BrowserClient(options, setupBrowserTransport(options).transport);
108118
const originalException = one;
109119
return client.eventFromException(originalException).then(event => {

packages/browser/test/unit/transports/setup.test.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,16 @@ import {
88
XHRTransport,
99
} from '../../../src/transports';
1010
import { SimpleTransport } from '../mocks/simpletransport';
11+
import { BrowserClientOptions } from '../../../src/client';
12+
13+
function getDefaultBrowserOptions(options: Partial<BrowserClientOptions> = {}): BrowserClientOptions {
14+
return {
15+
integrations: [],
16+
transport: NoopTransport,
17+
stackParser: () => [],
18+
...options,
19+
};
20+
}
1121

1222
const DSN = 'https://username@domain/123';
1323

@@ -64,7 +74,7 @@ describe('setupBrowserTransport', () => {
6474
});
6575

6676
it('returns the instantiated transport passed via the options', () => {
67-
const options = { dsn: DSN, transport: SimpleTransport };
77+
const options = getDefaultBrowserOptions({ dsn: DSN, transport: SimpleTransport });
6878
const { transport, newTransport } = setupBrowserTransport(options);
6979

7080
expect(transport).toBeDefined();
@@ -73,7 +83,7 @@ describe('setupBrowserTransport', () => {
7383
});
7484

7585
it('returns fetchTransports if fetch is supported', () => {
76-
const options = { dsn: DSN };
86+
const options = getDefaultBrowserOptions({ dsn: DSN });
7787
const { transport, newTransport } = setupBrowserTransport(options);
7888

7989
expect(transport).toBeDefined();
@@ -86,7 +96,7 @@ describe('setupBrowserTransport', () => {
8696
it('returns xhrTransports if fetch is not supported', () => {
8797
fetchSupported = false;
8898

89-
const options = { dsn: DSN };
99+
const options = getDefaultBrowserOptions({ dsn: DSN });
90100
const { transport, newTransport } = setupBrowserTransport(options);
91101

92102
expect(transport).toBeDefined();

packages/core/test/lib/base.test.ts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { dsnToString, logger, SentryError, SyncPromise } from '@sentry/utils';
44

55
import * as integrationModule from '../../src/integration';
66
import { NoopTransport } from '../../src/transports/noop';
7-
import { setupTestTransport, TestClient, TestOptions } from '../mocks/client';
7+
import { setupTestTransport, TestClient, getDefaultTestOptions } from '../mocks/client';
88
import { TestIntegration } from '../mocks/integration';
99
import { FakeTransport } from '../mocks/transport';
1010

@@ -53,15 +53,6 @@ jest.mock('@sentry/utils', () => {
5353
};
5454
});
5555

56-
function getDefaultTestOptions(options: Partial<TestOptions> = {}): TestOptions {
57-
return {
58-
integrations: [],
59-
transport: NoopTransport,
60-
stackParser: () => [],
61-
...options,
62-
};
63-
}
64-
6556
describe('BaseClient', () => {
6657
beforeEach(() => {
6758
TestClient.sendEventCalled = undefined;

packages/core/test/lib/sdk.test.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Client, Integration } from '@sentry/types';
33

44
import { installedIntegrations } from '../../src/integration';
55
import { initAndBind } from '../../src/sdk';
6-
import { setupTestTransport, TestClient, TestOptions } from '../mocks/client';
6+
import { setupTestTransport, TestClient, TestOptions, getDefaultTestOptions } from '../mocks/client';
77

88
// eslint-disable-next-line no-var
99
declare var global: any;
@@ -55,7 +55,7 @@ describe('SDK', () => {
5555
new MockIntegration('MockIntegration 1'),
5656
new MockIntegration('MockIntegration 2'),
5757
];
58-
const options = { dsn: PUBLIC_DSN, defaultIntegrations: DEFAULT_INTEGRATIONS };
58+
const options = getDefaultTestOptions({ dsn: PUBLIC_DSN, defaultIntegrations: DEFAULT_INTEGRATIONS });
5959
initAndBind(TestClient, options, setupTestTransport(options).transport);
6060
expect((DEFAULT_INTEGRATIONS[0].setupOnce as jest.Mock).mock.calls.length).toBe(1);
6161
expect((DEFAULT_INTEGRATIONS[1].setupOnce as jest.Mock).mock.calls.length).toBe(1);
@@ -66,7 +66,7 @@ describe('SDK', () => {
6666
new MockIntegration('MockIntegration 1'),
6767
new MockIntegration('MockIntegration 2'),
6868
];
69-
const options: TestOptions = { dsn: PUBLIC_DSN, defaultIntegrations: false };
69+
const options = getDefaultTestOptions({ dsn: PUBLIC_DSN, defaultIntegrations: false });
7070
initAndBind(TestClient, options, setupTestTransport(options).transport);
7171
expect((DEFAULT_INTEGRATIONS[0].setupOnce as jest.Mock).mock.calls.length).toBe(0);
7272
expect((DEFAULT_INTEGRATIONS[1].setupOnce as jest.Mock).mock.calls.length).toBe(0);
@@ -77,7 +77,7 @@ describe('SDK', () => {
7777
new MockIntegration('MockIntegration 1'),
7878
new MockIntegration('MockIntegration 2'),
7979
];
80-
const options = { dsn: PUBLIC_DSN, integrations };
80+
const options = getDefaultTestOptions({ dsn: PUBLIC_DSN, integrations });
8181
initAndBind(TestClient, options, setupTestTransport(options).transport);
8282
expect((integrations[0].setupOnce as jest.Mock).mock.calls.length).toBe(1);
8383
expect((integrations[1].setupOnce as jest.Mock).mock.calls.length).toBe(1);
@@ -92,7 +92,11 @@ describe('SDK', () => {
9292
new MockIntegration('MockIntegration 1'),
9393
new MockIntegration('MockIntegration 3'),
9494
];
95-
const options = { dsn: PUBLIC_DSN, defaultIntegrations: DEFAULT_INTEGRATIONS, integrations };
95+
const options = getDefaultTestOptions({
96+
dsn: PUBLIC_DSN,
97+
defaultIntegrations: DEFAULT_INTEGRATIONS,
98+
integrations,
99+
});
96100
initAndBind(TestClient, options, setupTestTransport(options).transport);
97101
// 'MockIntegration 1' should be overridden by the one with the same name provided through options
98102
expect((DEFAULT_INTEGRATIONS[0].setupOnce as jest.Mock).mock.calls.length).toBe(0);
@@ -107,11 +111,11 @@ describe('SDK', () => {
107111
new MockIntegration('MockIntegration 2'),
108112
];
109113
const newIntegration = new MockIntegration('MockIntegration 3');
110-
const options = {
114+
const options = getDefaultTestOptions({
111115
defaultIntegrations: DEFAULT_INTEGRATIONS,
112116
dsn: PUBLIC_DSN,
113117
integrations: (integrations: Integration[]) => integrations.slice(0, 1).concat(newIntegration),
114-
};
118+
});
115119
initAndBind(TestClient, options, setupTestTransport(options).transport);
116120
expect((DEFAULT_INTEGRATIONS[0].setupOnce as jest.Mock).mock.calls.length).toBe(1);
117121
expect((newIntegration.setupOnce as jest.Mock).mock.calls.length).toBe(1);

packages/core/test/mocks/client.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,16 @@ import { BaseClient } from '../../src/baseclient';
66
import { initAndBind } from '../../src/sdk';
77
import { NewTransport } from '../../src/transports/base';
88
import { NoopTransport } from '../../src/transports/noop';
9+
10+
export function getDefaultTestOptions(options: Partial<TestOptions> = {}): TestOptions {
11+
return {
12+
integrations: [],
13+
transport: NoopTransport,
14+
stackParser: () => [],
15+
...options,
16+
};
17+
}
18+
919
export interface TestOptions extends ClientOptions {
1020
test?: boolean;
1121
mockInstallFailure?: boolean;

packages/node/test/client.test.ts

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,21 @@
11
import { Scope, SessionFlusher } from '@sentry/hub';
2+
import { NoopTransport } from '@sentry/core';
23

34
import { NodeClient } from '../src';
45
import { setupNodeTransport } from '../src/transports';
6+
import { NodeClientOptions } from '../src/types';
57

68
const PUBLIC_DSN = 'https://username@domain/123';
79

10+
function getDefaultNodeClientOptions(options: Partial<NodeClientOptions> = {}): NodeClientOptions {
11+
return {
12+
integrations: [],
13+
transport: NoopTransport,
14+
stackParser: () => [],
15+
...options,
16+
};
17+
}
18+
819
describe('NodeClient', () => {
920
let client: NodeClient;
1021

@@ -15,7 +26,7 @@ describe('NodeClient', () => {
1526

1627
describe('captureException', () => {
1728
test('when autoSessionTracking is enabled, and requestHandler is not used -> requestStatus should not be set', () => {
18-
const options = { dsn: PUBLIC_DSN, autoSessionTracking: true, release: '1.4' };
29+
const options = getDefaultNodeClientOptions({ dsn: PUBLIC_DSN, autoSessionTracking: true, release: '1.4' });
1930
client = new NodeClient(options, setupNodeTransport(options).transport);
2031
const scope = new Scope();
2132
scope.setRequestSession({ status: 'ok' });
@@ -26,7 +37,7 @@ describe('NodeClient', () => {
2637
expect(requestSession!.status).toEqual('ok');
2738
});
2839
test('when autoSessionTracking is disabled -> requestStatus should not be set', () => {
29-
const options = { dsn: PUBLIC_DSN, autoSessionTracking: false, release: '1.4' };
40+
const options = getDefaultNodeClientOptions({ dsn: PUBLIC_DSN, autoSessionTracking: false, release: '1.4' });
3041
client = new NodeClient(options, setupNodeTransport(options).transport);
3142
// It is required to initialise SessionFlusher to capture Session Aggregates (it is usually initialised
3243
// by the`requestHandler`)
@@ -41,7 +52,7 @@ describe('NodeClient', () => {
4152
expect(requestSession!.status).toEqual('ok');
4253
});
4354
test('when autoSessionTracking is enabled + requestSession status is Crashed -> requestStatus should not be overridden', () => {
44-
const options = { dsn: PUBLIC_DSN, autoSessionTracking: true, release: '1.4' };
55+
const options = getDefaultNodeClientOptions({ dsn: PUBLIC_DSN, autoSessionTracking: true, release: '1.4' });
4556
client = new NodeClient(options, setupNodeTransport(options).transport);
4657
// It is required to initialise SessionFlusher to capture Session Aggregates (it is usually initialised
4758
// by the`requestHandler`)
@@ -56,7 +67,7 @@ describe('NodeClient', () => {
5667
expect(requestSession!.status).toEqual('crashed');
5768
});
5869
test('when autoSessionTracking is enabled + error occurs within request bounds -> requestStatus should be set to Errored', () => {
59-
const options = { dsn: PUBLIC_DSN, autoSessionTracking: true, release: '1.4' };
70+
const options = getDefaultNodeClientOptions({ dsn: PUBLIC_DSN, autoSessionTracking: true, release: '1.4' });
6071
client = new NodeClient(options, setupNodeTransport(options).transport);
6172
// It is required to initialise SessionFlusher to capture Session Aggregates (it is usually initialised
6273
// by the`requestHandler`)
@@ -71,7 +82,7 @@ describe('NodeClient', () => {
7182
expect(requestSession!.status).toEqual('errored');
7283
});
7384
test('when autoSessionTracking is enabled + error occurs outside of request bounds -> requestStatus should not be set to Errored', () => {
74-
const options = { dsn: PUBLIC_DSN, autoSessionTracking: true, release: '1.4' };
85+
const options = getDefaultNodeClientOptions({ dsn: PUBLIC_DSN, autoSessionTracking: true, release: '1.4' });
7586
client = new NodeClient(options, setupNodeTransport(options).transport);
7687
// It is required to initialise SessionFlusher to capture Session Aggregates (it is usually initialised
7788
// by the`requestHandler`)
@@ -88,7 +99,7 @@ describe('NodeClient', () => {
8899

89100
describe('captureEvent()', () => {
90101
test('If autoSessionTracking is disabled, requestSession status should not be set', () => {
91-
const options = { dsn: PUBLIC_DSN, autoSessionTracking: false, release: '1.4' };
102+
const options = getDefaultNodeClientOptions({ dsn: PUBLIC_DSN, autoSessionTracking: false, release: '1.4' });
92103
client = new NodeClient(options, setupNodeTransport(options).transport);
93104
// It is required to initialise SessionFlusher to capture Session Aggregates (it is usually initialised
94105
// by the`requestHandler`)
@@ -107,7 +118,7 @@ describe('NodeClient', () => {
107118
});
108119

109120
test('When captureEvent is called with an exception, requestSession status should be set to Errored', () => {
110-
const options = { dsn: PUBLIC_DSN, autoSessionTracking: true, release: '2.2' };
121+
const options = getDefaultNodeClientOptions({ dsn: PUBLIC_DSN, autoSessionTracking: true, release: '2.2' });
111122
client = new NodeClient(options, setupNodeTransport(options).transport);
112123
// It is required to initialise SessionFlusher to capture Session Aggregates (it is usually initialised
113124
// by the`requestHandler`)
@@ -123,7 +134,7 @@ describe('NodeClient', () => {
123134
});
124135

125136
test('When captureEvent is called without an exception, requestSession status should not be set to Errored', () => {
126-
const options = { dsn: PUBLIC_DSN, autoSessionTracking: true, release: '2.2' };
137+
const options = getDefaultNodeClientOptions({ dsn: PUBLIC_DSN, autoSessionTracking: true, release: '2.2' });
127138
client = new NodeClient(options, setupNodeTransport(options).transport);
128139
// It is required to initialise SessionFlusher to capture Session Aggregates (it is usually initialised
129140
// by the`requestHandler`)
@@ -139,7 +150,7 @@ describe('NodeClient', () => {
139150
});
140151

141152
test('When captureEvent is called with an exception but outside of a request, then requestStatus should not be set', () => {
142-
const options = { dsn: PUBLIC_DSN, autoSessionTracking: true, release: '2.2' };
153+
const options = getDefaultNodeClientOptions({ dsn: PUBLIC_DSN, autoSessionTracking: true, release: '2.2' });
143154
client = new NodeClient(options, setupNodeTransport(options).transport);
144155
// It is required to initialise SessionFlusher to capture Session Aggregates (it is usually initialised
145156
// by the`requestHandler`)
@@ -157,7 +168,7 @@ describe('NodeClient', () => {
157168
});
158169

159170
test('When captureEvent is called with a transaction, then requestSession status should not be set', () => {
160-
const options = { dsn: PUBLIC_DSN, autoSessionTracking: true, release: '1.3' };
171+
const options = getDefaultNodeClientOptions({ dsn: PUBLIC_DSN, autoSessionTracking: true, release: '1.3' });
161172
client = new NodeClient(options, setupNodeTransport(options).transport);
162173
// It is required to initialise SessionFlusher to capture Session Aggregates (it is usually initialised
163174
// by the`requestHandler`)
@@ -172,7 +183,7 @@ describe('NodeClient', () => {
172183
});
173184

174185
test('When captureEvent is called with an exception but requestHandler is not used, then requestSession status should not be set', () => {
175-
const options = { dsn: PUBLIC_DSN, autoSessionTracking: true, release: '1.3' };
186+
const options = getDefaultNodeClientOptions({ dsn: PUBLIC_DSN, autoSessionTracking: true, release: '1.3' });
176187
client = new NodeClient(options, setupNodeTransport(options).transport);
177188

178189
const scope = new Scope();
@@ -192,11 +203,11 @@ describe('NodeClient', () => {
192203
describe('flush/close', () => {
193204
test('client close function disables _sessionFlusher', async () => {
194205
jest.useRealTimers();
195-
const options = {
206+
const options = getDefaultNodeClientOptions({
196207
dsn: PUBLIC_DSN,
197208
autoSessionTracking: true,
198209
release: '1.1',
199-
};
210+
});
200211
const client = new NodeClient(options, setupNodeTransport(options).transport);
201212
client.initSessionFlusher();
202213
// Clearing interval is important here to ensure that the flush function later on is called by the `client.close()`

0 commit comments

Comments
 (0)