From 019e532844845101afcb5aee8c813a5d20080984 Mon Sep 17 00:00:00 2001 From: Piyush Date: Thu, 6 Nov 2025 16:16:56 +0530 Subject: [PATCH 1/3] Fix flaky test --- tests/navigation/isActiveRouteTests.tsx | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/tests/navigation/isActiveRouteTests.tsx b/tests/navigation/isActiveRouteTests.tsx index fc13b7c7f2f0..671f79cf2303 100644 --- a/tests/navigation/isActiveRouteTests.tsx +++ b/tests/navigation/isActiveRouteTests.tsx @@ -1,14 +1,35 @@ import {describe, expect} from '@jest/globals'; import {render} from '@testing-library/react-native'; +import {cleanup} from '@testing-library/react-native'; import useResponsiveLayout from '@hooks/useResponsiveLayout'; import getIsNarrowLayout from '@libs/getIsNarrowLayout'; import CONST from '@src/CONST'; import Navigation from '@src/libs/Navigation/Navigation'; +import navigationRef from '@src/libs/Navigation/navigationRef'; import NAVIGATORS from '@src/NAVIGATORS'; import type {Route} from '@src/ROUTES'; import SCREENS from '@src/SCREENS'; import TestNavigationContainer from '../utils/TestNavigationContainer'; +afterEach(() => { + // Ensure mounted components are unmounted + cleanup(); + + // Clear timers and restore real timers (in case fake timers are used anywhere) + jest.clearAllTimers(); + jest.useRealTimers(); + + // Reset any mocks used by this file + jest.restoreAllMocks(); + jest.resetModules(); + + // Clear the navigation ref so listeners/hooks attached to it don't keep the worker alive. + // This is intentionally type-unsafe to forcibly drop the ref between tests. + if ((navigationRef as any).current) { + (navigationRef as any).current = undefined; + } +}); + jest.mock('@hooks/useResponsiveLayout', () => jest.fn()); jest.mock('@libs/getIsNarrowLayout', () => jest.fn()); From 07b50c921df030e818859ca844c0246d74dc8076 Mon Sep 17 00:00:00 2001 From: Piyush Date: Thu, 6 Nov 2025 16:46:03 +0530 Subject: [PATCH 2/3] Fix eslint check error --- tests/navigation/isActiveRouteTests.tsx | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/tests/navigation/isActiveRouteTests.tsx b/tests/navigation/isActiveRouteTests.tsx index 671f79cf2303..18ff8471d2e8 100644 --- a/tests/navigation/isActiveRouteTests.tsx +++ b/tests/navigation/isActiveRouteTests.tsx @@ -1,6 +1,5 @@ import {describe, expect} from '@jest/globals'; -import {render} from '@testing-library/react-native'; -import {cleanup} from '@testing-library/react-native'; +import {cleanup, render} from '@testing-library/react-native'; import useResponsiveLayout from '@hooks/useResponsiveLayout'; import getIsNarrowLayout from '@libs/getIsNarrowLayout'; import CONST from '@src/CONST'; @@ -25,8 +24,8 @@ afterEach(() => { // Clear the navigation ref so listeners/hooks attached to it don't keep the worker alive. // This is intentionally type-unsafe to forcibly drop the ref between tests. - if ((navigationRef as any).current) { - (navigationRef as any).current = undefined; + if (navigationRef.current) { + navigationRef.current = null; } }); From ce28a12e59605a465e69cf3f806e48a34aeffd79 Mon Sep 17 00:00:00 2001 From: Piyush Date: Sat, 8 Nov 2025 13:52:39 +0530 Subject: [PATCH 3/3] added afterEach inside describe callback to be consistent --- tests/navigation/isActiveRouteTests.tsx | 37 ++++++++++++------------- 1 file changed, 18 insertions(+), 19 deletions(-) diff --git a/tests/navigation/isActiveRouteTests.tsx b/tests/navigation/isActiveRouteTests.tsx index 18ff8471d2e8..4e37b9c919db 100644 --- a/tests/navigation/isActiveRouteTests.tsx +++ b/tests/navigation/isActiveRouteTests.tsx @@ -10,25 +10,6 @@ import type {Route} from '@src/ROUTES'; import SCREENS from '@src/SCREENS'; import TestNavigationContainer from '../utils/TestNavigationContainer'; -afterEach(() => { - // Ensure mounted components are unmounted - cleanup(); - - // Clear timers and restore real timers (in case fake timers are used anywhere) - jest.clearAllTimers(); - jest.useRealTimers(); - - // Reset any mocks used by this file - jest.restoreAllMocks(); - jest.resetModules(); - - // Clear the navigation ref so listeners/hooks attached to it don't keep the worker alive. - // This is intentionally type-unsafe to forcibly drop the ref between tests. - if (navigationRef.current) { - navigationRef.current = null; - } -}); - jest.mock('@hooks/useResponsiveLayout', () => jest.fn()); jest.mock('@libs/getIsNarrowLayout', () => jest.fn()); @@ -39,6 +20,24 @@ const mockedGetIsNarrowLayout = getIsNarrowLayout as jest.MockedFunction; describe('Navigation', () => { + afterEach(() => { + // Ensure mounted components are unmounted + cleanup(); + + // Clear timers and restore real timers (in case fake timers are used anywhere) + jest.clearAllTimers(); + jest.useRealTimers(); + + // Reset any mocks used by this file + jest.restoreAllMocks(); + jest.resetModules(); + + // Clear the navigation ref so listeners/hooks attached to it don't keep the worker alive. + // This is intentionally type-unsafe to forcibly drop the ref between tests. + if (navigationRef.current) { + navigationRef.current = null; + } + }); beforeEach(() => { mockedGetIsNarrowLayout.mockReturnValue(true); mockedUseResponsiveLayout.mockReturnValue({...CONST.NAVIGATION_TESTS.DEFAULT_USE_RESPONSIVE_LAYOUT_VALUE, shouldUseNarrowLayout: true});