Skip to content

Commit

Permalink
RN: Eliminate Jest Log Spew
Browse files Browse the repository at this point in the history
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
  • Loading branch information
yungsters authored and facebook-github-bot committed Nov 10, 2021
1 parent 148c98e commit 36bbd8f
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 21 deletions.
14 changes: 1 addition & 13 deletions Libraries/Components/TextInput/__tests__/TextInput-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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', () => {
Expand Down
2 changes: 1 addition & 1 deletion Libraries/Core/Timers/__tests__/JSTimers-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});

Expand Down
2 changes: 1 addition & 1 deletion Libraries/Image/__tests__/AssetUtils-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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('');
Expand Down
2 changes: 1 addition & 1 deletion Libraries/Lists/__tests__/VirtualizedList-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
14 changes: 9 additions & 5 deletions Libraries/LogBox/__tests__/LogBox-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 4 additions & 0 deletions jest/mockNativeComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit 36bbd8f

Please sign in to comment.