Skip to content

Commit f76d41d

Browse files
committed
write exports tests
1 parent 596bafc commit f76d41d

File tree

1 file changed

+309
-0
lines changed

1 file changed

+309
-0
lines changed

packages/hub/test/exports.test.ts

Lines changed: 309 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,309 @@
1+
import { getCurrentHub, getHubFromCarrier, Scope } from '../src';
2+
3+
import {
4+
captureEvent,
5+
captureException,
6+
captureMessage,
7+
configureScope,
8+
setContext,
9+
setExtra,
10+
setExtras,
11+
setTag,
12+
setTags,
13+
setUser,
14+
withScope,
15+
} from '../src/exports';
16+
17+
export class TestClient {
18+
public static instance?: TestClient;
19+
20+
public constructor(public options: Record<string, unknown>) {
21+
TestClient.instance = this;
22+
}
23+
24+
public mySecretPublicMethod(str: string): string {
25+
return `secret: ${str}`;
26+
}
27+
}
28+
29+
export class TestClient2 {}
30+
31+
export function init(options: Record<string, unknown>): void {
32+
getCurrentHub().bindClient(new TestClient(options) as any);
33+
}
34+
35+
// eslint-disable-next-line no-var
36+
declare var global: any;
37+
38+
describe('Top Level API', () => {
39+
beforeEach(() => {
40+
global.__SENTRY__ = {
41+
hub: undefined,
42+
};
43+
});
44+
45+
describe('Capture', () => {
46+
test('Return an event_id', () => {
47+
const client: any = {
48+
captureException: jest.fn(async () => Promise.resolve()),
49+
};
50+
getCurrentHub().withScope(() => {
51+
getCurrentHub().bindClient(client);
52+
const e = new Error('test exception');
53+
const eventId = captureException(e);
54+
expect(eventId).toBeTruthy();
55+
});
56+
});
57+
58+
test('Exception', () => {
59+
const client: any = {
60+
captureException: jest.fn(async () => Promise.resolve()),
61+
};
62+
getCurrentHub().withScope(() => {
63+
getCurrentHub().bindClient(client);
64+
const e = new Error('test exception');
65+
captureException(e);
66+
expect(client.captureException.mock.calls[0][0]).toBe(e);
67+
});
68+
});
69+
70+
test('Exception with explicit scope', () => {
71+
const client: any = {
72+
captureException: jest.fn(async () => Promise.resolve()),
73+
};
74+
getCurrentHub().withScope(() => {
75+
getCurrentHub().bindClient(client);
76+
const e = new Error('test exception');
77+
const captureContext = { extra: { foo: 'wat' } };
78+
captureException(e, captureContext);
79+
expect(client.captureException.mock.calls[0][0]).toBe(e);
80+
expect(client.captureException.mock.calls[0][1].captureContext).toBe(captureContext);
81+
});
82+
});
83+
84+
test('Message', () => {
85+
const client: any = { captureMessage: jest.fn(async () => Promise.resolve()) };
86+
getCurrentHub().withScope(() => {
87+
getCurrentHub().bindClient(client);
88+
const message = 'yo';
89+
captureMessage(message);
90+
expect(client.captureMessage.mock.calls[0][0]).toBe(message);
91+
});
92+
});
93+
94+
test('Message with explicit scope', () => {
95+
const client: any = { captureMessage: jest.fn(async () => Promise.resolve()) };
96+
getCurrentHub().withScope(() => {
97+
getCurrentHub().bindClient(client);
98+
const message = 'yo';
99+
const captureContext = { extra: { foo: 'wat' } };
100+
captureMessage(message, captureContext);
101+
expect(client.captureMessage.mock.calls[0][0]).toBe(message);
102+
// Skip the level if explicit content is provided
103+
expect(client.captureMessage.mock.calls[0][1]).toBe(undefined);
104+
expect(client.captureMessage.mock.calls[0][2].captureContext).toBe(captureContext);
105+
});
106+
});
107+
108+
// NOTE: We left custom level as 2nd argument to not break the API. Should be removed and unified in v6.
109+
test('Message with custom level', () => {
110+
const client: any = { captureMessage: jest.fn(async () => Promise.resolve()) };
111+
getCurrentHub().withScope(() => {
112+
getCurrentHub().bindClient(client);
113+
const message = 'yo';
114+
const level = 'warning';
115+
captureMessage(message, level);
116+
expect(client.captureMessage.mock.calls[0][0]).toBe(message);
117+
expect(client.captureMessage.mock.calls[0][1]).toBe('warning');
118+
});
119+
});
120+
121+
test('Event', () => {
122+
const client: any = { captureEvent: jest.fn(async () => Promise.resolve()) };
123+
getCurrentHub().withScope(() => {
124+
getCurrentHub().bindClient(client);
125+
const e = { message: 'test' };
126+
captureEvent(e);
127+
expect(client.captureEvent.mock.calls[0][0]).toBe(e);
128+
});
129+
});
130+
});
131+
132+
describe('configureScope', () => {
133+
test('User Context', () => {
134+
const client: any = new TestClient({});
135+
getCurrentHub().pushScope();
136+
getCurrentHub().bindClient(client);
137+
configureScope((scope: Scope) => {
138+
scope.setUser({ id: '1234' });
139+
});
140+
expect(global.__SENTRY__.hub._stack[1].scope._user).toEqual({
141+
id: '1234',
142+
});
143+
getCurrentHub().popScope();
144+
});
145+
146+
test('Extra Context', () => {
147+
const client: any = new TestClient({});
148+
getCurrentHub().pushScope();
149+
getCurrentHub().bindClient(client);
150+
configureScope((scope: Scope) => {
151+
scope.setExtra('id', '1234');
152+
});
153+
expect(global.__SENTRY__.hub._stack[1].scope._extra).toEqual({
154+
id: '1234',
155+
});
156+
getCurrentHub().popScope();
157+
});
158+
159+
test('Tags Context', () => {
160+
init({});
161+
configureScope((scope: Scope) => {
162+
scope.setTag('id', '1234');
163+
});
164+
expect(global.__SENTRY__.hub._stack[0].scope._tags).toEqual({
165+
id: '1234',
166+
});
167+
});
168+
169+
test('Fingerprint', () => {
170+
const client: any = new TestClient({});
171+
getCurrentHub().pushScope();
172+
getCurrentHub().bindClient(client);
173+
configureScope((scope: Scope) => {
174+
scope.setFingerprint(['abcd']);
175+
});
176+
expect(global.__SENTRY__.hub._stack[1].scope._fingerprint).toEqual(['abcd']);
177+
});
178+
179+
test('Level', () => {
180+
const client: any = new TestClient({});
181+
const scope = getCurrentHub().pushScope();
182+
getCurrentHub().bindClient(client);
183+
scope.setLevel('warning');
184+
expect(global.__SENTRY__.hub._stack[1].scope._level).toEqual('warning');
185+
});
186+
});
187+
188+
test('Clear Scope', () => {
189+
const client: any = new TestClient({});
190+
getCurrentHub().withScope(() => {
191+
getCurrentHub().bindClient(client);
192+
expect(global.__SENTRY__.hub._stack.length).toBe(2);
193+
configureScope((scope: Scope) => {
194+
scope.setUser({ id: '1234' });
195+
});
196+
expect(global.__SENTRY__.hub._stack[1].scope._user).toEqual({
197+
id: '1234',
198+
});
199+
configureScope((scope: Scope) => {
200+
scope.clear();
201+
});
202+
expect(global.__SENTRY__.hub._stack[1].scope._user).toEqual({});
203+
});
204+
});
205+
206+
test('returns undefined before binding a client', () => {
207+
expect(getCurrentHub().getClient()).toBeUndefined();
208+
});
209+
210+
test('returns the bound client', () => {
211+
init({});
212+
expect(getCurrentHub().getClient()).toBe(TestClient.instance);
213+
});
214+
215+
test('does not throw an error when pushing different clients', () => {
216+
init({});
217+
expect(() => {
218+
getCurrentHub().withScope(() => {
219+
getCurrentHub().bindClient(new TestClient2() as any);
220+
});
221+
}).not.toThrow();
222+
});
223+
224+
test('does not throw an error when pushing same clients', () => {
225+
init({});
226+
expect(() => {
227+
getCurrentHub().withScope(() => {
228+
getCurrentHub().bindClient(new TestClient({}) as any);
229+
});
230+
}).not.toThrow();
231+
});
232+
233+
test('custom carrier', () => {
234+
const iAmSomeGlobalVarTheUserHasToManage = {
235+
state: {},
236+
};
237+
const hub = getHubFromCarrier(iAmSomeGlobalVarTheUserHasToManage.state);
238+
hub.pushScope();
239+
hub.bindClient(new TestClient({}) as any);
240+
hub.configureScope((scope: Scope) => {
241+
scope.setUser({ id: '1234' });
242+
});
243+
expect((iAmSomeGlobalVarTheUserHasToManage.state as any).__SENTRY__.hub._stack[1].scope._user).toEqual({
244+
id: '1234',
245+
});
246+
hub.popScope();
247+
expect((iAmSomeGlobalVarTheUserHasToManage.state as any).__SENTRY__.hub._stack[1]).toBeUndefined();
248+
});
249+
250+
test('withScope', () => {
251+
withScope(scope => {
252+
scope.setLevel('warning');
253+
scope.setFingerprint(['1']);
254+
withScope(scope2 => {
255+
scope2.setLevel('info');
256+
scope2.setFingerprint(['2']);
257+
withScope(scope3 => {
258+
scope3.clear();
259+
expect(global.__SENTRY__.hub._stack[1].scope._level).toEqual('warning');
260+
expect(global.__SENTRY__.hub._stack[1].scope._fingerprint).toEqual(['1']);
261+
expect(global.__SENTRY__.hub._stack[2].scope._level).toEqual('info');
262+
expect(global.__SENTRY__.hub._stack[2].scope._fingerprint).toEqual(['2']);
263+
expect(global.__SENTRY__.hub._stack[3].scope._level).toBeUndefined();
264+
});
265+
expect(global.__SENTRY__.hub._stack).toHaveLength(3);
266+
});
267+
expect(global.__SENTRY__.hub._stack).toHaveLength(2);
268+
});
269+
expect(global.__SENTRY__.hub._stack).toHaveLength(1);
270+
});
271+
272+
test('setExtras', () => {
273+
init({});
274+
setExtras({ a: 'b' });
275+
expect(global.__SENTRY__.hub._stack[0].scope._extra).toEqual({ a: 'b' });
276+
});
277+
278+
test('setTags', () => {
279+
init({});
280+
setTags({ a: 'b' });
281+
expect(global.__SENTRY__.hub._stack[0].scope._tags).toEqual({ a: 'b' });
282+
});
283+
284+
test('setExtra', () => {
285+
init({});
286+
setExtra('a', 'b');
287+
// prettier-ignore
288+
expect(global.__SENTRY__.hub._stack[0].scope._extra).toEqual({ 'a': 'b' });
289+
});
290+
291+
test('setTag', () => {
292+
init({});
293+
setTag('a', 'b');
294+
// prettier-ignore
295+
expect(global.__SENTRY__.hub._stack[0].scope._tags).toEqual({ 'a': 'b' });
296+
});
297+
298+
test('setUser', () => {
299+
init({});
300+
setUser({ id: 'b' });
301+
expect(global.__SENTRY__.hub._stack[0].scope._user).toEqual({ id: 'b' });
302+
});
303+
304+
test('setContext', () => {
305+
init({});
306+
setContext('test', { id: 'b' });
307+
expect(global.__SENTRY__.hub._stack[0].scope._contexts).toEqual({ test: { id: 'b' } });
308+
});
309+
});

0 commit comments

Comments
 (0)