Skip to content

Commit dea2136

Browse files
chiciometa-codesync[bot]
authored andcommitted
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 #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: #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
1 parent 422770d commit dea2136

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

packages/react-native/Libraries/LogBox/Data/__tests__/LogBoxData-test.js

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@
1010

1111
'use strict';
1212

13-
// TODO(legacy-fake-timers): Fix these tests to work with modern timers.
14-
jest.useFakeTimers({legacyFakeTimers: true});
15-
1613
jest.mock('../../../Core/Devtools/parseErrorStack', () => {
1714
return {__esModule: true, default: jest.fn(() => [])};
1815
});
@@ -144,16 +141,17 @@ const addSyntaxError = (options: $FlowFixMe) => {
144141
);
145142
};
146143

147-
beforeEach(() => {
148-
jest.resetModules();
149-
});
150-
151144
const flushToObservers = () => {
152145
// Observer updates are debounced and need to advance timers to flush.
153146
jest.runOnlyPendingTimers();
154147
};
155148

156149
describe('LogBoxData', () => {
150+
beforeEach(() => {
151+
jest.useFakeTimers();
152+
jest.resetModules();
153+
});
154+
157155
it('adds and dismisses logs', () => {
158156
addLogs(['A']);
159157
addSoftErrors(['B']);
@@ -590,9 +588,13 @@ describe('LogBoxData', () => {
590588
expect(observer.mock.calls.length).toBe(1);
591589

592590
addLogs(['A']);
591+
flushToObservers();
593592
addSoftErrors(['B']);
593+
flushToObservers();
594594
addFatalErrors(['C']);
595+
flushToObservers();
595596
addSyntaxError();
597+
flushToObservers();
596598
expect(observer.mock.calls.length).toBe(5);
597599

598600
LogBoxData.clearWarnings();
@@ -610,9 +612,13 @@ describe('LogBoxData', () => {
610612
expect(observer.mock.calls.length).toBe(1);
611613

612614
addLogs(['A']);
615+
flushToObservers();
613616
addSoftErrors(['B']);
617+
flushToObservers();
614618
addFatalErrors(['C']);
619+
flushToObservers();
615620
addSyntaxError();
621+
flushToObservers();
616622
expect(observer.mock.calls.length).toBe(5);
617623

618624
LogBoxData.clearErrors();
@@ -854,4 +860,8 @@ describe('LogBoxData', () => {
854860
expect(Array.from(observerAfter.mock.calls[0][0].logs).length).toBe(0);
855861
});
856862
});
863+
864+
afterEach(() => {
865+
jest.useRealTimers();
866+
});
857867
});

0 commit comments

Comments
 (0)