Fix Fantom tests to avoid logging errors to test output (#51346)

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

Changelog: [internal]

I saw that this test was logging errors to the console, which is considered a bad practice in Jest tests.

This prevents the logs from being printed in the test output and also adds assertions to verify what should be logged.

Reviewed By: rshest

Differential Revision: D74803463

fbshipit-source-id: 9c840a51e0e616a6bb15b7a40b3a6937fcb88b64
This commit is contained in:
Rubén Norte
2025-05-15 05:27:02 -07:00
committed by Facebook GitHub Bot
parent bf26cf9c31
commit ca764bb511
+40 -18
View File
@@ -245,29 +245,51 @@ describe('Fantom', () => {
Fantom.runWorkLoop();
});
it('should throw an error when running a task with LogBox installed', () => {
LogBox.install();
describe('when LogBox is installed', () => {
let originalConsoleError;
expect(() => {
Fantom.runTask(() => {});
}).toThrow(
'Cannot run work loop while LogBox is installed, as LogBox intercepts errors thrown in tests.' +
' If you are installing LogBox unintentionally using `InitializeCore`, replace it with `@react-native/fantom/src/setUpDefaultReactNativeEnvironment` to avoid this problem.',
);
beforeEach(() => {
LogBox.install();
// We need to do this cleanup or Fantom will fail the test for us.
LogBox.uninstall();
Fantom.runWorkLoop();
});
originalConsoleError = console.error;
it('should not throw an error when running a task with LogBox installed if setLogBoxCheckEnabled is set to false', () => {
LogBox.install();
// $FlowExpectedError[cannot-write]
console.error = jest.fn();
});
Fantom.setLogBoxCheckEnabled(false);
afterEach(() => {
LogBox.uninstall();
expect(() => {
Fantom.runTask(() => {});
}).not.toThrow();
// $FlowExpectedError[cannot-write]
console.error = originalConsoleError;
});
it('should throw an error when running a task', () => {
const expectedErrorMessage =
'Cannot run work loop while LogBox is installed, as LogBox intercepts errors thrown in tests.' +
' If you are installing LogBox unintentionally using `InitializeCore`, replace it with `@react-native/fantom/src/setUpDefaultReactNativeEnvironment` to avoid this problem.';
expect(() => {
Fantom.runTask(() => {});
}).toThrow(expectedErrorMessage);
expect(console.error).toHaveBeenCalledTimes(1);
expect(console.error).toHaveBeenCalledWith(expectedErrorMessage);
// We need to do this cleanup or Fantom will fail the test for us.
LogBox.uninstall();
Fantom.runWorkLoop();
});
it('should not throw an error if setLogBoxCheckEnabled is set to false', () => {
Fantom.setLogBoxCheckEnabled(false);
expect(() => {
Fantom.runTask(() => {});
}).not.toThrow();
expect(console.error).not.toHaveBeenCalled();
});
});
});
});