From 2bb6d7adc250a078fffb4ce83b795c144fb44f91 Mon Sep 17 00:00:00 2001 From: Ashish Sharma Date: Thu, 23 Jul 2026 20:19:32 +0530 Subject: [PATCH] test: [PERA-4706] stabilize flaky integration teardown Fix the intermittent CI 'Test' failure (EnvironmentTeardownError: Closing rpc while onUserConsoleLog was pending, blamed on messages.test.tsx). - onramp useOnrampForm.spec: add the missing useNetworkStore export to the wallet-core-blockchain mock. Analytics reads useNetworkStore.getState() on every trackEvent; without it each call logged 'No useNetworkStore export on the mock' (9x), and that pending console forwarding races worker teardown. - integration setup afterEach: drain the macrotask queue so late background async (polling logger.warn) forwards its console within the test window, not at worker close. Same root cause the waitForConfirmation mock addresses. Test-only. No app/runtime code touched. Co-Authored-By: Claude Opus 4.8 (1M context) --- .../OnrampForm/__tests__/useOnrampForm.spec.tsx | 7 +++++++ apps/mobile/vitest.integration-setup.ts | 8 +++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/apps/mobile/src/modules/onramp/components/OnrampForm/__tests__/useOnrampForm.spec.tsx b/apps/mobile/src/modules/onramp/components/OnrampForm/__tests__/useOnrampForm.spec.tsx index 048294e70..79cca4678 100644 --- a/apps/mobile/src/modules/onramp/components/OnrampForm/__tests__/useOnrampForm.spec.tsx +++ b/apps/mobile/src/modules/onramp/components/OnrampForm/__tests__/useOnrampForm.spec.tsx @@ -108,6 +108,13 @@ vi.mock('@perawallet/wallet-core-assets', () => ({ vi.mock('@perawallet/wallet-core-blockchain', () => ({ useNetwork: () => ({ network: 'mainnet' }), + // Analytics (packages/analytics/src/log.ts) reads useNetworkStore.getState().network on every + // trackEvent. Without it, each analytics call logs "No useNetworkStore export on the mock", and + // that pending console forwarding intermittently crashes the vitest worker at teardown + // (EnvironmentTeardownError, blamed on messages.test.tsx in the same worker). + useNetworkStore: Object.assign(() => 'mainnet', { + getState: () => ({ network: 'mainnet' }), + }), })) vi.mock('@hooks/useToast', () => ({ diff --git a/apps/mobile/vitest.integration-setup.ts b/apps/mobile/vitest.integration-setup.ts index 5c2b62e41..0717d3e87 100644 --- a/apps/mobile/vitest.integration-setup.ts +++ b/apps/mobile/vitest.integration-setup.ts @@ -29,8 +29,14 @@ import { useBottomSheetStore } from './src/modules/bottom-sheet' // Reset it after each integration test so flows that count requests, or // assert sheet visibility from a known clean slate, aren't poisoned by // the previous case. -afterEach(() => { +afterEach(async () => { useBottomSheetStore.getState().resetState() + // Drain the macrotask queue so any background async that settles just after a flow test + // finishes (a late query resolve / logger.warn from polling code — see the + // waitForConfirmation note below) forwards its console DURING the test window rather than at + // worker teardown, where a pending forward crashes as + // `EnvironmentTeardownError: Closing rpc while "onUserConsoleLog" was pending` and fails CI. + await new Promise(resolve => setTimeout(resolve, 0)) }) // jsdom installs its own `Uint8Array` constructor on `globalThis`. Node's