Skip to content

Commit c8aa52e

Browse files
committed
feat: Add more tests
1 parent cfa2302 commit c8aa52e

File tree

2 files changed

+227
-153
lines changed

2 files changed

+227
-153
lines changed

packages/hub/test/hub.test.ts

Lines changed: 191 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -32,33 +32,57 @@ describe('Hub', () => {
3232
expect(hub.isOlderThan(0)).toBeFalsy();
3333
});
3434

35-
test('pushScope', () => {
36-
const localScope = new Scope();
37-
localScope.setExtra('a', 'b');
38-
const hub = new Hub(undefined, localScope);
39-
hub.pushScope();
40-
expect(hub.getStack()).toHaveLength(2);
41-
expect(hub.getStack()[1].scope).not.toBe(localScope);
42-
expect(((hub.getStack()[1].scope as Scope) as any)._extra).toEqual({ a: 'b' });
43-
});
35+
describe('pushScope', () => {
36+
test('simple', () => {
37+
const localScope = new Scope();
38+
localScope.setExtra('a', 'b');
39+
const hub = new Hub(undefined, localScope);
40+
hub.pushScope();
41+
expect(hub.getStack()).toHaveLength(2);
42+
expect(hub.getStack()[1].scope).not.toBe(localScope);
43+
expect(((hub.getStack()[1].scope as Scope) as any)._extra).toEqual({ a: 'b' });
44+
});
4445

45-
test('pushScope inherit client', () => {
46-
const testClient: any = { bla: 'a' };
47-
const hub = new Hub(testClient);
48-
hub.pushScope();
49-
expect(hub.getStack()).toHaveLength(2);
50-
expect(hub.getStack()[1].client).toBe(testClient);
51-
});
46+
test('inherit client', () => {
47+
const testClient: any = { bla: 'a' };
48+
const hub = new Hub(testClient);
49+
hub.pushScope();
50+
expect(hub.getStack()).toHaveLength(2);
51+
expect(hub.getStack()[1].client).toBe(testClient);
52+
});
5253

53-
test('pushScope bindClient', () => {
54-
const testClient: any = { bla: 'a' };
55-
const hub = new Hub(testClient);
56-
const ndClient: any = { foo: 'bar' };
57-
hub.pushScope();
58-
hub.bindClient(ndClient);
59-
expect(hub.getStack()).toHaveLength(2);
60-
expect(hub.getStack()[0].client).toBe(testClient);
61-
expect(hub.getStack()[1].client).toBe(ndClient);
54+
test('bindClient', () => {
55+
const testClient: any = { bla: 'a' };
56+
const hub = new Hub(testClient);
57+
const ndClient: any = { foo: 'bar' };
58+
hub.pushScope();
59+
hub.bindClient(ndClient);
60+
expect(hub.getStack()).toHaveLength(2);
61+
expect(hub.getStack()[0].client).toBe(testClient);
62+
expect(hub.getStack()[1].client).toBe(ndClient);
63+
});
64+
65+
test('inherit processors', () => {
66+
expect.assertions(1);
67+
const event: Event = {
68+
extra: { b: 3 },
69+
};
70+
const localScope = new Scope();
71+
localScope.setExtra('a', 'b');
72+
const hub = new Hub({ a: 'b' } as any, localScope);
73+
74+
localScope.addEventProcessor(async (processedEvent: Event) => {
75+
processedEvent.dist = '1';
76+
return processedEvent;
77+
});
78+
79+
hub.pushScope();
80+
const pushedScope = hub.getStackTop().scope;
81+
82+
return pushedScope!.applyToEvent(event).then(final => {
83+
expect(final!.dist).toEqual('1');
84+
});
85+
});
6286
});
6387

6488
test('popScope', () => {
@@ -69,23 +93,25 @@ describe('Hub', () => {
6993
expect(hub.getStack()).toHaveLength(1);
7094
});
7195

72-
test('withScope', () => {
73-
const hub = new Hub();
74-
hub.withScope(() => {
75-
expect(hub.getStack()).toHaveLength(2);
96+
describe('withScope', () => {
97+
test('simple', () => {
98+
const hub = new Hub();
99+
hub.withScope(() => {
100+
expect(hub.getStack()).toHaveLength(2);
101+
});
102+
expect(hub.getStack()).toHaveLength(1);
76103
});
77-
expect(hub.getStack()).toHaveLength(1);
78-
});
79104

80-
test('withScope bindClient', () => {
81-
const hub = new Hub();
82-
const testClient: any = { bla: 'a' };
83-
hub.withScope(() => {
84-
hub.bindClient(testClient);
85-
expect(hub.getStack()).toHaveLength(2);
86-
expect(hub.getStack()[1].client).toBe(testClient);
105+
test('bindClient', () => {
106+
const hub = new Hub();
107+
const testClient: any = { bla: 'a' };
108+
hub.withScope(() => {
109+
hub.bindClient(testClient);
110+
expect(hub.getStack()).toHaveLength(2);
111+
expect(hub.getStack()[1].client).toBe(testClient);
112+
});
113+
expect(hub.getStack()).toHaveLength(1);
87114
});
88-
expect(hub.getStack()).toHaveLength(1);
89115
});
90116

91117
test('getCurrentClient', () => {
@@ -109,98 +135,103 @@ describe('Hub', () => {
109135
expect(hub.getStackTop().client).toEqual({ bla: 'a' });
110136
});
111137

112-
test('captureException', () => {
113-
const hub = new Hub();
114-
const spy = jest.spyOn(hub as any, '_invokeClient');
115-
hub.captureException('a');
116-
expect(spy).toHaveBeenCalled();
117-
expect(spy.mock.calls[0][0]).toBe('captureException');
118-
expect(spy.mock.calls[0][1]).toBe('a');
119-
});
138+
describe('configureScope', () => {
139+
test('no client, should not invoke configureScope', () => {
140+
expect.assertions(0);
141+
const hub = new Hub();
142+
hub.configureScope(_ => {
143+
expect(true).toBeFalsy();
144+
});
145+
});
120146

121-
test('captureMessage', () => {
122-
const hub = new Hub();
123-
const spy = jest.spyOn(hub as any, '_invokeClient');
124-
hub.captureMessage('a');
125-
expect(spy).toHaveBeenCalled();
126-
expect(spy.mock.calls[0][0]).toBe('captureMessage');
127-
expect(spy.mock.calls[0][1]).toBe('a');
147+
test('no client, should not invoke configureScope', () => {
148+
expect.assertions(1);
149+
const localScope = new Scope();
150+
localScope.setExtra('a', 'b');
151+
const hub = new Hub({ a: 'b' } as any, localScope);
152+
hub.configureScope(confScope => {
153+
expect((confScope as any)._extra).toEqual({ a: 'b' });
154+
});
155+
});
128156
});
129157

130-
test('captureEvent', () => {
131-
const event: Event = {
132-
extra: { b: 3 },
133-
};
134-
const hub = new Hub();
135-
const spy = jest.spyOn(hub as any, '_invokeClient');
136-
hub.captureEvent(event);
137-
expect(spy).toHaveBeenCalled();
138-
expect(spy.mock.calls[0][0]).toBe('captureEvent');
139-
expect(spy.mock.calls[0][1]).toBe(event);
140-
});
158+
describe('captureException', () => {
159+
test('simple', () => {
160+
const hub = new Hub();
161+
const spy = jest.spyOn(hub as any, '_invokeClient');
162+
hub.captureException('a');
163+
expect(spy).toHaveBeenCalled();
164+
expect(spy.mock.calls[0][0]).toBe('captureException');
165+
expect(spy.mock.calls[0][1]).toBe('a');
166+
});
141167

142-
test('configureScope', () => {
143-
expect.assertions(0);
144-
const hub = new Hub();
145-
hub.configureScope(_ => {
146-
expect(true).toBeFalsy();
168+
test('should set event_id in hint', () => {
169+
const hub = new Hub();
170+
const spy = jest.spyOn(hub as any, '_invokeClient');
171+
hub.captureException('a');
172+
expect(spy.mock.calls[0][2].event_id).toBeTruthy();
147173
});
148-
});
149174

150-
test('configureScope', () => {
151-
expect.assertions(1);
152-
const localScope = new Scope();
153-
localScope.setExtra('a', 'b');
154-
const hub = new Hub({ a: 'b' } as any, localScope);
155-
hub.configureScope(confScope => {
156-
expect((confScope as any)._extra).toEqual({ a: 'b' });
175+
test('should generate hint if not provided in the call', () => {
176+
const hub = new Hub();
177+
const spy = jest.spyOn(hub as any, '_invokeClient');
178+
const ex = new Error('foo');
179+
hub.captureException(ex);
180+
expect(spy.mock.calls[0][2].originalException).toBe(ex);
181+
expect(spy.mock.calls[0][2].syntheticException).toBeInstanceOf(Error);
182+
expect(spy.mock.calls[0][2].syntheticException.message).toBe('Sentry syntheticException');
157183
});
158184
});
159185

160-
test('pushScope inherit processors', () => {
161-
expect.assertions(1);
162-
const event: Event = {
163-
extra: { b: 3 },
164-
};
165-
const localScope = new Scope();
166-
localScope.setExtra('a', 'b');
167-
const hub = new Hub({ a: 'b' } as any, localScope);
168-
169-
localScope.addEventProcessor(async (processedEvent: Event) => {
170-
processedEvent.dist = '1';
171-
return processedEvent;
186+
describe('captureMessage', () => {
187+
test('simple', () => {
188+
const hub = new Hub();
189+
const spy = jest.spyOn(hub as any, '_invokeClient');
190+
hub.captureMessage('a');
191+
expect(spy).toHaveBeenCalled();
192+
expect(spy.mock.calls[0][0]).toBe('captureMessage');
193+
expect(spy.mock.calls[0][1]).toBe('a');
172194
});
173195

174-
hub.pushScope();
175-
const pushedScope = hub.getStackTop().scope;
176-
177-
return pushedScope!.applyToEvent(event).then(final => {
178-
expect(final!.dist).toEqual('1');
196+
test('should set event_id in hint', () => {
197+
const hub = new Hub();
198+
const spy = jest.spyOn(hub as any, '_invokeClient');
199+
hub.captureMessage('a');
200+
expect(spy.mock.calls[0][3].event_id).toBeTruthy();
179201
});
180-
});
181202

182-
test('captureException should set event_id in hint', () => {
183-
const hub = new Hub();
184-
const spy = jest.spyOn(hub as any, '_invokeClient');
185-
hub.captureException('a');
186-
expect(spy.mock.calls[0][2].event_id).toBeTruthy();
203+
test('should generate hint if not provided in the call', () => {
204+
const hub = new Hub();
205+
const spy = jest.spyOn(hub as any, '_invokeClient');
206+
hub.captureMessage('foo');
207+
expect(spy.mock.calls[0][3].originalException).toBe('foo');
208+
expect(spy.mock.calls[0][3].syntheticException).toBeInstanceOf(Error);
209+
expect(spy.mock.calls[0][3].syntheticException.message).toBe('foo');
210+
});
187211
});
188212

189-
test('captureMessage should set event_id in hint', () => {
190-
const hub = new Hub();
191-
const spy = jest.spyOn(hub as any, '_invokeClient');
192-
hub.captureMessage('a');
193-
expect(spy.mock.calls[0][3].event_id).toBeTruthy();
194-
});
213+
describe('captureEvent', () => {
214+
test('simple', () => {
215+
const event: Event = {
216+
extra: { b: 3 },
217+
};
218+
const hub = new Hub();
219+
const spy = jest.spyOn(hub as any, '_invokeClient');
220+
hub.captureEvent(event);
221+
expect(spy).toHaveBeenCalled();
222+
expect(spy.mock.calls[0][0]).toBe('captureEvent');
223+
expect(spy.mock.calls[0][1]).toBe(event);
224+
});
195225

196-
test('captureEvent should set event_id in hint', () => {
197-
const event: Event = {
198-
extra: { b: 3 },
199-
};
200-
const hub = new Hub();
201-
const spy = jest.spyOn(hub as any, '_invokeClient');
202-
hub.captureEvent(event);
203-
expect(spy.mock.calls[0][2].event_id).toBeTruthy();
226+
test('should set event_id in hint', () => {
227+
const event: Event = {
228+
extra: { b: 3 },
229+
};
230+
const hub = new Hub();
231+
const spy = jest.spyOn(hub as any, '_invokeClient');
232+
hub.captureEvent(event);
233+
expect(spy.mock.calls[0][2].event_id).toBeTruthy();
234+
});
204235
});
205236

206237
test('lastEventId should be the same as last created', () => {
@@ -212,25 +243,6 @@ describe('Hub', () => {
212243
expect(eventId).toBe(hub.lastEventId());
213244
});
214245

215-
test('captureException should generate hint if not provided in the call', () => {
216-
const hub = new Hub();
217-
const spy = jest.spyOn(hub as any, '_invokeClient');
218-
const ex = new Error('foo');
219-
hub.captureException(ex);
220-
expect(spy.mock.calls[0][2].originalException).toBe(ex);
221-
expect(spy.mock.calls[0][2].syntheticException).toBeInstanceOf(Error);
222-
expect(spy.mock.calls[0][2].syntheticException.message).toBe('Sentry syntheticException');
223-
});
224-
225-
test('captureMessage should generate hint if not provided in the call', () => {
226-
const hub = new Hub();
227-
const spy = jest.spyOn(hub as any, '_invokeClient');
228-
hub.captureMessage('foo');
229-
expect(spy.mock.calls[0][3].originalException).toBe('foo');
230-
expect(spy.mock.calls[0][3].syntheticException).toBeInstanceOf(Error);
231-
expect(spy.mock.calls[0][3].syntheticException.message).toBe('foo');
232-
});
233-
234246
test('run', () => {
235247
const currentHub = getCurrentHub();
236248
const myScope = new Scope();
@@ -269,4 +281,49 @@ describe('Hub', () => {
269281
});
270282
});
271283
});
284+
285+
describe('spans', () => {
286+
describe('start', () => {
287+
test('simple', () => {
288+
const hub = new Hub(clientFn);
289+
const span = hub.startSpan() as any;
290+
expect(span._spanId).toBeTruthy();
291+
});
292+
293+
test('bindOnScope', () => {
294+
const myScope = new Scope();
295+
const hub = new Hub(clientFn, myScope);
296+
const span = hub.startSpan({}, true) as any;
297+
expect((myScope as any)._span).toBe(span);
298+
});
299+
});
300+
301+
describe('finish', () => {
302+
test('simple', () => {
303+
const hub = new Hub(clientFn);
304+
const span = hub.startSpan() as any;
305+
expect(span.timestamp).toBeUndefined();
306+
expect(hub.finishSpan(span)).toBeUndefined();
307+
expect(span.timestamp).toBeGreaterThan(1);
308+
});
309+
310+
test('finish a scope span without transaction', () => {
311+
const myScope = new Scope();
312+
const hub = new Hub(clientFn, myScope);
313+
const spy = jest.spyOn(hub as any, 'captureEvent');
314+
const span = hub.startSpan({}, true) as any;
315+
expect(hub.finishSpan(span)).toBeUndefined();
316+
expect(spy).not.toHaveBeenCalled();
317+
});
318+
319+
test('finish a scope span with transaction', () => {
320+
const myScope = new Scope();
321+
const hub = new Hub(clientFn, myScope);
322+
const spy = jest.spyOn(hub as any, 'captureEvent');
323+
const span = hub.startSpan({ transaction: 'test' }, true) as any;
324+
expect(hub.finishSpan(span)).toBeDefined();
325+
expect(spy).toHaveBeenCalled();
326+
});
327+
});
328+
});
272329
});

0 commit comments

Comments
 (0)