diff --git a/dev-packages/browser-integration-tests/suites/public-api/logger/consoleLoggingIntegrationShim/init.js b/dev-packages/browser-integration-tests/suites/public-api/logger/consoleLoggingIntegrationShim/init.js new file mode 100644 index 000000000000..7b5ee5690589 --- /dev/null +++ b/dev-packages/browser-integration-tests/suites/public-api/logger/consoleLoggingIntegrationShim/init.js @@ -0,0 +1,10 @@ +import * as Sentry from '@sentry/browser'; + +window.Sentry = Sentry; + +// consoleLoggingIntegration should not actually work, but still not error out +Sentry.init({ + dsn: 'https://public@dsn.ingest.sentry.io/1337', + sampleRate: 1, + integrations: [Sentry.consoleLoggingIntegration()], +}); diff --git a/dev-packages/browser-integration-tests/suites/public-api/logger/consoleLoggingIntegrationShim/test.ts b/dev-packages/browser-integration-tests/suites/public-api/logger/consoleLoggingIntegrationShim/test.ts new file mode 100644 index 000000000000..fa0f77d97643 --- /dev/null +++ b/dev-packages/browser-integration-tests/suites/public-api/logger/consoleLoggingIntegrationShim/test.ts @@ -0,0 +1,37 @@ +import { expect } from '@playwright/test'; +import { sentryTest } from '../../../../utils/fixtures'; + +sentryTest('exports a shim consoleLoggingIntegration for non-logs bundles', async ({ getLocalTestUrl, page }) => { + const bundle = process.env.PW_BUNDLE; + + // Only run this for CDN bundles that do NOT include logs + // Skip minified bundles because DEBUG_BUILD is false and warnings won't appear + if (!bundle?.startsWith('bundle') || bundle.includes('logs') || bundle.includes('min')) { + sentryTest.skip(); + } + + const consoleMessages: string[] = []; + page.on('console', msg => consoleMessages.push(msg.text())); + + let requestCount = 0; + await page.route(/^https:\/\/dsn\.ingest\.sentry\.io\//, route => { + requestCount++; + return route.fulfill({ + status: 200, + contentType: 'application/json', + body: JSON.stringify({ id: 'test-id' }), + }); + }); + + const url = await getLocalTestUrl({ testDir: __dirname, skipDsnRouteHandler: true }); + + await page.goto(url); + + // Wait a bit to ensure no requests are made + await page.waitForTimeout(500); + + expect(requestCount).toBe(0); + expect(consoleMessages).toEqual([ + 'You are using consoleLoggingIntegration() even though this bundle does not include logs.', + ]); +}); diff --git a/dev-packages/browser-integration-tests/suites/public-api/logger/loggerShim/init.js b/dev-packages/browser-integration-tests/suites/public-api/logger/loggerShim/init.js new file mode 100644 index 000000000000..c4b3013773c3 --- /dev/null +++ b/dev-packages/browser-integration-tests/suites/public-api/logger/loggerShim/init.js @@ -0,0 +1,18 @@ +import * as Sentry from '@sentry/browser'; + +window.Sentry = Sentry; + +Sentry.init({ + dsn: 'https://public@dsn.ingest.sentry.io/1337', + sampleRate: 1, +}); + +// These should not actually work, but still not error out +Sentry.logger.trace('test trace'); +Sentry.logger.debug('test debug'); +Sentry.logger.info('test info'); +Sentry.logger.warn('test warn'); +Sentry.logger.error('test error'); +Sentry.logger.fatal('test fatal'); +const testVar = 'test'; +Sentry.logger.info(Sentry.logger.fmt`formatted ${testVar}`); diff --git a/dev-packages/browser-integration-tests/suites/public-api/logger/loggerShim/test.ts b/dev-packages/browser-integration-tests/suites/public-api/logger/loggerShim/test.ts new file mode 100644 index 000000000000..58f74976f27d --- /dev/null +++ b/dev-packages/browser-integration-tests/suites/public-api/logger/loggerShim/test.ts @@ -0,0 +1,37 @@ +import { expect } from '@playwright/test'; +import { sentryTest } from '../../../../utils/fixtures'; + +sentryTest('exports a shim logger for non-logs bundles', async ({ getLocalTestUrl, page }) => { + const bundle = process.env.PW_BUNDLE; + + // Only run this for CDN bundles that do NOT include logs + // Skip minified bundles because DEBUG_BUILD is false and warnings won't appear + if (!bundle?.startsWith('bundle') || bundle.includes('logs') || bundle.includes('min')) { + sentryTest.skip(); + } + + const consoleMessages: string[] = []; + page.on('console', msg => consoleMessages.push(msg.text())); + + let requestCount = 0; + await page.route(/^https:\/\/dsn\.ingest\.sentry\.io\//, route => { + requestCount++; + return route.fulfill({ + status: 200, + contentType: 'application/json', + body: JSON.stringify({ id: 'test-id' }), + }); + }); + + const url = await getLocalTestUrl({ testDir: __dirname, skipDsnRouteHandler: true }); + + await page.goto(url); + + // Wait a bit to ensure no requests are made + await page.waitForTimeout(500); + + expect(requestCount).toBe(0); + + expect(consoleMessages).toContain('You are using Sentry.logger.* even though this bundle does not include logs.'); + expect(consoleMessages).toContain('You are using Sentry.logger.fmt even though this bundle does not include logs.'); +}); diff --git a/packages/browser/src/index.bundle.feedback.ts b/packages/browser/src/index.bundle.feedback.ts index 3e5acd440f5a..7f8e663bfd0a 100644 --- a/packages/browser/src/index.bundle.feedback.ts +++ b/packages/browser/src/index.bundle.feedback.ts @@ -1,8 +1,16 @@ -import { browserTracingIntegrationShim, replayIntegrationShim } from '@sentry-internal/integration-shims'; +import { + browserTracingIntegrationShim, + consoleLoggingIntegrationShim, + loggerShim, + replayIntegrationShim, +} from '@sentry-internal/integration-shims'; import { feedbackAsyncIntegration } from './feedbackAsync'; export * from './index.bundle.base'; +// TODO(v11): Export metricsShim here once we remove metrics from the base bundle. +export { consoleLoggingIntegrationShim as consoleLoggingIntegration, loggerShim as logger }; + export { getFeedback, sendFeedback } from '@sentry-internal/feedback'; export { diff --git a/packages/browser/src/index.bundle.replay.feedback.ts b/packages/browser/src/index.bundle.replay.feedback.ts index dd01508c4fc6..60c2a0e2ac4b 100644 --- a/packages/browser/src/index.bundle.replay.feedback.ts +++ b/packages/browser/src/index.bundle.replay.feedback.ts @@ -1,8 +1,15 @@ -import { browserTracingIntegrationShim } from '@sentry-internal/integration-shims'; +import { + browserTracingIntegrationShim, + consoleLoggingIntegrationShim, + loggerShim, +} from '@sentry-internal/integration-shims'; import { feedbackAsyncIntegration } from './feedbackAsync'; export * from './index.bundle.base'; +// TODO(v11): Export metricsShim here once we remove metrics from the base bundle. +export { consoleLoggingIntegrationShim as consoleLoggingIntegration, loggerShim as logger }; + export { getFeedback, sendFeedback } from '@sentry-internal/feedback'; export { diff --git a/packages/browser/src/index.bundle.replay.ts b/packages/browser/src/index.bundle.replay.ts index 86dc0fba7d25..9a370ae51b81 100644 --- a/packages/browser/src/index.bundle.replay.ts +++ b/packages/browser/src/index.bundle.replay.ts @@ -1,7 +1,15 @@ -import { browserTracingIntegrationShim, feedbackIntegrationShim } from '@sentry-internal/integration-shims'; +import { + browserTracingIntegrationShim, + consoleLoggingIntegrationShim, + feedbackIntegrationShim, + loggerShim, +} from '@sentry-internal/integration-shims'; export * from './index.bundle.base'; +// TODO(v11): Export metricsShim here once we remove metrics from the base bundle. +export { consoleLoggingIntegrationShim as consoleLoggingIntegration, loggerShim as logger }; + export { replayIntegration, getReplay } from '@sentry-internal/replay'; export { diff --git a/packages/browser/src/index.bundle.tracing.replay.feedback.ts b/packages/browser/src/index.bundle.tracing.replay.feedback.ts index 7aa4b3ae778c..b6b298189aef 100644 --- a/packages/browser/src/index.bundle.tracing.replay.feedback.ts +++ b/packages/browser/src/index.bundle.tracing.replay.feedback.ts @@ -1,10 +1,14 @@ import { registerSpanErrorInstrumentation } from '@sentry/core'; +import { consoleLoggingIntegrationShim, loggerShim } from '@sentry-internal/integration-shims'; import { feedbackAsyncIntegration } from './feedbackAsync'; registerSpanErrorInstrumentation(); export * from './index.bundle.base'; +// TODO(v11): Export metricsShim here once we remove metrics from the base bundle. +export { consoleLoggingIntegrationShim as consoleLoggingIntegration, loggerShim as logger }; + export { getActiveSpan, getRootSpan, diff --git a/packages/browser/src/index.bundle.tracing.replay.ts b/packages/browser/src/index.bundle.tracing.replay.ts index 3dc858d69cb5..a20a7b8388f1 100644 --- a/packages/browser/src/index.bundle.tracing.replay.ts +++ b/packages/browser/src/index.bundle.tracing.replay.ts @@ -1,10 +1,13 @@ import { registerSpanErrorInstrumentation } from '@sentry/core'; -import { feedbackIntegrationShim } from '@sentry-internal/integration-shims'; +import { consoleLoggingIntegrationShim, feedbackIntegrationShim, loggerShim } from '@sentry-internal/integration-shims'; registerSpanErrorInstrumentation(); export * from './index.bundle.base'; +// TODO(v11): Export metricsShim here once we remove metrics from the base bundle. +export { consoleLoggingIntegrationShim as consoleLoggingIntegration, loggerShim as logger }; + export { getActiveSpan, getRootSpan, diff --git a/packages/browser/src/index.bundle.tracing.ts b/packages/browser/src/index.bundle.tracing.ts index 62259b92ce7e..c3cb0a85cf1d 100644 --- a/packages/browser/src/index.bundle.tracing.ts +++ b/packages/browser/src/index.bundle.tracing.ts @@ -1,10 +1,18 @@ import { registerSpanErrorInstrumentation } from '@sentry/core'; -import { feedbackIntegrationShim, replayIntegrationShim } from '@sentry-internal/integration-shims'; +import { + consoleLoggingIntegrationShim, + feedbackIntegrationShim, + loggerShim, + replayIntegrationShim, +} from '@sentry-internal/integration-shims'; registerSpanErrorInstrumentation(); export * from './index.bundle.base'; +// TODO(v11): Export metricsShim here once we remove metrics from the base bundle. +export { consoleLoggingIntegrationShim as consoleLoggingIntegration, loggerShim as logger }; + export { getActiveSpan, getRootSpan, diff --git a/packages/browser/src/index.bundle.ts b/packages/browser/src/index.bundle.ts index 5004b376cd46..cd7de6dd80c8 100644 --- a/packages/browser/src/index.bundle.ts +++ b/packages/browser/src/index.bundle.ts @@ -1,11 +1,16 @@ import { browserTracingIntegrationShim, + consoleLoggingIntegrationShim, feedbackIntegrationShim, + loggerShim, replayIntegrationShim, } from '@sentry-internal/integration-shims'; export * from './index.bundle.base'; +// TODO(v11): Export metricsShim here once we remove metrics from the base bundle. +export { consoleLoggingIntegrationShim as consoleLoggingIntegration, loggerShim as logger }; + export { browserTracingIntegrationShim as browserTracingIntegration, feedbackIntegrationShim as feedbackAsyncIntegration, diff --git a/packages/browser/test/index.bundle.feedback.test.ts b/packages/browser/test/index.bundle.feedback.test.ts index 4e2e0d33797b..5b72e0566236 100644 --- a/packages/browser/test/index.bundle.feedback.test.ts +++ b/packages/browser/test/index.bundle.feedback.test.ts @@ -1,4 +1,9 @@ -import { browserTracingIntegrationShim, replayIntegrationShim } from '@sentry-internal/integration-shims'; +import { + browserTracingIntegrationShim, + consoleLoggingIntegrationShim, + loggerShim, + replayIntegrationShim, +} from '@sentry-internal/integration-shims'; import { describe, expect, it } from 'vitest'; import { feedbackAsyncIntegration } from '../src'; import * as FeedbackBundle from '../src/index.bundle.feedback'; @@ -9,5 +14,8 @@ describe('index.bundle.feedback', () => { expect(FeedbackBundle.feedbackAsyncIntegration).toBe(feedbackAsyncIntegration); expect(FeedbackBundle.feedbackIntegration).toBe(feedbackAsyncIntegration); expect(FeedbackBundle.replayIntegration).toBe(replayIntegrationShim); + + expect(FeedbackBundle.logger).toBe(loggerShim); + expect(FeedbackBundle.consoleLoggingIntegration).toBe(consoleLoggingIntegrationShim); }); }); diff --git a/packages/browser/test/index.bundle.replay.feedback.test.ts b/packages/browser/test/index.bundle.replay.feedback.test.ts new file mode 100644 index 000000000000..b92c2c41b731 --- /dev/null +++ b/packages/browser/test/index.bundle.replay.feedback.test.ts @@ -0,0 +1,20 @@ +import { + browserTracingIntegrationShim, + consoleLoggingIntegrationShim, + loggerShim, +} from '@sentry-internal/integration-shims'; +import { describe, expect, it } from 'vitest'; +import { feedbackAsyncIntegration, replayIntegration } from '../src'; +import * as ReplayFeedbackBundle from '../src/index.bundle.replay.feedback'; + +describe('index.bundle.replay.feedback', () => { + it('has correct exports', () => { + expect(ReplayFeedbackBundle.browserTracingIntegration).toBe(browserTracingIntegrationShim); + expect(ReplayFeedbackBundle.feedbackAsyncIntegration).toBe(feedbackAsyncIntegration); + expect(ReplayFeedbackBundle.feedbackIntegration).toBe(feedbackAsyncIntegration); + expect(ReplayFeedbackBundle.replayIntegration).toBe(replayIntegration); + + expect(ReplayFeedbackBundle.logger).toBe(loggerShim); + expect(ReplayFeedbackBundle.consoleLoggingIntegration).toBe(consoleLoggingIntegrationShim); + }); +}); diff --git a/packages/browser/test/index.bundle.replay.test.ts b/packages/browser/test/index.bundle.replay.test.ts index dac5dab59a51..2bfc2ffcf7fc 100644 --- a/packages/browser/test/index.bundle.replay.test.ts +++ b/packages/browser/test/index.bundle.replay.test.ts @@ -1,4 +1,9 @@ -import { browserTracingIntegrationShim, feedbackIntegrationShim } from '@sentry-internal/integration-shims'; +import { + browserTracingIntegrationShim, + consoleLoggingIntegrationShim, + feedbackIntegrationShim, + loggerShim, +} from '@sentry-internal/integration-shims'; import { describe, expect, it } from 'vitest'; import { replayIntegration } from '../src'; import * as ReplayBundle from '../src/index.bundle.replay'; @@ -9,5 +14,8 @@ describe('index.bundle.replay', () => { expect(ReplayBundle.feedbackAsyncIntegration).toBe(feedbackIntegrationShim); expect(ReplayBundle.feedbackIntegration).toBe(feedbackIntegrationShim); expect(ReplayBundle.replayIntegration).toBe(replayIntegration); + + expect(ReplayBundle.logger).toBe(loggerShim); + expect(ReplayBundle.consoleLoggingIntegration).toBe(consoleLoggingIntegrationShim); }); }); diff --git a/packages/browser/test/index.bundle.test.ts b/packages/browser/test/index.bundle.test.ts index f2f0fdf0d9a9..6b29fea23aeb 100644 --- a/packages/browser/test/index.bundle.test.ts +++ b/packages/browser/test/index.bundle.test.ts @@ -1,6 +1,8 @@ import { browserTracingIntegrationShim, + consoleLoggingIntegrationShim, feedbackIntegrationShim, + loggerShim, replayIntegrationShim, } from '@sentry-internal/integration-shims'; import { describe, expect, it } from 'vitest'; @@ -12,5 +14,8 @@ describe('index.bundle', () => { expect(Bundle.feedbackAsyncIntegration).toBe(feedbackIntegrationShim); expect(Bundle.feedbackIntegration).toBe(feedbackIntegrationShim); expect(Bundle.replayIntegration).toBe(replayIntegrationShim); + + expect(Bundle.logger).toBe(loggerShim); + expect(Bundle.consoleLoggingIntegration).toBe(consoleLoggingIntegrationShim); }); }); diff --git a/packages/browser/test/index.bundle.tracing.replay.feedback.test.ts b/packages/browser/test/index.bundle.tracing.replay.feedback.test.ts index a15770e2869f..1cb7a18fe8bd 100644 --- a/packages/browser/test/index.bundle.tracing.replay.feedback.test.ts +++ b/packages/browser/test/index.bundle.tracing.replay.feedback.test.ts @@ -1,3 +1,4 @@ +import { consoleLoggingIntegrationShim, loggerShim } from '@sentry-internal/integration-shims'; import { describe, expect, it } from 'vitest'; import { browserTracingIntegration, feedbackAsyncIntegration, replayIntegration } from '../src'; import * as TracingReplayFeedbackBundle from '../src/index.bundle.tracing.replay.feedback'; @@ -8,5 +9,8 @@ describe('index.bundle.tracing.replay.feedback', () => { expect(TracingReplayFeedbackBundle.feedbackAsyncIntegration).toBe(feedbackAsyncIntegration); expect(TracingReplayFeedbackBundle.feedbackIntegration).toBe(feedbackAsyncIntegration); expect(TracingReplayFeedbackBundle.replayIntegration).toBe(replayIntegration); + + expect(TracingReplayFeedbackBundle.logger).toBe(loggerShim); + expect(TracingReplayFeedbackBundle.consoleLoggingIntegration).toBe(consoleLoggingIntegrationShim); }); }); diff --git a/packages/browser/test/index.bundle.tracing.replay.test.ts b/packages/browser/test/index.bundle.tracing.replay.test.ts index 151343ab2ab9..90c82f32cb6b 100644 --- a/packages/browser/test/index.bundle.tracing.replay.test.ts +++ b/packages/browser/test/index.bundle.tracing.replay.test.ts @@ -1,4 +1,4 @@ -import { feedbackIntegrationShim } from '@sentry-internal/integration-shims'; +import { consoleLoggingIntegrationShim, feedbackIntegrationShim, loggerShim } from '@sentry-internal/integration-shims'; import { describe, expect, it } from 'vitest'; import { browserTracingIntegration, replayIntegration } from '../src'; import * as TracingReplayBundle from '../src/index.bundle.tracing.replay'; @@ -9,5 +9,8 @@ describe('index.bundle.tracing.replay', () => { expect(TracingReplayBundle.feedbackAsyncIntegration).toBe(feedbackIntegrationShim); expect(TracingReplayBundle.feedbackIntegration).toBe(feedbackIntegrationShim); expect(TracingReplayBundle.replayIntegration).toBe(replayIntegration); + + expect(TracingReplayBundle.logger).toBe(loggerShim); + expect(TracingReplayBundle.consoleLoggingIntegration).toBe(consoleLoggingIntegrationShim); }); }); diff --git a/packages/browser/test/index.bundle.tracing.test.ts b/packages/browser/test/index.bundle.tracing.test.ts index 5d020c2a2e6f..2ec58fcb9e48 100644 --- a/packages/browser/test/index.bundle.tracing.test.ts +++ b/packages/browser/test/index.bundle.tracing.test.ts @@ -1,4 +1,9 @@ -import { feedbackIntegrationShim, replayIntegrationShim } from '@sentry-internal/integration-shims'; +import { + consoleLoggingIntegrationShim, + feedbackIntegrationShim, + loggerShim, + replayIntegrationShim, +} from '@sentry-internal/integration-shims'; import { describe, expect, it } from 'vitest'; import { browserTracingIntegration } from '../src'; import * as TracingBundle from '../src/index.bundle.tracing'; @@ -9,5 +14,8 @@ describe('index.bundle.tracing', () => { expect(TracingBundle.feedbackAsyncIntegration).toBe(feedbackIntegrationShim); expect(TracingBundle.feedbackIntegration).toBe(feedbackIntegrationShim); expect(TracingBundle.replayIntegration).toBe(replayIntegrationShim); + + expect(TracingBundle.logger).toBe(loggerShim); + expect(TracingBundle.consoleLoggingIntegration).toBe(consoleLoggingIntegrationShim); }); }); diff --git a/packages/integration-shims/package.json b/packages/integration-shims/package.json index b5ee84f43421..fe82d9d87130 100644 --- a/packages/integration-shims/package.json +++ b/packages/integration-shims/package.json @@ -44,7 +44,8 @@ "clean": "rimraf build", "fix": "eslint . --format stylish --fix", "lint": "eslint . --format stylish", - "lint:es-compatibility": "es-check es2020 ./build/cjs/*.js && es-check es2020 ./build/esm/*.js --module" + "lint:es-compatibility": "es-check es2020 ./build/cjs/*.js && es-check es2020 ./build/esm/*.js --module", + "test": "vitest run" }, "repository": { "type": "git", diff --git a/packages/integration-shims/src/debug-build.ts b/packages/integration-shims/src/debug-build.ts new file mode 100644 index 000000000000..60aa50940582 --- /dev/null +++ b/packages/integration-shims/src/debug-build.ts @@ -0,0 +1,8 @@ +declare const __DEBUG_BUILD__: boolean; + +/** + * This serves as a build time flag that will be true by default, but false in non-debug builds or if users replace `__SENTRY_DEBUG__` in their generated code. + * + * ATTENTION: This constant must never cross package boundaries (i.e. be exported) to guarantee that it can be used for tree shaking. + */ +export const DEBUG_BUILD = __DEBUG_BUILD__; diff --git a/packages/integration-shims/src/index.ts b/packages/integration-shims/src/index.ts index e887ac725023..1d535b6da35d 100644 --- a/packages/integration-shims/src/index.ts +++ b/packages/integration-shims/src/index.ts @@ -2,3 +2,4 @@ export { feedbackIntegrationShim } from './Feedback'; export { replayIntegrationShim } from './Replay'; export { browserTracingIntegrationShim } from './BrowserTracing'; export { launchDarklyIntegrationShim, buildLaunchDarklyFlagUsedHandlerShim } from './launchDarkly'; +export { loggerShim, consoleLoggingIntegrationShim } from './logs'; diff --git a/packages/integration-shims/src/logs.ts b/packages/integration-shims/src/logs.ts new file mode 100644 index 000000000000..33af020efefc --- /dev/null +++ b/packages/integration-shims/src/logs.ts @@ -0,0 +1,59 @@ +import type { Integration, ParameterizedString } from '@sentry/core'; +import { consoleSandbox, defineIntegration } from '@sentry/core'; +import { FAKE_FUNCTION } from './common'; +import { DEBUG_BUILD } from './debug-build'; + +/** + * This is a shim for the logger namespace. + * It is needed in order for the CDN bundles to continue working when users add/remove logs + * from it, without changing their config. This is necessary for the loader mechanism. + */ +function logShim(_message: unknown, _attributes?: unknown): void { + DEBUG_BUILD && + consoleSandbox(() => { + // eslint-disable-next-line no-console + console.warn('You are using Sentry.logger.* even though this bundle does not include logs.'); + }); +} + +/** + * This is a shim for the logger.fmt template literal function. + * It is needed in order for the CDN bundles to continue working when users add/remove logs + * from it, without changing their config. This is necessary for the loader mechanism. + */ +function fmtShim(_strings: TemplateStringsArray, ..._values: unknown[]): ParameterizedString { + DEBUG_BUILD && + consoleSandbox(() => { + // eslint-disable-next-line no-console + console.warn('You are using Sentry.logger.fmt even though this bundle does not include logs.'); + }); + return '' as ParameterizedString; +} + +export const loggerShim = { + trace: logShim, + debug: logShim, + info: logShim, + warn: logShim, + error: logShim, + fatal: logShim, + fmt: fmtShim, +}; + +/** + * This is a shim for the consoleLoggingIntegration. + * It is needed in order for the CDN bundles to continue working when users add/remove logs + * from it, without changing their config. This is necessary for the loader mechanism. + */ +export const consoleLoggingIntegrationShim = defineIntegration((_options?: unknown) => { + DEBUG_BUILD && + consoleSandbox(() => { + // eslint-disable-next-line no-console + console.warn('You are using consoleLoggingIntegration() even though this bundle does not include logs.'); + }); + + return { + name: 'ConsoleLogs', + setup: FAKE_FUNCTION, + }; +}) as () => Integration; diff --git a/packages/integration-shims/test/logs.test.ts b/packages/integration-shims/test/logs.test.ts new file mode 100644 index 000000000000..9bde70bf8ec5 --- /dev/null +++ b/packages/integration-shims/test/logs.test.ts @@ -0,0 +1,77 @@ +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; + +let mockDebugBuild = true; + +vi.mock('../src/debug-build', () => ({ + get DEBUG_BUILD() { + return mockDebugBuild; + }, +})); + +// Must import after mocking +const { loggerShim, consoleLoggingIntegrationShim } = await import('../src/logs'); + +describe('logs shims', () => { + const consoleWarnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {}); + + beforeEach(() => { + consoleWarnSpy.mockClear(); + }); + + afterEach(() => { + mockDebugBuild = true; + }); + + describe('when DEBUG_BUILD is true', () => { + beforeEach(() => { + mockDebugBuild = true; + }); + + it('loggerShim methods should warn', () => { + loggerShim.trace('test'); + expect(consoleWarnSpy).toHaveBeenCalledWith( + 'You are using Sentry.logger.* even though this bundle does not include logs.', + ); + }); + + it('loggerShim.fmt should warn', () => { + loggerShim.fmt`test`; + expect(consoleWarnSpy).toHaveBeenCalledWith( + 'You are using Sentry.logger.fmt even though this bundle does not include logs.', + ); + }); + + it('consoleLoggingIntegrationShim should warn', () => { + consoleLoggingIntegrationShim(); + expect(consoleWarnSpy).toHaveBeenCalledWith( + 'You are using consoleLoggingIntegration() even though this bundle does not include logs.', + ); + }); + }); + + describe('when DEBUG_BUILD is false', () => { + beforeEach(() => { + mockDebugBuild = false; + }); + + it('loggerShim methods should NOT warn', () => { + loggerShim.trace('test'); + loggerShim.debug('test'); + loggerShim.info('test'); + loggerShim.warn('test'); + loggerShim.error('test'); + loggerShim.fatal('test'); + expect(consoleWarnSpy).not.toHaveBeenCalled(); + }); + + it('loggerShim.fmt should NOT warn', () => { + loggerShim.fmt`test`; + expect(consoleWarnSpy).not.toHaveBeenCalled(); + }); + + it('consoleLoggingIntegrationShim should NOT warn', () => { + consoleLoggingIntegrationShim(); + expect(consoleWarnSpy).not.toHaveBeenCalled(); + }); + }); +}); diff --git a/packages/integration-shims/tsconfig.test.json b/packages/integration-shims/tsconfig.test.json new file mode 100644 index 000000000000..ac7243b6aebf --- /dev/null +++ b/packages/integration-shims/tsconfig.test.json @@ -0,0 +1,7 @@ +{ + "extends": "./tsconfig.json", + "include": ["test/**/*", "vite.config.ts"], + "compilerOptions": { + "types": ["vitest/globals"] + } +} diff --git a/packages/integration-shims/vite.config.ts b/packages/integration-shims/vite.config.ts new file mode 100644 index 000000000000..f18ec92095bc --- /dev/null +++ b/packages/integration-shims/vite.config.ts @@ -0,0 +1,8 @@ +import baseConfig from '../../vite/vite.config'; + +export default { + ...baseConfig, + test: { + ...baseConfig.test, + }, +};