LogBoxData test migrated to Jest modern timers (#55757)

Summary:
This PR migrates the LogBoxData tests so that it uses Jest modern timers.
I removed the `legacyFakeTimers` property from  `useFakeTimers` (like I did in the previous PR for Pressability tests https://github.com/facebook/react-native/issues/55410), and moved it to a `beforeEach`. This in combination with restoring real timers in `afterEach` improves tests reliability and isolation.
Then I modified some tests that started to fail by adding explicit  `flushToObservers()`.
The extra flushToObservers() calls are necessary because addLog/addException schedule processing using `setImmediate`, and inside that callback `handleUpdate()` schedules observer notifications using another `setImmediate` (so we need to flush twice to simulate the correct behaviour like it was already done in some other tests in the test suite).

## Changelog:

[GENERAL] [CHANGED] - Migrated LogBoxData tests to Jest modern timers

Pull Request resolved: https://github.com/facebook/react-native/pull/55757

Test Plan:
- Ran LogBoxData-test and verify all tests cases passed
- Ran the React Native test suite to ensure all tests pass
- Verified test correctness by intentionally breaking production code after fixing the tests (eg. removing the `handleUpdate` call in `addException`,  that changes the call to the `observer`)

Reviewed By: fabriziocucci

Differential Revision: D94903193

Pulled By: cortinico

fbshipit-source-id: caf497b77610c701849ceab5068e54589684d2ab
This commit is contained in:
Fabrizio Duroni authored and meta-codesync[bot] committed 2026-03-03 06:46:45 -08:00
1 parent 422770d5a4
commit dea2136a49
1 file changed
+17 -7
@@ -10,9 +10,6 @@
'use strict';
// TODO(legacy-fake-timers): Fix these tests to work with modern timers.
jest.useFakeTimers({legacyFakeTimers: true});
jest.mock('../../../Core/Devtools/parseErrorStack', () => {
return {__esModule: true, default: jest.fn(() => [])};
});
@@ -144,16 +141,17 @@ const addSyntaxError = (options: $FlowFixMe) => {
);
};
beforeEach(() => {
jest.resetModules();
});
const flushToObservers = () => {
// Observer updates are debounced and need to advance timers to flush.
jest.runOnlyPendingTimers();
};
describe('LogBoxData', () => {
beforeEach(() => {
jest.useFakeTimers();
jest.resetModules();
});
it('adds and dismisses logs', () => {
addLogs(['A']);
addSoftErrors(['B']);
@@ -590,9 +588,13 @@ describe('LogBoxData', () => {
expect(observer.mock.calls.length).toBe(1);
addLogs(['A']);
flushToObservers();
addSoftErrors(['B']);
flushToObservers();
addFatalErrors(['C']);
flushToObservers();
addSyntaxError();
flushToObservers();
expect(observer.mock.calls.length).toBe(5);
LogBoxData.clearWarnings();
@@ -610,9 +612,13 @@ describe('LogBoxData', () => {
expect(observer.mock.calls.length).toBe(1);
addLogs(['A']);
flushToObservers();
addSoftErrors(['B']);
flushToObservers();
addFatalErrors(['C']);
flushToObservers();
addSyntaxError();
flushToObservers();
expect(observer.mock.calls.length).toBe(5);
LogBoxData.clearErrors();
@@ -854,4 +860,8 @@ describe('LogBoxData', () => {
expect(Array.from(observerAfter.mock.calls[0][0].logs).length).toBe(0);
});
});
afterEach(() => {
jest.useRealTimers();
});
});