Skip to content

Commit f8b6dd8

Browse files
committed
ref: Add tests for breadcrumbs
1 parent 610ff22 commit f8b6dd8

File tree

6 files changed

+82
-51
lines changed

6 files changed

+82
-51
lines changed

packages/browser/examples/app.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ class HappyTransport extends Sentry.Transports.BaseTransport {
2525
event,
2626
);
2727

28-
return {
28+
return Promise.resolve({
2929
status: 'success',
30-
};
30+
});
3131
}
3232
}
3333

packages/browser/src/client.ts

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -73,30 +73,14 @@ export class BrowserClient extends BaseClient<BrowserBackend, BrowserOptions> {
7373
/**
7474
* @inheritDoc
7575
*/
76-
public captureEvent(event: Event, hint?: EventHint, scope?: Scope): string | undefined {
77-
let eventId: string | undefined = hint && hint.event_id;
78-
this._processing = true;
79-
80-
this._processEvent(event, hint, scope)
81-
.then(finalEvent => {
82-
// We need to check for finalEvent in case beforeSend returned null
83-
// We do this here to not parse any requests if they are outgoing to Sentry
84-
// We log breadcrumbs if the integration has them enabled
85-
if (finalEvent) {
86-
eventId = finalEvent.event_id;
87-
const integration = this.getIntegration(Breadcrumbs);
88-
if (integration) {
89-
integration.addSentryBreadcrumb(finalEvent);
90-
}
91-
}
92-
this._processing = false;
93-
})
94-
.then(null, reason => {
95-
logger.error(reason);
96-
this._processing = false;
97-
});
98-
99-
return eventId;
76+
protected _sendEvent(event: Event): void {
77+
if (event) {
78+
const integration = this.getIntegration(Breadcrumbs);
79+
if (integration) {
80+
integration.addSentryBreadcrumb(event);
81+
}
82+
}
83+
super._sendEvent(event);
10084
}
10185

10286
/**

packages/browser/test/integration/common/init.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,11 @@ function initSDK() {
5151
}
5252

5353
// One of the tests use manually created breadcrumb without eventId and we want to let it through
54-
if (breadcrumb.category.indexOf("sentry" === 0) && breadcrumb.event_id) {
54+
if (
55+
breadcrumb.category.indexOf("sentry" === 0) &&
56+
breadcrumb.event_id &&
57+
!window.allowSentryBreadcrumbs
58+
) {
5559
return null;
5660
}
5761

packages/browser/test/integration/karma.conf.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ module.exports = config => {
7373
"/base/variants/123": "/base/subjects/123",
7474
// Supresses warnings
7575
"/api/1/store/": "/",
76+
"/api/1/envelope/": "/",
7677
},
7778
frameworks: ["mocha", "chai", "sinon"],
7879
files,

packages/browser/test/integration/suites/api.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,56 @@ describe("API", function() {
2626
});
2727
});
2828

29+
it("should capture Sentry internal event as breadcrumbs for the following event sent", function() {
30+
return runInSandbox(sandbox, { manual: true }, function() {
31+
window.allowSentryBreadcrumbs = true;
32+
Sentry.captureMessage("a");
33+
Sentry.captureMessage("b");
34+
// For the loader
35+
Sentry.flush && Sentry.flush(2000);
36+
window.finalizeManualTest();
37+
}).then(function(summary) {
38+
assert.equal(summary.events.length, 2);
39+
assert.equal(summary.breadcrumbs.length, 2);
40+
assert.equal(summary.events[1].breadcrumbs[0].category, "sentry.event");
41+
assert.equal(
42+
summary.events[1].breadcrumbs[0].event_id,
43+
summary.events[0].event_id
44+
);
45+
assert.equal(
46+
summary.events[1].breadcrumbs[0].level,
47+
summary.events[0].level
48+
);
49+
});
50+
});
51+
52+
it("should capture Sentry internal transaction as breadcrumbs for the following event sent", function() {
53+
return runInSandbox(sandbox, { manual: true }, function() {
54+
window.allowSentryBreadcrumbs = true;
55+
Sentry.captureEvent({
56+
event_id: "aa3ff046696b4bc6b609ce6d28fde9e2",
57+
message: "someMessage",
58+
transaction: "wat",
59+
type: "transaction",
60+
});
61+
Sentry.captureMessage("c");
62+
// For the loader
63+
Sentry.flush && Sentry.flush(2000);
64+
window.finalizeManualTest();
65+
}).then(function(summary) {
66+
// We have a length of one here since transactions don't go through beforeSend
67+
// and we add events to summary in beforeSend
68+
assert.equal(summary.events.length, 1);
69+
assert.equal(summary.breadcrumbs.length, 2);
70+
assert.equal(
71+
summary.events[0].breadcrumbs[0].category,
72+
"sentry.transaction"
73+
);
74+
assert.isNotEmpty(summary.events[0].breadcrumbs[0].event_id);
75+
assert.isUndefined(summary.events[0].breadcrumbs[0].level);
76+
});
77+
});
78+
2979
it("should generate a synthetic trace for captureException w/ non-errors", function() {
3080
return runInSandbox(sandbox, function() {
3181
throwNonError();

packages/core/src/baseclient.ts

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -91,15 +91,8 @@ export abstract class BaseClient<B extends Backend, O extends Options> implement
9191

9292
this._getBackend()
9393
.eventFromException(exception, hint)
94-
.then(event => this._processEvent(event, hint, scope))
95-
.then(finalEvent => {
96-
// We need to check for finalEvent in case beforeSend returned null
97-
eventId = finalEvent && finalEvent.event_id;
98-
this._processing = false;
99-
})
100-
.then(null, reason => {
101-
logger.error(reason);
102-
this._processing = false;
94+
.then(event => {
95+
eventId = this.captureEvent(event, hint, scope);
10396
});
10497

10598
return eventId;
@@ -110,24 +103,15 @@ export abstract class BaseClient<B extends Backend, O extends Options> implement
110103
*/
111104
public captureMessage(message: string, level?: Severity, hint?: EventHint, scope?: Scope): string | undefined {
112105
let eventId: string | undefined = hint && hint.event_id;
113-
114106
this._processing = true;
115107

116108
const promisedEvent = isPrimitive(message)
117109
? this._getBackend().eventFromMessage(`${message}`, level, hint)
118110
: this._getBackend().eventFromException(message, hint);
119111

120-
promisedEvent
121-
.then(event => this._processEvent(event, hint, scope))
122-
.then(finalEvent => {
123-
// We need to check for finalEvent in case beforeSend returned null
124-
eventId = finalEvent && finalEvent.event_id;
125-
this._processing = false;
126-
})
127-
.then(null, reason => {
128-
logger.error(reason);
129-
this._processing = false;
130-
});
112+
promisedEvent.then(event => {
113+
eventId = this.captureEvent(event, hint, scope);
114+
});
131115

132116
return eventId;
133117
}
@@ -372,6 +356,14 @@ export abstract class BaseClient<B extends Backend, O extends Options> implement
372356
}
373357
}
374358

359+
/**
360+
* Tells the backend to send this event
361+
* @param event The Sentry event to send
362+
*/
363+
protected _sendEvent(event: Event): void {
364+
this._getBackend().sendEvent(event);
365+
}
366+
375367
/**
376368
* Processes an event (either error or message) and sends it to Sentry.
377369
*
@@ -413,7 +405,7 @@ export abstract class BaseClient<B extends Backend, O extends Options> implement
413405
const isInternalException = hint && hint.data && (hint.data as { [key: string]: any }).__sentry__ === true;
414406
// We skip beforeSend in case of transactions
415407
if (isInternalException || !beforeSend || isTransaction) {
416-
this._getBackend().sendEvent(finalEvent);
408+
this._sendEvent(finalEvent);
417409
resolve(finalEvent);
418410
return;
419411
}
@@ -434,7 +426,7 @@ export abstract class BaseClient<B extends Backend, O extends Options> implement
434426
}
435427

436428
// From here on we are really async
437-
this._getBackend().sendEvent(finalEvent);
429+
this._sendEvent(finalEvent);
438430
resolve(finalEvent);
439431
}
440432
})
@@ -467,7 +459,7 @@ export abstract class BaseClient<B extends Backend, O extends Options> implement
467459
return;
468460
}
469461
// From here on we are really async
470-
this._getBackend().sendEvent(processedEvent);
462+
this._sendEvent(processedEvent);
471463
resolve(processedEvent);
472464
})
473465
.then(null, e => {

0 commit comments

Comments
 (0)