-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: add max breadcrumbs limitation (#28)
* feat: add max breadcrumbs limitation * feat: add breadcrumbs limitation to setBreadcrumbs method
- Loading branch information
Showing
4 changed files
with
129 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const MAX_BREADCRUMBS = 100; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,22 @@ | ||
import { BrowserMicroSentryClient } from './browser-micro-sentry-client'; | ||
import { Severity } from '@micro-sentry/core'; | ||
import { MAX_BREADCRUMBS } from '../consts/max-breadcrumbs'; | ||
|
||
describe('BrowserMicroSentryClient', () => { | ||
let client: BrowserMicroSentryClient; | ||
const maxBreadcrumbs = 10; | ||
const getBreadcrumbs = (amount: number) => | ||
[...Array(amount).keys()].map((index) => ({ | ||
event_id: `id${index}`, | ||
type: 'console', | ||
level: Severity.critical, | ||
})); | ||
|
||
beforeAll(() => { | ||
client = new BrowserMicroSentryClient({ | ||
dsn: 'http://[email protected]/2', | ||
release: '1.0.0', | ||
maxBreadcrumbs, | ||
}); | ||
}); | ||
|
||
|
@@ -205,6 +214,97 @@ describe('BrowserMicroSentryClient', () => { | |
}); | ||
}); | ||
|
||
it('should limit breadcrumbs amount - with custom limit; incremental addition', () => { | ||
getBreadcrumbs(maxBreadcrumbs + 2).forEach((bc) => | ||
client.addBreadcrumb(bc) | ||
); | ||
|
||
expect(client.state.breadcrumbs?.length).toBe(maxBreadcrumbs); | ||
}); | ||
|
||
it('should limit breadcrumbs amount - with custom limit; all at once', () => { | ||
client.setBreadcrumbs(getBreadcrumbs(maxBreadcrumbs + 2)); | ||
|
||
expect(client.state.breadcrumbs?.length).toBe(maxBreadcrumbs); | ||
}); | ||
|
||
it('should limit breadcrumbs amount - with default limit; incremental addition', () => { | ||
const anotherClient = new BrowserMicroSentryClient({}); | ||
|
||
getBreadcrumbs(MAX_BREADCRUMBS + 2).forEach((bc) => | ||
anotherClient.addBreadcrumb(bc) | ||
); | ||
|
||
expect(anotherClient.state.breadcrumbs?.length).toBe(MAX_BREADCRUMBS); | ||
}); | ||
|
||
it('should limit breadcrumbs amount - with default limit; all at once', () => { | ||
const anotherClient = new BrowserMicroSentryClient({}); | ||
anotherClient.setBreadcrumbs(getBreadcrumbs(MAX_BREADCRUMBS + 2)); | ||
|
||
expect(anotherClient.state.breadcrumbs?.length).toBe(MAX_BREADCRUMBS); | ||
}); | ||
|
||
it('should save only last breadcrumbs; incremental addition', () => { | ||
getBreadcrumbs(maxBreadcrumbs + 2).forEach((bc) => | ||
client.addBreadcrumb(bc) | ||
); | ||
|
||
expect( | ||
(client.state.breadcrumbs ?? []) | ||
.map((breadcrumb) => breadcrumb.event_id) | ||
.join(',') | ||
).toEqual('id2,id3,id4,id5,id6,id7,id8,id9,id10,id11'); | ||
}); | ||
|
||
it('should save only last breadcrumbs; all at once', () => { | ||
client.setBreadcrumbs(getBreadcrumbs(maxBreadcrumbs + 2)); | ||
|
||
expect( | ||
(client.state.breadcrumbs ?? []) | ||
.map((breadcrumb) => breadcrumb.event_id) | ||
.join(',') | ||
).toEqual('id2,id3,id4,id5,id6,id7,id8,id9,id10,id11'); | ||
}); | ||
|
||
it('should not add breadcrumbs at all if maxBreadcrumbs is set to 0; incremental addition', () => { | ||
const anotherClient = new BrowserMicroSentryClient({ maxBreadcrumbs: 0 }); | ||
|
||
getBreadcrumbs(1).forEach((bc) => anotherClient.addBreadcrumb(bc)); | ||
|
||
expect(anotherClient.state.breadcrumbs?.length).toBe(0); | ||
}); | ||
|
||
it('should not add breadcrumbs at all if maxBreadcrumbs is set to 0; all at once', () => { | ||
const anotherClient = new BrowserMicroSentryClient({ maxBreadcrumbs: 0 }); | ||
|
||
anotherClient.setBreadcrumbs(getBreadcrumbs(1)); | ||
|
||
expect(anotherClient.state.breadcrumbs?.length).toBe(0); | ||
}); | ||
|
||
it('should ignore maxBreadcrumbs option if maxBreadcrumbs is a negative number; incremental addition', () => { | ||
const anotherClient = new BrowserMicroSentryClient({ | ||
maxBreadcrumbs: -100, | ||
}); | ||
|
||
getBreadcrumbs(MAX_BREADCRUMBS + 2).forEach((bc) => | ||
anotherClient.addBreadcrumb(bc) | ||
); | ||
|
||
expect(anotherClient.state.breadcrumbs?.length).toBe(MAX_BREADCRUMBS); | ||
}); | ||
|
||
it('should ignore maxBreadcrumbs option if maxBreadcrumbs is a negative number; all at once', () => { | ||
const anotherClient = new BrowserMicroSentryClient({ | ||
maxBreadcrumbs: -100, | ||
}); | ||
|
||
anotherClient.setBreadcrumbs(getBreadcrumbs(MAX_BREADCRUMBS + 2)); | ||
|
||
expect(anotherClient.state.breadcrumbs?.length).toBe(MAX_BREADCRUMBS); | ||
}); | ||
|
||
it('should skip breadcrumbs if beforeBreadcrumb returns null', () => { | ||
client = new BrowserMicroSentryClient({ | ||
dsn: 'http://[email protected]/2', | ||
|
@@ -237,7 +337,7 @@ describe('BrowserMicroSentryClient', () => { | |
]); | ||
}); | ||
|
||
afterAll(() => { | ||
afterEach(() => { | ||
client.clearState(); | ||
}); | ||
}); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters