Skip to content

Commit a08335d

Browse files
authored
feat(react): Add React version to events (#12390)
Given React 19 has changes with error handling, we may want to treat it differently in the product. (see https://www.notion.so/sentry/React-JS-19-23a7a2c5f88d4ef59a4b9647c84333a3?d=98df57f2272d482fa12a3c0618b43fc4#9bb5d5f0a9604508bb290fd7405fcce0 as an example). To make this easier, we now attach the react version as context on all outgoing events.
1 parent ce3e605 commit a08335d

File tree

4 files changed

+27
-2
lines changed

4 files changed

+27
-2
lines changed

dev-packages/e2e-tests/test-applications/create-next-app/tests/client-transactions.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ test('Sends a pageload transaction to Sentry', async ({ page }) => {
1717
transaction_info: { source: 'route' },
1818
type: 'transaction',
1919
contexts: {
20+
react: {
21+
version: '18.2.0',
22+
},
2023
trace: {
2124
span_id: expect.any(String),
2225
trace_id: expect.any(String),
@@ -60,6 +63,9 @@ test('captures a navigation transcation to Sentry', async ({ page }) => {
6063
transaction_info: { source: 'route' },
6164
type: 'transaction',
6265
contexts: {
66+
react: {
67+
version: '18.2.0',
68+
},
6369
trace: {
6470
span_id: expect.any(String),
6571
trace_id: expect.any(String),

dev-packages/e2e-tests/test-applications/nextjs-app-dir/tests/transactions.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ test('Sends a pageload transaction', async ({ page }) => {
1919
transaction_info: { source: 'url' },
2020
type: 'transaction',
2121
contexts: {
22+
react: {
23+
version: expect.any(String),
24+
},
2225
trace: {
2326
span_id: expect.any(String),
2427
trace_id: expect.any(String),

packages/react/src/sdk.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import type { BrowserOptions } from '@sentry/browser';
2-
import { init as browserInit } from '@sentry/browser';
2+
import { init as browserInit, setContext } from '@sentry/browser';
33
import { applySdkMetadata } from '@sentry/core';
44

5+
import { version } from 'react';
6+
57
/**
68
* Inits the React SDK
79
*/
@@ -11,6 +13,6 @@ export function init(options: BrowserOptions): void {
1113
};
1214

1315
applySdkMetadata(opts, 'react');
14-
16+
setContext('react', { version });
1517
browserInit(opts);
1618
}

packages/react/test/sdk.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import * as SentryBrowser from '@sentry/browser';
2+
import { version } from 'react';
3+
import { init } from '../src/sdk';
4+
5+
describe('init', () => {
6+
it('sets the React version (if available) in the global scope', () => {
7+
const setContextSpy = jest.spyOn(SentryBrowser, 'setContext');
8+
9+
init({});
10+
11+
expect(setContextSpy).toHaveBeenCalledTimes(1);
12+
expect(setContextSpy).toHaveBeenCalledWith('react', { version });
13+
});
14+
});

0 commit comments

Comments
 (0)