Skip to content

Commit ca764bb

Browse files
rubennortefacebook-github-bot
authored andcommitted
Fix Fantom tests to avoid logging errors to test output (#51346)
Summary: Pull Request resolved: #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
1 parent bf26cf9 commit ca764bb

File tree

1 file changed

+40
-18
lines changed

1 file changed

+40
-18
lines changed

packages/react-native-fantom/src/__tests__/Fantom-itest.js

+40-18
Original file line numberDiff line numberDiff line change
@@ -245,29 +245,51 @@ describe('Fantom', () => {
245245
Fantom.runWorkLoop();
246246
});
247247

248-
it('should throw an error when running a task with LogBox installed', () => {
249-
LogBox.install();
248+
describe('when LogBox is installed', () => {
249+
let originalConsoleError;
250250

251-
expect(() => {
252-
Fantom.runTask(() => {});
253-
}).toThrow(
254-
'Cannot run work loop while LogBox is installed, as LogBox intercepts errors thrown in tests.' +
255-
' If you are installing LogBox unintentionally using `InitializeCore`, replace it with `@react-native/fantom/src/setUpDefaultReactNativeEnvironment` to avoid this problem.',
256-
);
251+
beforeEach(() => {
252+
LogBox.install();
257253

258-
// We need to do this cleanup or Fantom will fail the test for us.
259-
LogBox.uninstall();
260-
Fantom.runWorkLoop();
261-
});
254+
originalConsoleError = console.error;
262255

263-
it('should not throw an error when running a task with LogBox installed if setLogBoxCheckEnabled is set to false', () => {
264-
LogBox.install();
256+
// $FlowExpectedError[cannot-write]
257+
console.error = jest.fn();
258+
});
265259

266-
Fantom.setLogBoxCheckEnabled(false);
260+
afterEach(() => {
261+
LogBox.uninstall();
267262

268-
expect(() => {
269-
Fantom.runTask(() => {});
270-
}).not.toThrow();
263+
// $FlowExpectedError[cannot-write]
264+
console.error = originalConsoleError;
265+
});
266+
267+
it('should throw an error when running a task', () => {
268+
const expectedErrorMessage =
269+
'Cannot run work loop while LogBox is installed, as LogBox intercepts errors thrown in tests.' +
270+
' If you are installing LogBox unintentionally using `InitializeCore`, replace it with `@react-native/fantom/src/setUpDefaultReactNativeEnvironment` to avoid this problem.';
271+
272+
expect(() => {
273+
Fantom.runTask(() => {});
274+
}).toThrow(expectedErrorMessage);
275+
276+
expect(console.error).toHaveBeenCalledTimes(1);
277+
expect(console.error).toHaveBeenCalledWith(expectedErrorMessage);
278+
279+
// We need to do this cleanup or Fantom will fail the test for us.
280+
LogBox.uninstall();
281+
Fantom.runWorkLoop();
282+
});
283+
284+
it('should not throw an error if setLogBoxCheckEnabled is set to false', () => {
285+
Fantom.setLogBoxCheckEnabled(false);
286+
287+
expect(() => {
288+
Fantom.runTask(() => {});
289+
}).not.toThrow();
290+
291+
expect(console.error).not.toHaveBeenCalled();
292+
});
271293
});
272294
});
273295
});

0 commit comments

Comments
 (0)