|
1 | 1 | import { Hub, Scope } from '@sentry/hub'; |
2 | | -import { Event, Severity, Span } from '@sentry/types'; |
| 2 | +import { Event, RequestSessionStatus, Severity, Span } from '@sentry/types'; |
3 | 3 | import { logger, SentryError, SyncPromise } from '@sentry/utils'; |
4 | 4 |
|
5 | 5 | import { TestBackend } from '../mocks/backend'; |
@@ -238,6 +238,63 @@ describe('BaseClient', () => { |
238 | 238 | }), |
239 | 239 | ); |
240 | 240 | }); |
| 241 | + |
| 242 | + test('when autoSessionTracking is disabled, does not set requestSession status', () => { |
| 243 | + const client = new TestClient({ dsn: PUBLIC_DSN, autoSessionTracking: false }); |
| 244 | + const scope = new Scope(); |
| 245 | + scope.setRequestSession(RequestSessionStatus.Ok); |
| 246 | + client.captureException(new Error('test exception'), {}, scope); |
| 247 | + expect(scope.getRequestSession()).toEqual({ status: RequestSessionStatus.Ok }); |
| 248 | + }); |
| 249 | + |
| 250 | + test('Does not set requestSession status when an exception occurs outside of a request', () => { |
| 251 | + const client = new TestClient({ dsn: PUBLIC_DSN, autoSessionTracking: true }); |
| 252 | + const scope = new Scope(); |
| 253 | + client.captureException(new Error('test exception'), {}, scope); |
| 254 | + expect(scope.getRequestSession()).toEqual({}); |
| 255 | + }); |
| 256 | + |
| 257 | + test('Sets requestSession status to Errored when an exception occurs within a request', () => { |
| 258 | + const client = new TestClient({ dsn: PUBLIC_DSN, autoSessionTracking: true }); |
| 259 | + const scope = new Scope(); |
| 260 | + scope.setRequestSession(RequestSessionStatus.Ok); |
| 261 | + client.captureException(new Error('test exception'), {}, scope); |
| 262 | + expect(scope.getRequestSession()).toEqual({ status: RequestSessionStatus.Errored }); |
| 263 | + }); |
| 264 | + |
| 265 | + test('Sets requestSession status to Crashed when an onuncaughtexception occurs within a request', () => { |
| 266 | + const client = new TestClient({ dsn: PUBLIC_DSN, autoSessionTracking: true }); |
| 267 | + const scope = new Scope(); |
| 268 | + scope.setRequestSession(RequestSessionStatus.Ok); |
| 269 | + scope.setExtra('onUncaughtException', true); |
| 270 | + client.captureException(new Error('test exception'), {}, scope); |
| 271 | + expect(scope.getRequestSession()).toEqual({ status: RequestSessionStatus.Crashed }); |
| 272 | + }); |
| 273 | + |
| 274 | + test('Does not set requestSession status when an onunhandledrejection occurs outside a request', () => { |
| 275 | + const client = new TestClient({ dsn: PUBLIC_DSN, autoSessionTracking: true }); |
| 276 | + const scope = new Scope(); |
| 277 | + scope.setRequestSession(RequestSessionStatus.Ok); |
| 278 | + scope.setExtra('unhandledPromiseRejection', true); |
| 279 | + client.captureException(new Error('test exception'), {}, scope); |
| 280 | + expect(scope.getRequestSession()).toEqual({ status: RequestSessionStatus.Crashed }); |
| 281 | + }); |
| 282 | + |
| 283 | + test('Does not set requestSession status when an onuncaughtexception occurs outside a request', () => { |
| 284 | + const client = new TestClient({ dsn: PUBLIC_DSN, autoSessionTracking: true }); |
| 285 | + const scope = new Scope(); |
| 286 | + scope.setExtra('onUncaughtException', true); |
| 287 | + client.captureException(new Error('test exception'), {}, scope); |
| 288 | + expect(scope.getRequestSession()).toEqual({}); |
| 289 | + }); |
| 290 | + |
| 291 | + test('Sets requestSession status to Crashed when an onunhandledrejection occurs within a request', () => { |
| 292 | + const client = new TestClient({ dsn: PUBLIC_DSN, autoSessionTracking: true }); |
| 293 | + const scope = new Scope(); |
| 294 | + scope.setExtra('unhandledPromiseRejection', true); |
| 295 | + client.captureException(new Error('test exception'), {}, scope); |
| 296 | + expect(scope.getRequestSession()).toEqual({}); |
| 297 | + }); |
241 | 298 | }); |
242 | 299 |
|
243 | 300 | describe('captureMessage', () => { |
@@ -477,6 +534,45 @@ describe('BaseClient', () => { |
477 | 534 | }); |
478 | 535 | }); |
479 | 536 |
|
| 537 | + test('If autoSessionTracking is disabled, requestSession status should not be set', () => { |
| 538 | + const client = new TestClient({ dsn: PUBLIC_DSN, autoSessionTracking: false }); |
| 539 | + const scope = new Scope(); |
| 540 | + scope.setRequestSession(RequestSessionStatus.Ok); |
| 541 | + client.captureEvent({ message: 'message' }, undefined, scope); |
| 542 | + expect(scope.getRequestSession()).toEqual({ status: RequestSessionStatus.Ok }); |
| 543 | + }); |
| 544 | + |
| 545 | + test('When captureEvent is called with an exception, requestSession status should be set to Errored', () => { |
| 546 | + const client = new TestClient({ dsn: PUBLIC_DSN, autoSessionTracking: true }); |
| 547 | + const scope = new Scope(); |
| 548 | + scope.setRequestSession(RequestSessionStatus.Ok); |
| 549 | + client.captureEvent({ message: 'message' }, undefined, scope); |
| 550 | + expect(scope.getRequestSession()).toEqual({ status: RequestSessionStatus.Errored }); |
| 551 | + }); |
| 552 | + |
| 553 | + test('When captureEvent is called with an exception and, requestSession status is Crashed, it should not be overridden', () => { |
| 554 | + const client = new TestClient({ dsn: PUBLIC_DSN, autoSessionTracking: true }); |
| 555 | + const scope = new Scope(); |
| 556 | + scope.setRequestSession(RequestSessionStatus.Crashed); |
| 557 | + client.captureEvent({ message: 'message' }, undefined, scope); |
| 558 | + expect(scope.getRequestSession()).toEqual({ status: RequestSessionStatus.Crashed }); |
| 559 | + }); |
| 560 | + |
| 561 | + test('When captureEvent is called with an exception but outside of a request, then requestStatus should not be set', () => { |
| 562 | + const client = new TestClient({ dsn: PUBLIC_DSN, autoSessionTracking: true }); |
| 563 | + const scope = new Scope(); |
| 564 | + client.captureEvent({ message: 'message' }, undefined, scope); |
| 565 | + expect(scope.getRequestSession()).toEqual({}); |
| 566 | + }); |
| 567 | + |
| 568 | + test('When captureEvent is called with a transaction, then requestStatus should not be set', () => { |
| 569 | + const client = new TestClient({ dsn: PUBLIC_DSN, autoSessionTracking: true }); |
| 570 | + const scope = new Scope(); |
| 571 | + scope.setRequestSession(RequestSessionStatus.Ok); |
| 572 | + client.captureEvent({ message: 'message', type: 'transaction' }, undefined, scope); |
| 573 | + expect(scope.getRequestSession()).toEqual({ status: RequestSessionStatus.Ok }); |
| 574 | + }); |
| 575 | + |
480 | 576 | test('normalizes event with default depth of 3', () => { |
481 | 577 | expect.assertions(1); |
482 | 578 | const client = new TestClient({ dsn: PUBLIC_DSN }); |
|
0 commit comments