From 2853575179cbcc4a6df00451b481cd9234640e0b Mon Sep 17 00:00:00 2001 From: Antonis Lilis Date: Tue, 24 Nov 2026 12:57:53 +0100 Subject: [PATCH] test(e2e): Fix flaky sample app E2E tests Stop the sample app before each test file starts its mocked Sentry server. The app launched by the previous test file kept running until the next Maestro flow relaunched it, so transactions it finished in the meantime were delivered to the next test's server and mixed into its envelopes. Make device.connection_type optional on the iOS message event. sentry-cocoa only knows it after its reachability monitor reported the first network path, which can be after a message captured right after launch. Assert it on the ErrorsScreen transaction instead, which is sent seconds later. Print the received transactions when the ErrorsScreen app start transaction is missing, to diagnose the remaining Android failure. Refs #7093 Co-Authored-By: Claude Opus 5.5 --- .../e2e/jest.config.android.auto.js | 1 + .../e2e/jest.config.android.manual.js | 1 + .../react-native/e2e/jest.config.ios.auto.js | 1 + .../e2e/jest.config.ios.manual.js | 1 + .../captureErrorsScreenTransaction.test.ts | 42 +++++++++++++++++++ .../captureMessage/captureMessage.test.ios.ts | 11 ++++- samples/react-native/e2e/utils/app.ts | 28 +++++++++++++ .../e2e/utils/mockedSentryServer.ts | 4 ++ 8 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 samples/react-native/e2e/utils/app.ts diff --git a/samples/react-native/e2e/jest.config.android.auto.js b/samples/react-native/e2e/jest.config.android.auto.js index db4de3a8fd..477100069c 100644 --- a/samples/react-native/e2e/jest.config.android.auto.js +++ b/samples/react-native/e2e/jest.config.android.auto.js @@ -4,6 +4,7 @@ const baseConfig = require('./jest.config.base'); /** @type {import('@jest/types').Config.InitialOptions} */ module.exports = { ...baseConfig, + globals: { E2E_PLATFORM: 'android' }, globalSetup: path.resolve(__dirname, 'setup.android.auto.ts'), testMatch: [ ...baseConfig.testMatch, diff --git a/samples/react-native/e2e/jest.config.android.manual.js b/samples/react-native/e2e/jest.config.android.manual.js index 89d80d3fc7..48c1c00746 100644 --- a/samples/react-native/e2e/jest.config.android.manual.js +++ b/samples/react-native/e2e/jest.config.android.manual.js @@ -3,6 +3,7 @@ const baseConfig = require('./jest.config.base'); /** @type {import('@jest/types').Config.InitialOptions} */ module.exports = { ...baseConfig, + globals: { E2E_PLATFORM: 'android' }, testMatch: [ ...baseConfig.testMatch, '/e2e/**/*.test.android.ts', diff --git a/samples/react-native/e2e/jest.config.ios.auto.js b/samples/react-native/e2e/jest.config.ios.auto.js index 8d04d86887..bdbaf5ee60 100644 --- a/samples/react-native/e2e/jest.config.ios.auto.js +++ b/samples/react-native/e2e/jest.config.ios.auto.js @@ -4,6 +4,7 @@ const baseConfig = require('./jest.config.base'); /** @type {import('@jest/types').Config.InitialOptions} */ module.exports = { ...baseConfig, + globals: { E2E_PLATFORM: 'ios' }, globalSetup: path.resolve(__dirname, 'setup.ios.auto.ts'), testMatch: [ ...baseConfig.testMatch, diff --git a/samples/react-native/e2e/jest.config.ios.manual.js b/samples/react-native/e2e/jest.config.ios.manual.js index df5a5b8e95..0f85b07c9c 100644 --- a/samples/react-native/e2e/jest.config.ios.manual.js +++ b/samples/react-native/e2e/jest.config.ios.manual.js @@ -4,6 +4,7 @@ const path = require('path'); /** @type {import('@jest/types').Config.InitialOptions} */ module.exports = { ...baseConfig, + globals: { E2E_PLATFORM: 'ios' }, testMatch: [ ...baseConfig.testMatch, '/e2e/**/*.test.ios.ts', diff --git a/samples/react-native/e2e/tests/captureErrorScreenTransaction/captureErrorsScreenTransaction.test.ts b/samples/react-native/e2e/tests/captureErrorScreenTransaction/captureErrorsScreenTransaction.test.ts index 44f8b0dc84..bf7dbbe1c4 100644 --- a/samples/react-native/e2e/tests/captureErrorScreenTransaction/captureErrorsScreenTransaction.test.ts +++ b/samples/react-native/e2e/tests/captureErrorScreenTransaction/captureErrorsScreenTransaction.test.ts @@ -49,6 +49,28 @@ describe('Capture Errors Screen Transaction', () => { if (appStartTransaction) break; } + if (!appStartTransaction) { + // Print what did arrive, so a CI failure shows whether the app start was not + // attached to the ErrorsScreen transaction or that transaction was never sent. + const received = sentryServer.getAllEnvelopes(() => true).flatMap(envelope => + envelope[1] + .filter(([header]) => header.type === 'transaction') + .map(([, payload]) => { + const event = payload as any; + return { + transaction: event.transaction, + op: event.contexts?.trace?.op, + origin: event.contexts?.trace?.origin, + start_timestamp: event.start_timestamp, + timestamp: event.timestamp, + measurements: Object.keys(event.measurements ?? {}), + span_ops: (event.spans ?? []).map((span: any) => span.op), + }; + }), + ); + console.log('Received transactions:', JSON.stringify(received, null, 2)); + } + expect(appStartTransaction).toBeDefined(); const [header, payload] = appStartTransaction!; @@ -123,6 +145,26 @@ describe('Capture Errors Screen Transaction', () => { ); }); + it('contains device connection type', async () => { + // The transaction is sent seconds after launch, once the native SDK reported the + // network path, so `connection_type` is reliably present here — unlike on a message + // captured right after launch (see captureMessage.test.ios.ts). + const item = getItemOfTypeFrom( + getErrorsEnvelope(), + 'transaction', + ); + + expect(item?.[1]).toEqual( + expect.objectContaining({ + contexts: expect.objectContaining({ + device: expect.objectContaining({ + connection_type: expect.any(String), + }), + }), + }), + ); + }); + it('contains JS stall measurements', async () => { const item = getItemOfTypeFrom( await getErrorsEnvelope(), diff --git a/samples/react-native/e2e/tests/captureMessage/captureMessage.test.ios.ts b/samples/react-native/e2e/tests/captureMessage/captureMessage.test.ios.ts index f0be0fcad3..ad9c92f81a 100644 --- a/samples/react-native/e2e/tests/captureMessage/captureMessage.test.ios.ts +++ b/samples/react-native/e2e/tests/captureMessage/captureMessage.test.ios.ts @@ -58,7 +58,6 @@ describe('Capture message', () => { contexts: expect.objectContaining({ device: expect.objectContaining({ arch: expect.any(String), - connection_type: expect.any(String), family: expect.any(String), free_memory: expect.any(Number), locale: expect.any(String), @@ -73,6 +72,16 @@ describe('Capture message', () => { }), }), ); + + // sentry-cocoa only knows the connection type after its reachability monitor reported the + // first network path, which happens asynchronously after start. A message captured right + // after launch can be sent before that, so the field is optional here. Its presence is + // asserted on a later event in captureErrorsScreenTransaction.test.ts. + const connectionType = (item?.[1].contexts?.device as Record | undefined) + ?.connection_type; + if (connectionType !== undefined) { + expect(connectionType).toEqual(expect.any(String)); + } }); it('contains app context', async () => { diff --git a/samples/react-native/e2e/utils/app.ts b/samples/react-native/e2e/utils/app.ts new file mode 100644 index 0000000000..d602a5faf8 --- /dev/null +++ b/samples/react-native/e2e/utils/app.ts @@ -0,0 +1,28 @@ +import { execFileSync } from 'node:child_process'; + +const APP_ID = 'io.sentry.reactnative.sample'; + +type E2EPlatform = 'android' | 'ios'; + +declare const E2E_PLATFORM: E2EPlatform | undefined; + +/** + * Stops the sample app if it is still running from a previous test. + * + * Each test file starts its own mocked Sentry server, but the app launched by the + * previous test file keeps running until the next Maestro flow relaunches it. Any + * transaction that app finishes in the meantime (idle navigation spans end seconds + * later) would be delivered to the new server and mixed into the new test's envelopes. + */ +export function stopApp(): void { + const platform = typeof E2E_PLATFORM === 'undefined' ? undefined : E2E_PLATFORM; + try { + if (platform === 'android') { + execFileSync('adb', ['shell', 'am', 'force-stop', APP_ID], { stdio: 'ignore' }); + } else if (platform === 'ios') { + execFileSync('xcrun', ['simctl', 'terminate', 'booted', APP_ID], { stdio: 'ignore' }); + } + } catch { + // The app is not running, which is the state we want. + } +} diff --git a/samples/react-native/e2e/utils/mockedSentryServer.ts b/samples/react-native/e2e/utils/mockedSentryServer.ts index 34baaeacb1..14c3631a08 100644 --- a/samples/react-native/e2e/utils/mockedSentryServer.ts +++ b/samples/react-native/e2e/utils/mockedSentryServer.ts @@ -2,6 +2,7 @@ import { IncomingMessage, ServerResponse, createServer } from 'node:http'; import { createGunzip } from 'node:zlib'; import { Envelope, EnvelopeItem } from '@sentry/core'; import { parseEnvelope } from './parseEnvelope'; +import { stopApp } from './app'; import { Event } from '@sentry/core'; type RecordedRequest = { @@ -64,6 +65,9 @@ export function createSentryServer({ port = 8961 } = {}): { return { start: () => { + // Stop the app left running by the previous test, so its late envelopes + // can't reach this server. + stopApp(); return new Promise((resolve, _reject) => { server.listen(port, () => { resolve();