diff --git a/dev-packages/e2e-tests/test-applications/create-next-app/tests/client-transactions.test.ts b/dev-packages/e2e-tests/test-applications/create-next-app/tests/client-transactions.test.ts index 7af02f35fa47..dd4633620e8b 100644 --- a/dev-packages/e2e-tests/test-applications/create-next-app/tests/client-transactions.test.ts +++ b/dev-packages/e2e-tests/test-applications/create-next-app/tests/client-transactions.test.ts @@ -17,6 +17,9 @@ test('Sends a pageload transaction to Sentry', async ({ page }) => { transaction_info: { source: 'route' }, type: 'transaction', contexts: { + react: { + version: '18.2.0', + }, trace: { span_id: expect.any(String), trace_id: expect.any(String), @@ -60,6 +63,9 @@ test('captures a navigation transcation to Sentry', async ({ page }) => { transaction_info: { source: 'route' }, type: 'transaction', contexts: { + react: { + version: '18.2.0', + }, trace: { span_id: expect.any(String), trace_id: expect.any(String), diff --git a/dev-packages/e2e-tests/test-applications/nextjs-app-dir/tests/transactions.test.ts b/dev-packages/e2e-tests/test-applications/nextjs-app-dir/tests/transactions.test.ts index 4a5c7771705b..94af99d6c386 100644 --- a/dev-packages/e2e-tests/test-applications/nextjs-app-dir/tests/transactions.test.ts +++ b/dev-packages/e2e-tests/test-applications/nextjs-app-dir/tests/transactions.test.ts @@ -19,6 +19,9 @@ test('Sends a pageload transaction', async ({ page }) => { transaction_info: { source: 'url' }, type: 'transaction', contexts: { + react: { + version: expect.any(String), + }, trace: { span_id: expect.any(String), trace_id: expect.any(String), diff --git a/packages/react/src/sdk.ts b/packages/react/src/sdk.ts index 24e75e8556f5..b2963b8c1f5e 100644 --- a/packages/react/src/sdk.ts +++ b/packages/react/src/sdk.ts @@ -1,7 +1,9 @@ import type { BrowserOptions } from '@sentry/browser'; -import { init as browserInit } from '@sentry/browser'; +import { init as browserInit, setContext } from '@sentry/browser'; import { applySdkMetadata } from '@sentry/core'; +import { version } from 'react'; + /** * Inits the React SDK */ @@ -11,6 +13,6 @@ export function init(options: BrowserOptions): void { }; applySdkMetadata(opts, 'react'); - + setContext('react', { version }); browserInit(opts); } diff --git a/packages/react/test/sdk.test.ts b/packages/react/test/sdk.test.ts new file mode 100644 index 000000000000..ce1b7f174f0d --- /dev/null +++ b/packages/react/test/sdk.test.ts @@ -0,0 +1,14 @@ +import * as SentryBrowser from '@sentry/browser'; +import { version } from 'react'; +import { init } from '../src/sdk'; + +describe('init', () => { + it('sets the React version (if available) in the global scope', () => { + const setContextSpy = jest.spyOn(SentryBrowser, 'setContext'); + + init({}); + + expect(setContextSpy).toHaveBeenCalledTimes(1); + expect(setContextSpy).toHaveBeenCalledWith('react', { version }); + }); +});