Skip to content

Commit deff2f0

Browse files
authored
feat: Domain / integration node / hub tests (#1394)
* feat: Move get global hub to global function * feat: Fix domain handling in node * feat: More domain tests * feat: Add tests for unhandled promises * feat: Add tests for hub * ref: Minor code changes * ref: Only clone top stack
1 parent 5317a75 commit deff2f0

File tree

19 files changed

+629
-170
lines changed

19 files changed

+629
-170
lines changed

packages/browser/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export {
1919
configureScope,
2020
} from '@sentry/minimal';
2121

22-
export { Hub, Scope } from '@sentry/hub';
22+
export { getGlobalHub, Hub, Scope } from '@sentry/hub';
2323

2424
export { BrowserBackend, BrowserOptions } from './backend';
2525
export { BrowserClient } from './client';

packages/browser/test/index.test.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
captureException,
1010
captureMessage,
1111
configureScope,
12-
Hub,
12+
getGlobalHub,
1313
init,
1414
Scope,
1515
SentryEvent,
@@ -25,11 +25,11 @@ describe('SentryBrowser', () => {
2525
});
2626

2727
beforeEach(() => {
28-
Hub.getGlobal().pushScope();
28+
getGlobalHub().pushScope();
2929
});
3030

3131
afterEach(() => {
32-
Hub.getGlobal().popScope();
32+
getGlobalHub().popScope();
3333
});
3434

3535
describe('getContext() / setContext()', () => {
@@ -75,7 +75,7 @@ describe('SentryBrowser', () => {
7575
});
7676

7777
it('should record auto breadcrumbs', done => {
78-
Hub.getGlobal().pushScope(
78+
getGlobalHub().pushScope(
7979
new BrowserClient({
8080
afterSend: (event: SentryEvent) => {
8181
expect(event.breadcrumbs!).to.have.lengthOf(3);
@@ -97,7 +97,7 @@ describe('SentryBrowser', () => {
9797
addBreadcrumb({ message: 'test2' });
9898

9999
captureMessage('event');
100-
Hub.getGlobal().popScope();
100+
getGlobalHub().popScope();
101101
});
102102
});
103103

@@ -115,7 +115,7 @@ describe('SentryBrowser', () => {
115115
});
116116

117117
it('should capture an exception', done => {
118-
Hub.getGlobal().pushScope(
118+
getGlobalHub().pushScope(
119119
new BrowserClient({
120120
afterSend: (event: SentryEvent) => {
121121
expect(event.exception).to.not.be.undefined;
@@ -133,11 +133,11 @@ describe('SentryBrowser', () => {
133133
} catch (e) {
134134
captureException(e);
135135
}
136-
Hub.getGlobal().popScope();
136+
getGlobalHub().popScope();
137137
});
138138

139139
it('should capture a message', done => {
140-
Hub.getGlobal().pushScope(
140+
getGlobalHub().pushScope(
141141
new BrowserClient({
142142
afterSend: (event: SentryEvent) => {
143143
expect(event.message).to.equal('test');
@@ -148,11 +148,11 @@ describe('SentryBrowser', () => {
148148
}),
149149
);
150150
captureMessage('test');
151-
Hub.getGlobal().popScope();
151+
getGlobalHub().popScope();
152152
});
153153

154154
it('should capture an event', done => {
155-
Hub.getGlobal().pushScope(
155+
getGlobalHub().pushScope(
156156
new BrowserClient({
157157
afterSend: (event: SentryEvent) => {
158158
expect(event.message).to.equal('test');
@@ -163,7 +163,7 @@ describe('SentryBrowser', () => {
163163
}),
164164
);
165165
captureEvent({ message: 'test' });
166-
Hub.getGlobal().popScope();
166+
getGlobalHub().popScope();
167167
});
168168
});
169169
});

packages/hub/src/global.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
1+
import { Hub } from './hub';
12
import { Carrier } from './interfaces';
23

4+
/**
5+
* API compatibility version of this hub.
6+
*
7+
* WARNING: This number should only be incresed when the global interface
8+
* changes a and new methods are introduced.
9+
*/
10+
export const API_VERSION = 2;
11+
312
/** Global interface helper for type safety. */
413
interface Global {
514
__SENTRY__: Carrier;
@@ -15,3 +24,35 @@ global.__SENTRY__ = global.__SENTRY__ || {
1524
export function getGlobalCarrier(): Carrier {
1625
return global.__SENTRY__;
1726
}
27+
28+
/**
29+
* Returns the latest global hum instance.
30+
*
31+
* If a hub is already registered in the global carrier but this module
32+
* contains a more recent version, it replaces the registered version.
33+
* Otherwise, the currently registered hub will be returned.
34+
*/
35+
export function getGlobalHub(): Hub {
36+
const registry = getGlobalCarrier();
37+
38+
if (!registry.hub || registry.hub.isOlderThan(API_VERSION)) {
39+
registry.hub = new Hub();
40+
}
41+
42+
return registry.hub;
43+
}
44+
45+
/**
46+
* This will create a new {@link Hub} and add to the passed object on
47+
* __SENTRY__.hub.
48+
* @param carrier object
49+
*/
50+
export function getHubFromCarrier(carrier: any): Hub {
51+
if (carrier && carrier.__SENTRY__ && carrier.__SENTRY__.hub) {
52+
return carrier.__SENTRY__.hub;
53+
} else {
54+
carrier.__SENTRY__ = {};
55+
carrier.__SENTRY__.hub = new Hub();
56+
return carrier.__SENTRY__.hub;
57+
}
58+
}

packages/hub/src/hub.ts

Lines changed: 3 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,8 @@
11
import { Breadcrumb, SentryEvent } from '@sentry/types';
2-
import { getGlobalCarrier } from './global';
2+
import { API_VERSION } from './global';
33
import { Layer } from './interfaces';
44
import { Scope } from './scope';
55

6-
/**
7-
* API compatibility version of this hub.
8-
*
9-
* WARNING: This number should only be incresed when the global interface
10-
* changes a and new methods are introduced.
11-
*/
12-
export const API_VERSION = 2;
13-
146
/**
157
* Internal class used to make sure we always have the latest internal functions
168
* working in case we have a version conflict.
@@ -23,30 +15,13 @@ export class Hub {
2315
*/
2416
public constructor(
2517
private readonly stack: Layer[] = [],
26-
public readonly version: number = API_VERSION,
18+
private readonly version: number = API_VERSION,
2719
) {
2820
if (stack.length === 0) {
2921
this.stack.push({ scope: this.createScope(), type: 'process' });
3022
}
3123
}
3224

33-
/**
34-
* Returns the latest global hum instance.
35-
*
36-
* If a hub is already registered in the global carrier but this module
37-
* contains a more recent version, it replaces the registered version.
38-
* Otherwise, the currently registered hub will be returned.
39-
*/
40-
public static getGlobal(): Hub {
41-
const registry = getGlobalCarrier();
42-
43-
if (!registry.hub || registry.hub.isOlderThan(API_VERSION)) {
44-
registry.hub = new Hub();
45-
}
46-
47-
return registry.hub;
48-
}
49-
5025
/**
5126
* Internal helper function to call a method on the top client if it exists.
5227
*
@@ -97,7 +72,7 @@ export class Hub {
9772
*/
9873
public pushScope(client?: any): void {
9974
const usedClient = client || this.getCurrentClient();
100-
// We want to clone the last scope and not create a new one
75+
// We want to clone the content of prev scope
10176
const stack = this.getStack();
10277
const parentScope =
10378
stack.length > 0 ? stack[stack.length - 1].scope : undefined;
@@ -234,14 +209,3 @@ export class Hub {
234209
}
235210
}
236211
}
237-
238-
/** TODO */
239-
export function hubFromCarrier(carrier: any): Hub {
240-
if (carrier && carrier.__SENTRY__ && carrier.__SENTRY__.hub) {
241-
return carrier.__SENTRY__.hub;
242-
} else {
243-
carrier.__SENTRY__ = {};
244-
carrier.__SENTRY__.hub = new Hub();
245-
return carrier.__SENTRY__.hub;
246-
}
247-
}

packages/hub/src/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1-
export { Carrier, Layer } from './interfaces';
1+
export { Carrier, Layer, LayerType } from './interfaces';
22
export { Scope } from './scope';
3-
export { hubFromCarrier, Hub } from './hub';
3+
export { Hub } from './hub';
4+
export { getGlobalHub, getHubFromCarrier } from './global';

packages/hub/src/interfaces.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Hub } from './hub';
22
import { Scope } from './scope';
33

44
/** The type of a process stack layer. */
5-
export type LayerType = 'process' | 'domain' | 'local';
5+
export type LayerType = 'process' | 'local';
66

77
/** A layer in the process stack. */
88
export interface Layer {
@@ -13,6 +13,5 @@ export interface Layer {
1313

1414
/** An object that contains a hub and maintains a scope stack. */
1515
export interface Carrier {
16-
stack: Layer[];
1716
hub?: Hub;
1817
}

packages/hub/src/scope.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,8 @@ export class Scope {
141141

142142
/**
143143
* Sets the breadcrumbs in the scope
144-
* @param breadcrumbs
145-
* @param maxBreadcrumbs
144+
* @param breadcrumbs Breadcrumb
145+
* @param maxBreadcrumbs number of max breadcrumbs to merged into event.
146146
*/
147147
public addBreadcrumb(breadcrumb: Breadcrumb, maxBreadcrumbs?: number): void {
148148
this.breadcrumbs =
@@ -155,8 +155,9 @@ export class Scope {
155155
/**
156156
* Applies the current context and fingerprint to the event.
157157
* Note that breadcrumbs will be added by the client.
158-
* @param event
159-
* @param maxBreadcrumbs
158+
* Also if the event has already breadcrumbs on it, we do not merge them.
159+
* @param event SentryEvent
160+
* @param maxBreadcrumbs number of max breadcrumbs to merged into event.
160161
*/
161162
public applyToEvent(event: SentryEvent, maxBreadcrumbs?: number): void {
162163
if (this.extra && Object.keys(this.extra).length) {

packages/hub/test/lib/global.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { getGlobalHub, getHubFromCarrier, Hub } from '../../src';
2+
3+
describe('global', () => {
4+
test('getGlobalHub', () => {
5+
expect(getGlobalHub()).toBeTruthy();
6+
expect((global as any).__SENTRY__.hub).toBeTruthy();
7+
});
8+
9+
test('getHubFromCarrier', () => {
10+
const bla = { a: 'b' };
11+
getHubFromCarrier(bla);
12+
expect((bla as any).__SENTRY__.hub).toBeTruthy();
13+
expect((bla as any).__SENTRY__.hub).toBe((bla as any).__SENTRY__.hub);
14+
getHubFromCarrier(bla);
15+
});
16+
17+
test('getGlobalHub', () => {
18+
const newestHub = new Hub([], 999999);
19+
(global as any).__SENTRY__.hub = newestHub;
20+
expect(getGlobalHub()).toBe(newestHub);
21+
});
22+
});

0 commit comments

Comments
 (0)