From 36bbd8fa31eceabf0fbf52b8daccc848e9c5830c Mon Sep 17 00:00:00 2001 From: Tim Yung Date: Tue, 9 Nov 2021 23:40:55 -0800 Subject: [PATCH] RN: Eliminate Jest Log Spew Summary: Eliminates all of the console logs that appear when successfully running Jest tests for React Native. Changelog: [Internal] Reviewed By: lunaleaps Differential Revision: D32304619 fbshipit-source-id: 8bc8ef9337ae6af588238cec7cfb874ac6067340 --- .../TextInput/__tests__/TextInput-test.js | 14 +------------- Libraries/Core/Timers/__tests__/JSTimers-test.js | 2 +- Libraries/Image/__tests__/AssetUtils-test.js | 2 +- Libraries/Lists/__tests__/VirtualizedList-test.js | 2 +- Libraries/LogBox/__tests__/LogBox-test.js | 14 +++++++++----- jest/mockNativeComponent.js | 4 ++++ 6 files changed, 17 insertions(+), 21 deletions(-) diff --git a/Libraries/Components/TextInput/__tests__/TextInput-test.js b/Libraries/Components/TextInput/__tests__/TextInput-test.js index 5d912d78aef..fb60e30e671 100644 --- a/Libraries/Components/TextInput/__tests__/TextInput-test.js +++ b/Libraries/Components/TextInput/__tests__/TextInput-test.js @@ -102,15 +102,10 @@ describe('TextInput tests', () => { TextInput.State.focusTextInput(textInputRef.current); expect(textInputRef.current.isFocused()).toBe(true); expect(TextInput.State.currentlyFocusedInput()).toBe(textInputRef.current); - // This function is currently deprecated and will be removed in the future - expect(TextInput.State.currentlyFocusedField()).toBe( - ReactNative.findNodeHandle(textInputRef.current), - ); + TextInput.State.blurTextInput(textInputRef.current); expect(textInputRef.current.isFocused()).toBe(false); expect(TextInput.State.currentlyFocusedInput()).toBe(null); - // This function is currently deprecated and will be removed in the future - expect(TextInput.State.currentlyFocusedField()).toBe(null); }); it('should unfocus when other TextInput is focused', () => { @@ -144,24 +139,17 @@ describe('TextInput tests', () => { expect(textInputRe1.current.isFocused()).toBe(false); expect(textInputRe2.current.isFocused()).toBe(false); - const inputTag1 = ReactNative.findNodeHandle(textInputRe1.current); - const inputTag2 = ReactNative.findNodeHandle(textInputRe2.current); - TextInput.State.focusTextInput(textInputRe1.current); expect(textInputRe1.current.isFocused()).toBe(true); expect(textInputRe2.current.isFocused()).toBe(false); expect(TextInput.State.currentlyFocusedInput()).toBe(textInputRe1.current); - // This function is currently deprecated and will be removed in the future - expect(TextInput.State.currentlyFocusedField()).toBe(inputTag1); TextInput.State.focusTextInput(textInputRe2.current); expect(textInputRe1.current.isFocused()).toBe(false); expect(textInputRe2.current.isFocused()).toBe(true); expect(TextInput.State.currentlyFocusedInput()).toBe(textInputRe2.current); - // This function is currently deprecated and will be removed in the future - expect(TextInput.State.currentlyFocusedField()).toBe(inputTag2); }); it('should render as expected', () => { diff --git a/Libraries/Core/Timers/__tests__/JSTimers-test.js b/Libraries/Core/Timers/__tests__/JSTimers-test.js index 2df2001c7e3..d5f2dabf8f9 100644 --- a/Libraries/Core/Timers/__tests__/JSTimers-test.js +++ b/Libraries/Core/Timers/__tests__/JSTimers-test.js @@ -28,7 +28,7 @@ const JSTimers = require('../JSTimers'); describe('JSTimers', function () { beforeEach(function () { - jest.spyOn(console, 'warn'); + jest.spyOn(console, 'warn').mockReturnValue(undefined); global.setTimeout = JSTimers.setTimeout; }); diff --git a/Libraries/Image/__tests__/AssetUtils-test.js b/Libraries/Image/__tests__/AssetUtils-test.js index a5315293b9c..f1cbe77e535 100644 --- a/Libraries/Image/__tests__/AssetUtils-test.js +++ b/Libraries/Image/__tests__/AssetUtils-test.js @@ -17,7 +17,7 @@ describe('AssetUtils', () => { }); it('should return empty string and warn once if no cacheBreaker set (DEV)', () => { - const mockWarn = jest.spyOn(console, 'warn'); + const mockWarn = jest.spyOn(console, 'warn').mockReturnValue(undefined); global.__DEV__ = true; expect(getUrlCacheBreaker()).toEqual(''); expect(getUrlCacheBreaker()).toEqual(''); diff --git a/Libraries/Lists/__tests__/VirtualizedList-test.js b/Libraries/Lists/__tests__/VirtualizedList-test.js index 6fd9d915f47..b2093ce07c1 100644 --- a/Libraries/Lists/__tests__/VirtualizedList-test.js +++ b/Libraries/Lists/__tests__/VirtualizedList-test.js @@ -362,7 +362,7 @@ describe('VirtualizedList', () => { const layout = {width: 300, height: 600}; let data = Array(20) .fill() - .map((_, key) => ({key: String(key)})); + .map((_, index) => ({key: `key-${index}`})); const onEndReached = jest.fn(); const props = { data, diff --git a/Libraries/LogBox/__tests__/LogBox-test.js b/Libraries/LogBox/__tests__/LogBox-test.js index 4c8144c7334..75b80cfb36a 100644 --- a/Libraries/LogBox/__tests__/LogBox-test.js +++ b/Libraries/LogBox/__tests__/LogBox-test.js @@ -30,20 +30,20 @@ function mockFilterResult(returnValues) { } describe('LogBox', () => { - const {error, warn} = console; - let consoleError; - let consoleWarn; + const {error, log, warn} = console; beforeEach(() => { jest.resetModules(); - console.error = consoleError = jest.fn(); - console.warn = consoleWarn = jest.fn(); + console.error = jest.fn(); + console.log = jest.fn(); + console.warn = jest.fn(); console.disableYellowBox = false; }); afterEach(() => { LogBox.uninstall(); console.error = error; + console.log = log; console.warn = warn; }); @@ -329,6 +329,8 @@ describe('LogBox', () => { }); it('preserves decorations of console.error after installing/uninstalling', () => { + const consoleError = console.error; + LogBox.install(); const originalConsoleError = console.error; @@ -356,6 +358,8 @@ describe('LogBox', () => { }); it('preserves decorations of console.warn after installing/uninstalling', () => { + const consoleWarn = console.warn; + LogBox.install(); const originalConsoleWarn = console.warn; diff --git a/jest/mockNativeComponent.js b/jest/mockNativeComponent.js index e6080b7d149..f6545e3bcff 100644 --- a/jest/mockNativeComponent.js +++ b/jest/mockNativeComponent.js @@ -11,8 +11,12 @@ const React = require('react'); +let nativeTag = 1; + module.exports = viewName => { const Component = class extends React.Component { + _nativeTag = nativeTag++; + render() { return React.createElement(viewName, this.props, this.props.children); }