|
| 1 | +/** |
| 2 | + * @vitest-environment jsdom |
| 3 | + */ |
| 4 | + |
| 5 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 6 | +import * as sentryCore from '@sentry/core'; |
| 7 | +import { BrowserClient } from '../src/client'; |
| 8 | +import { WINDOW } from '../src/helpers'; |
| 9 | +import { getDefaultBrowserClientOptions } from './helper/browser-client-options'; |
| 10 | + |
| 11 | +vi.mock('@sentry/core', async requireActual => { |
| 12 | + return { |
| 13 | + ...((await requireActual()) as any), |
| 14 | + _INTERNAL_flushLogsBuffer: vi.fn(), |
| 15 | + }; |
| 16 | +}); |
| 17 | + |
| 18 | +describe('BrowserClient', () => { |
| 19 | + let client: BrowserClient; |
| 20 | + const DEFAULT_FLUSH_INTERVAL = 5000; |
| 21 | + |
| 22 | + afterEach(() => { |
| 23 | + vi.useRealTimers(); |
| 24 | + vi.clearAllMocks(); |
| 25 | + }); |
| 26 | + |
| 27 | + it('does not flush logs when logs are disabled', () => { |
| 28 | + client = new BrowserClient( |
| 29 | + getDefaultBrowserClientOptions({ |
| 30 | + _experiments: { enableLogs: false }, |
| 31 | + sendClientReports: true, |
| 32 | + }), |
| 33 | + ); |
| 34 | + |
| 35 | + // Add some logs |
| 36 | + sentryCore._INTERNAL_captureLog({ level: 'info', message: 'test log 1' }, client); |
| 37 | + sentryCore._INTERNAL_captureLog({ level: 'info', message: 'test log 2' }, client); |
| 38 | + |
| 39 | + // Simulate visibility change to hidden |
| 40 | + if (WINDOW.document) { |
| 41 | + Object.defineProperty(WINDOW.document, 'visibilityState', { value: 'hidden' }); |
| 42 | + WINDOW.document.dispatchEvent(new Event('visibilitychange')); |
| 43 | + } |
| 44 | + |
| 45 | + expect(sentryCore._INTERNAL_flushLogsBuffer).not.toHaveBeenCalled(); |
| 46 | + }); |
| 47 | + |
| 48 | + describe('log flushing', () => { |
| 49 | + beforeEach(() => { |
| 50 | + vi.useFakeTimers(); |
| 51 | + client = new BrowserClient( |
| 52 | + getDefaultBrowserClientOptions({ |
| 53 | + _experiments: { enableLogs: true }, |
| 54 | + sendClientReports: true, |
| 55 | + }), |
| 56 | + ); |
| 57 | + }); |
| 58 | + |
| 59 | + it('flushes logs when page visibility changes to hidden', () => { |
| 60 | + const flushOutcomesSpy = vi.spyOn(client as any, '_flushOutcomes'); |
| 61 | + |
| 62 | + // Add some logs |
| 63 | + sentryCore._INTERNAL_captureLog({ level: 'info', message: 'test log 1' }, client); |
| 64 | + sentryCore._INTERNAL_captureLog({ level: 'info', message: 'test log 2' }, client); |
| 65 | + |
| 66 | + // Simulate visibility change to hidden |
| 67 | + if (WINDOW.document) { |
| 68 | + Object.defineProperty(WINDOW.document, 'visibilityState', { value: 'hidden' }); |
| 69 | + WINDOW.document.dispatchEvent(new Event('visibilitychange')); |
| 70 | + } |
| 71 | + |
| 72 | + expect(flushOutcomesSpy).toHaveBeenCalled(); |
| 73 | + expect(sentryCore._INTERNAL_flushLogsBuffer).toHaveBeenCalledWith(client); |
| 74 | + }); |
| 75 | + |
| 76 | + it('flushes logs on flush event', () => { |
| 77 | + // Add some logs |
| 78 | + sentryCore._INTERNAL_captureLog({ level: 'info', message: 'test log 1' }, client); |
| 79 | + sentryCore._INTERNAL_captureLog({ level: 'info', message: 'test log 2' }, client); |
| 80 | + |
| 81 | + // Trigger flush event |
| 82 | + client.emit('flush'); |
| 83 | + |
| 84 | + expect(sentryCore._INTERNAL_flushLogsBuffer).toHaveBeenCalledWith(client); |
| 85 | + }); |
| 86 | + |
| 87 | + it('flushes logs after idle timeout', () => { |
| 88 | + // Add a log which will trigger afterCaptureLog event |
| 89 | + sentryCore._INTERNAL_captureLog({ level: 'info', message: 'test log' }, client); |
| 90 | + |
| 91 | + // Fast forward the idle timeout |
| 92 | + vi.advanceTimersByTime(DEFAULT_FLUSH_INTERVAL); |
| 93 | + |
| 94 | + expect(sentryCore._INTERNAL_flushLogsBuffer).toHaveBeenCalledWith(client); |
| 95 | + }); |
| 96 | + |
| 97 | + it('resets idle timeout when new logs are captured', () => { |
| 98 | + // Add initial log |
| 99 | + sentryCore._INTERNAL_captureLog({ level: 'info', message: 'test log 1' }, client); |
| 100 | + |
| 101 | + // Fast forward part of the idle timeout |
| 102 | + vi.advanceTimersByTime(DEFAULT_FLUSH_INTERVAL / 2); |
| 103 | + |
| 104 | + // Add another log which should reset the timeout |
| 105 | + sentryCore._INTERNAL_captureLog({ level: 'info', message: 'test log 2' }, client); |
| 106 | + |
| 107 | + // Fast forward the remaining time |
| 108 | + vi.advanceTimersByTime(DEFAULT_FLUSH_INTERVAL / 2); |
| 109 | + |
| 110 | + // Should not have flushed yet since timeout was reset |
| 111 | + expect(sentryCore._INTERNAL_flushLogsBuffer).not.toHaveBeenCalled(); |
| 112 | + |
| 113 | + // Fast forward the full timeout |
| 114 | + vi.advanceTimersByTime(DEFAULT_FLUSH_INTERVAL); |
| 115 | + |
| 116 | + // Now should have flushed both logs |
| 117 | + expect(sentryCore._INTERNAL_flushLogsBuffer).toHaveBeenCalledWith(client); |
| 118 | + }); |
| 119 | + }); |
| 120 | +}); |
0 commit comments