Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
amortemousque committed Mar 1, 2022
1 parent 43ef98b commit f5e2587
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 15 deletions.
53 changes: 44 additions & 9 deletions packages/logs/src/boot/startLogs.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
updateExperimentalFeatures,
getTimeStamp,
stopSessionManager,
isChromium,
} from '@datadog/browser-core'
import sinon from 'sinon'
import type { Clock } from '../../../core/test/specHelper'
Expand Down Expand Up @@ -56,6 +55,7 @@ declare global {
const DEFAULT_MESSAGE = { status: StatusType.info, message: 'message' }

describe('logs', () => {
const initConfiguration = { clientToken: 'xxx', service: 'service' }
let baseConfiguration: LogsConfiguration
let sessionIsTracked: boolean
let server: sinon.SinonFakeServer
Expand Down Expand Up @@ -84,7 +84,7 @@ describe('logs', () => {

beforeEach(() => {
baseConfiguration = {
...validateAndBuildLogsConfiguration({ clientToken: 'xxx', service: 'service' })!,
...validateAndBuildLogsConfiguration(initConfiguration)!,
logsEndpointBuilder: stubEndpointBuilder('https://localhost/v1/input/log'),
maxBatchSize: 1,
}
Expand Down Expand Up @@ -192,13 +192,16 @@ describe('logs', () => {
})
})

it('should send console logs', () => {
it('should send console logs when ff forward-logs is enabled', () => {
const logger = new Logger(noop)
const logErrorSpy = spyOn(logger, 'log')
const consoleLogSpy = spyOn(console, 'log').and.callFake(() => true)

updateExperimentalFeatures(['forward-logs'])
originalStartLogs({ ...baseConfiguration, forwardConsoleLogs: ['log'] }, logger)
originalStartLogs(
validateAndBuildLogsConfiguration({ ...initConfiguration, forwardConsoleLogs: ['log'] })!,
logger
)

/* eslint-disable-next-line no-console */
console.log('foo', 'bar')
Expand All @@ -209,15 +212,32 @@ describe('logs', () => {
resetExperimentalFeatures()
})

it('should send reports', () => {
if (!isChromium()) {
pending('no ReportingObserver support')
}
it('should not send console logs when ff forward-logs is disabled', () => {
const logger = new Logger(noop)
const logErrorSpy = spyOn(logger, 'log')
const consoleLogSpy = spyOn(console, 'log').and.callFake(() => true)

originalStartLogs(
validateAndBuildLogsConfiguration({ ...initConfiguration, forwardConsoleLogs: ['log'] })!,
logger
)

/* eslint-disable-next-line no-console */
console.log('foo', 'bar')

expect(logErrorSpy).not.toHaveBeenCalled()
expect(consoleLogSpy).toHaveBeenCalled()
})

it('should send reports when ff forward-reports is enabled', () => {
const logger = new Logger(noop)
const logErrorSpy = spyOn(logger, 'log')
const reportingObserverStub = stubReportingObserver()
updateExperimentalFeatures(['forward-reports'])
originalStartLogs({ ...baseConfiguration, forwardReports: ['intervention'] }, logger)
originalStartLogs(
validateAndBuildLogsConfiguration({ ...initConfiguration, forwardReports: ['intervention'] })!,
logger
)

reportingObserverStub.raiseReport('intervention')

Expand All @@ -226,6 +246,21 @@ describe('logs', () => {
resetExperimentalFeatures()
reportingObserverStub.reset()
})

it('should not send reports when ff forward-reports is disabled', () => {
const logger = new Logger(noop)
const logErrorSpy = spyOn(logger, 'log')
const reportingObserverStub = stubReportingObserver()
originalStartLogs(
validateAndBuildLogsConfiguration({ ...initConfiguration, forwardReports: ['intervention'] })!,
logger
)
reportingObserverStub.raiseReport('intervention')

expect(logErrorSpy).not.toHaveBeenCalled()

reportingObserverStub.reset()
})
})

describe('sampling', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import type { RawError, Subscription } from '@datadog/browser-core'
import { ErrorHandling, ErrorSource, Observable, clocksNow, isChromium } from '@datadog/browser-core'
import {
ErrorHandling,
ErrorSource,
Observable,
clocksNow,
updateExperimentalFeatures,
resetExperimentalFeatures,
} from '@datadog/browser-core'
import type { Clock } from '../../../../../core/test/specHelper'
import { mockClock } from '../../../../../core/test/specHelper'
import { stubReportingObserver } from '../../../../../core/test/stubReportApis'
Expand All @@ -16,7 +23,6 @@ describe('trackReportError', () => {
errorObservable = new Observable()
notifyLog = jasmine.createSpy('notifyLog')
reportingObserverStub = stubReportingObserver()
trackReportError(errorObservable)
subscription = errorObservable.subscribe(notifyLog)
clock = mockClock()
})
Expand All @@ -25,13 +31,13 @@ describe('trackReportError', () => {
subscription.unsubscribe()
clock.cleanup()
reportingObserverStub.reset()
resetExperimentalFeatures()
})

it('should error report', () => {
if (!isChromium()) {
pending('no ReportingObserver support')
}
it('should error report when ff forward-reports enabled', () => {
updateExperimentalFeatures(['forward-reports'])

trackReportError(errorObservable)
reportingObserverStub.raiseReport('intervention')

expect(notifyLog).toHaveBeenCalledWith({
Expand All @@ -43,4 +49,12 @@ describe('trackReportError', () => {
type: 'intervention',
})
})

it('should not report when ff forward-reports disabled', () => {
trackReportError(errorObservable)

reportingObserverStub.raiseReport('intervention')

expect(notifyLog).not.toHaveBeenCalled()
})
})

0 comments on commit f5e2587

Please sign in to comment.