|
1 | 1 | import { BrowserMicroSentryClient } from './browser-micro-sentry-client';
|
2 | 2 | import { Severity } from '@micro-sentry/core';
|
| 3 | +import { MAX_BREADCRUMBS } from '../consts/max-breadcrumbs'; |
3 | 4 |
|
4 | 5 | describe('BrowserMicroSentryClient', () => {
|
5 | 6 | let client: BrowserMicroSentryClient;
|
| 7 | + const maxBreadcrumbs = 10; |
| 8 | + const getBreadcrumbs = (amount: number) => |
| 9 | + [...Array(amount).keys()].map((index) => ({ |
| 10 | + event_id: `id${index}`, |
| 11 | + type: 'console', |
| 12 | + level: Severity.critical, |
| 13 | + })); |
6 | 14 |
|
7 | 15 | beforeAll(() => {
|
8 | 16 | client = new BrowserMicroSentryClient({
|
9 | 17 | dsn: 'http://[email protected]/2',
|
10 | 18 | release: '1.0.0',
|
| 19 | + maxBreadcrumbs, |
11 | 20 | });
|
12 | 21 | });
|
13 | 22 |
|
@@ -205,6 +214,97 @@ describe('BrowserMicroSentryClient', () => {
|
205 | 214 | });
|
206 | 215 | });
|
207 | 216 |
|
| 217 | + it('should limit breadcrumbs amount - with custom limit; incremental addition', () => { |
| 218 | + getBreadcrumbs(maxBreadcrumbs + 2).forEach((bc) => |
| 219 | + client.addBreadcrumb(bc) |
| 220 | + ); |
| 221 | + |
| 222 | + expect(client.state.breadcrumbs?.length).toBe(maxBreadcrumbs); |
| 223 | + }); |
| 224 | + |
| 225 | + it('should limit breadcrumbs amount - with custom limit; all at once', () => { |
| 226 | + client.setBreadcrumbs(getBreadcrumbs(maxBreadcrumbs + 2)); |
| 227 | + |
| 228 | + expect(client.state.breadcrumbs?.length).toBe(maxBreadcrumbs); |
| 229 | + }); |
| 230 | + |
| 231 | + it('should limit breadcrumbs amount - with default limit; incremental addition', () => { |
| 232 | + const anotherClient = new BrowserMicroSentryClient({}); |
| 233 | + |
| 234 | + getBreadcrumbs(MAX_BREADCRUMBS + 2).forEach((bc) => |
| 235 | + anotherClient.addBreadcrumb(bc) |
| 236 | + ); |
| 237 | + |
| 238 | + expect(anotherClient.state.breadcrumbs?.length).toBe(MAX_BREADCRUMBS); |
| 239 | + }); |
| 240 | + |
| 241 | + it('should limit breadcrumbs amount - with default limit; all at once', () => { |
| 242 | + const anotherClient = new BrowserMicroSentryClient({}); |
| 243 | + anotherClient.setBreadcrumbs(getBreadcrumbs(MAX_BREADCRUMBS + 2)); |
| 244 | + |
| 245 | + expect(anotherClient.state.breadcrumbs?.length).toBe(MAX_BREADCRUMBS); |
| 246 | + }); |
| 247 | + |
| 248 | + it('should save only last breadcrumbs; incremental addition', () => { |
| 249 | + getBreadcrumbs(maxBreadcrumbs + 2).forEach((bc) => |
| 250 | + client.addBreadcrumb(bc) |
| 251 | + ); |
| 252 | + |
| 253 | + expect( |
| 254 | + (client.state.breadcrumbs ?? []) |
| 255 | + .map((breadcrumb) => breadcrumb.event_id) |
| 256 | + .join(',') |
| 257 | + ).toEqual('id2,id3,id4,id5,id6,id7,id8,id9,id10,id11'); |
| 258 | + }); |
| 259 | + |
| 260 | + it('should save only last breadcrumbs; all at once', () => { |
| 261 | + client.setBreadcrumbs(getBreadcrumbs(maxBreadcrumbs + 2)); |
| 262 | + |
| 263 | + expect( |
| 264 | + (client.state.breadcrumbs ?? []) |
| 265 | + .map((breadcrumb) => breadcrumb.event_id) |
| 266 | + .join(',') |
| 267 | + ).toEqual('id2,id3,id4,id5,id6,id7,id8,id9,id10,id11'); |
| 268 | + }); |
| 269 | + |
| 270 | + it('should not add breadcrumbs at all if maxBreadcrumbs is set to 0; incremental addition', () => { |
| 271 | + const anotherClient = new BrowserMicroSentryClient({ maxBreadcrumbs: 0 }); |
| 272 | + |
| 273 | + getBreadcrumbs(1).forEach((bc) => anotherClient.addBreadcrumb(bc)); |
| 274 | + |
| 275 | + expect(anotherClient.state.breadcrumbs?.length).toBe(0); |
| 276 | + }); |
| 277 | + |
| 278 | + it('should not add breadcrumbs at all if maxBreadcrumbs is set to 0; all at once', () => { |
| 279 | + const anotherClient = new BrowserMicroSentryClient({ maxBreadcrumbs: 0 }); |
| 280 | + |
| 281 | + anotherClient.setBreadcrumbs(getBreadcrumbs(1)); |
| 282 | + |
| 283 | + expect(anotherClient.state.breadcrumbs?.length).toBe(0); |
| 284 | + }); |
| 285 | + |
| 286 | + it('should ignore maxBreadcrumbs option if maxBreadcrumbs is a negative number; incremental addition', () => { |
| 287 | + const anotherClient = new BrowserMicroSentryClient({ |
| 288 | + maxBreadcrumbs: -100, |
| 289 | + }); |
| 290 | + |
| 291 | + getBreadcrumbs(MAX_BREADCRUMBS + 2).forEach((bc) => |
| 292 | + anotherClient.addBreadcrumb(bc) |
| 293 | + ); |
| 294 | + |
| 295 | + expect(anotherClient.state.breadcrumbs?.length).toBe(MAX_BREADCRUMBS); |
| 296 | + }); |
| 297 | + |
| 298 | + it('should ignore maxBreadcrumbs option if maxBreadcrumbs is a negative number; all at once', () => { |
| 299 | + const anotherClient = new BrowserMicroSentryClient({ |
| 300 | + maxBreadcrumbs: -100, |
| 301 | + }); |
| 302 | + |
| 303 | + anotherClient.setBreadcrumbs(getBreadcrumbs(MAX_BREADCRUMBS + 2)); |
| 304 | + |
| 305 | + expect(anotherClient.state.breadcrumbs?.length).toBe(MAX_BREADCRUMBS); |
| 306 | + }); |
| 307 | + |
208 | 308 | it('should skip breadcrumbs if beforeBreadcrumb returns null', () => {
|
209 | 309 | client = new BrowserMicroSentryClient({
|
210 | 310 | dsn: 'http://[email protected]/2',
|
@@ -237,7 +337,7 @@ describe('BrowserMicroSentryClient', () => {
|
237 | 337 | ]);
|
238 | 338 | });
|
239 | 339 |
|
240 |
| - afterAll(() => { |
| 340 | + afterEach(() => { |
241 | 341 | client.clearState();
|
242 | 342 | });
|
243 | 343 | });
|
|
0 commit comments