Skip to content

Commit 80efec1

Browse files
committed
test(core): Add unit test for the updated log level function
Signed-off-by: Kaung Zin Hein <[email protected]>
1 parent fbcc140 commit 80efec1

File tree

4 files changed

+28
-18
lines changed

4 files changed

+28
-18
lines changed

packages/core/src/breadcrumbs.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import type { Breadcrumb, BreadcrumbHint } from '@sentry/types';
22
import { consoleSandbox, dateTimestampInSeconds } from '@sentry/utils';
33
import { getClient, getIsolationScope } from './currentScopes';
4-
import { assignBreadcrumbLogLevel } from './utils/breadcrumbsUtils';
54

65
/**
76
* Default maximum number of breadcrumbs added to an event. Can be overwritten
@@ -26,7 +25,7 @@ export function addBreadcrumb(breadcrumb: Breadcrumb, hint?: BreadcrumbHint): vo
2625
if (maxBreadcrumbs <= 0) return;
2726

2827
const timestamp = dateTimestampInSeconds();
29-
const mergedBreadcrumb = { timestamp, ...assignBreadcrumbLogLevel(breadcrumb) };
28+
const mergedBreadcrumb = { timestamp, breadcrumb };
3029
const finalBreadcrumb = beforeBreadcrumb
3130
? (consoleSandbox(() => beforeBreadcrumb(mergedBreadcrumb, hint)) as Breadcrumb | null)
3231
: mergedBreadcrumb;

packages/core/src/scope.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import type {
2424
import { dateTimestampInSeconds, generatePropagationContext, isPlainObject, logger, uuid4 } from '@sentry/utils';
2525

2626
import { updateSession } from './session';
27-
import { assignBreadcrumbLogLevel } from './utils/breadcrumbsUtils';
2827
import { _getSpanForScope, _setSpanForScope } from './utils/spanOnScope';
2928

3029
/**
@@ -413,7 +412,7 @@ class ScopeClass implements ScopeInterface {
413412

414413
const mergedBreadcrumb = {
415414
timestamp: dateTimestampInSeconds(),
416-
...assignBreadcrumbLogLevel(breadcrumb),
415+
breadcrumb,
417416
};
418417

419418
const breadcrumbs = this._breadcrumbs;
Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,17 @@
1-
import type { Breadcrumb } from '@sentry/types';
1+
import type { SeverityLevel } from '@sentry/types';
22

33
/**
4-
* Set a Fetch/XHR breadcrumb's log level based on the returned status code
5-
* @param breadcrumb
4+
* Determine a breadcrumb's log level based on the response status code
5+
* @param statusCode
66
*/
7-
export function assignBreadcrumbLogLevel(breadcrumb: Breadcrumb): Breadcrumb {
8-
const statusCode = breadcrumb.data?.status_code;
9-
if (typeof statusCode !== 'number') {
10-
return breadcrumb;
11-
}
12-
13-
if (statusCode >= 400 && statusCode < 500) {
14-
breadcrumb.level = 'warning';
7+
export function getBreadcrumbLogLevel(statusCode: number | undefined): SeverityLevel {
8+
if (statusCode === undefined) {
9+
return 'info';
10+
} else if (statusCode >= 400 && statusCode < 500) {
11+
return 'warning';
1512
} else if (statusCode >= 500) {
16-
breadcrumb.level = 'error';
13+
return 'error';
14+
} else {
15+
return 'info';
1716
}
18-
19-
return breadcrumb;
2017
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import type { SeverityLevel } from '@sentry/types';
2+
import { getBreadcrumbLogLevel } from '../../../src/utils/breadcrumbsUtils';
3+
4+
describe('getBreadcrumbLogLevel()', () => {
5+
it.each([
6+
['warning', '4xx', 403],
7+
['error', '5xx', 500],
8+
['info', '3xx', 307],
9+
['info', '2xx', 200],
10+
['info', '1xx', 103],
11+
['info', 'undefined', undefined],
12+
] as [SeverityLevel, string, number | undefined][])('should return `%s` for %s', (output, _codeRange, input) => {
13+
expect(getBreadcrumbLogLevel(input)).toBe(output);
14+
});
15+
});

0 commit comments

Comments
 (0)