|
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,67 @@ 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 | + const requestSession = (scope as any)._requestSession; |
| 246 | + requestSession.status = RequestSessionStatus.Ok; |
| 247 | + client.captureException(new Error('test exception'), {}, scope); |
| 248 | + expect(requestSession).toEqual({ status: RequestSessionStatus.Ok }); |
| 249 | + }); |
| 250 | + |
| 251 | + test('Does not set requestSession status when an exception occurs outside of a request', () => { |
| 252 | + const client = new TestClient({ dsn: PUBLIC_DSN, autoSessionTracking: true }); |
| 253 | + const scope = new Scope(); |
| 254 | + client.captureException(new Error('test exception'), {}, scope); |
| 255 | + expect((scope as any)._requestSession).toEqual({}); |
| 256 | + }); |
| 257 | + |
| 258 | + test('Sets requestSession status to Errored when an exception occurs within a request', () => { |
| 259 | + const client = new TestClient({ dsn: PUBLIC_DSN, autoSessionTracking: true }); |
| 260 | + const scope = new Scope(); |
| 261 | + const requestSession = (scope as any)._requestSession; |
| 262 | + requestSession.status = RequestSessionStatus.Ok; |
| 263 | + client.captureException(new Error('test exception'), {}, scope); |
| 264 | + expect(requestSession).toEqual({ status: RequestSessionStatus.Errored }); |
| 265 | + }); |
| 266 | + |
| 267 | + test('Sets requestSession status to Crashed when an onuncaughtexception occurs within a request', () => { |
| 268 | + const client = new TestClient({ dsn: PUBLIC_DSN, autoSessionTracking: true }); |
| 269 | + const scope = new Scope(); |
| 270 | + const requestSession = (scope as any)._requestSession; |
| 271 | + requestSession.status = RequestSessionStatus.Ok; |
| 272 | + scope.setExtra('onUncaughtException', true); |
| 273 | + client.captureException(new Error('test exception'), {}, scope); |
| 274 | + expect(requestSession).toEqual({ status: RequestSessionStatus.Crashed }); |
| 275 | + }); |
| 276 | + |
| 277 | + test('Does not set requestSession status when an onunhandledrejection occurs outside a request', () => { |
| 278 | + const client = new TestClient({ dsn: PUBLIC_DSN, autoSessionTracking: true }); |
| 279 | + const scope = new Scope(); |
| 280 | + const requestSession = (scope as any)._requestSession; |
| 281 | + requestSession.status = RequestSessionStatus.Ok; |
| 282 | + scope.setExtra('unhandledPromiseRejection', true); |
| 283 | + client.captureException(new Error('test exception'), {}, scope); |
| 284 | + expect(requestSession).toEqual({ status: RequestSessionStatus.Crashed }); |
| 285 | + }); |
| 286 | + |
| 287 | + test('Does not set requestSession status when an onuncaughtexception occurs outside a request', () => { |
| 288 | + const client = new TestClient({ dsn: PUBLIC_DSN, autoSessionTracking: true }); |
| 289 | + const scope = new Scope(); |
| 290 | + scope.setExtra('onUncaughtException', true); |
| 291 | + client.captureException(new Error('test exception'), {}, scope); |
| 292 | + expect((scope as any)._requestSession).toEqual({}); |
| 293 | + }); |
| 294 | + |
| 295 | + test('Sets requestSession status to Crashed when an onunhandledrejection occurs within a request', () => { |
| 296 | + const client = new TestClient({ dsn: PUBLIC_DSN, autoSessionTracking: true }); |
| 297 | + const scope = new Scope(); |
| 298 | + scope.setExtra('unhandledPromiseRejection', true); |
| 299 | + client.captureException(new Error('test exception'), {}, scope); |
| 300 | + expect((scope as any)._requestSession).toEqual({}); |
| 301 | + }); |
241 | 302 | }); |
242 | 303 |
|
243 | 304 | describe('captureMessage', () => { |
@@ -477,6 +538,49 @@ describe('BaseClient', () => { |
477 | 538 | }); |
478 | 539 | }); |
479 | 540 |
|
| 541 | + test('If autoSessionTracking is disabled, requestSession status should not be set', () => { |
| 542 | + const client = new TestClient({ dsn: PUBLIC_DSN, autoSessionTracking: false }); |
| 543 | + const scope = new Scope(); |
| 544 | + const requestSession = (scope as any)._requestSession; |
| 545 | + requestSession.status = RequestSessionStatus.Ok; |
| 546 | + client.captureEvent({ message: 'message' }, undefined, scope); |
| 547 | + expect(requestSession).toEqual({ status: RequestSessionStatus.Ok }); |
| 548 | + }); |
| 549 | + |
| 550 | + test('When captureEvent is called with an exception, requestSession status should be set to Errored', () => { |
| 551 | + const client = new TestClient({ dsn: PUBLIC_DSN, autoSessionTracking: true }); |
| 552 | + const scope = new Scope(); |
| 553 | + const requestSession = (scope as any)._requestSession; |
| 554 | + requestSession.status = RequestSessionStatus.Ok; |
| 555 | + client.captureEvent({ message: 'message' }, undefined, scope); |
| 556 | + expect(requestSession).toEqual({ status: RequestSessionStatus.Errored }); |
| 557 | + }); |
| 558 | + |
| 559 | + test('When captureEvent is called with an exception and, requestSession status is Crashed, it should not be overridden', () => { |
| 560 | + const client = new TestClient({ dsn: PUBLIC_DSN, autoSessionTracking: true }); |
| 561 | + const scope = new Scope(); |
| 562 | + const requestSession = (scope as any)._requestSession; |
| 563 | + requestSession.status = RequestSessionStatus.Crashed; |
| 564 | + client.captureEvent({ message: 'message' }, undefined, scope); |
| 565 | + expect(requestSession).toEqual({ status: RequestSessionStatus.Crashed }); |
| 566 | + }); |
| 567 | + |
| 568 | + test('When captureEvent is called with an exception but outside of a request, then requestStatus should not be set', () => { |
| 569 | + const client = new TestClient({ dsn: PUBLIC_DSN, autoSessionTracking: true }); |
| 570 | + const scope = new Scope(); |
| 571 | + client.captureEvent({ message: 'message' }, undefined, scope); |
| 572 | + expect((scope as any)._requestSession).toEqual({}); |
| 573 | + }); |
| 574 | + |
| 575 | + test('When captureEvent is called with a transaction, then requestStatus should not be set', () => { |
| 576 | + const client = new TestClient({ dsn: PUBLIC_DSN, autoSessionTracking: true }); |
| 577 | + const scope = new Scope(); |
| 578 | + const requestSession = (scope as any)._requestSession; |
| 579 | + requestSession.status = RequestSessionStatus.Ok; |
| 580 | + client.captureEvent({ message: 'message', type: 'transaction' }, undefined, scope); |
| 581 | + expect(requestSession).toEqual({ status: RequestSessionStatus.Ok }); |
| 582 | + }); |
| 583 | + |
480 | 584 | test('normalizes event with default depth of 3', () => { |
481 | 585 | expect.assertions(1); |
482 | 586 | const client = new TestClient({ dsn: PUBLIC_DSN }); |
|
0 commit comments