From 1dca01b532b5ba633df17cbf6b94d30e9a9a6ada Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ramos=20Ortiz?= Date: Tue, 31 Oct 2017 14:18:00 -0700 Subject: [PATCH] Remove ReactNativeVersionCheck __DEV__ gating, print error instead of throwing Reviewed By: cblappert Differential Revision: D6192020 fbshipit-source-id: 026f376d6d43ab3de188f94f2a4d5f0cf485733c --- Libraries/Core/InitializeCore.js | 6 +-- Libraries/Core/ReactNativeVersionCheck.js | 2 +- .../__tests__/ReactNativeVersionCheck-test.js | 43 +++++++++++++------ 3 files changed, 34 insertions(+), 17 deletions(-) diff --git a/Libraries/Core/InitializeCore.js b/Libraries/Core/InitializeCore.js index ecf661759d965a..90e9360cb90919 100644 --- a/Libraries/Core/InitializeCore.js +++ b/Libraries/Core/InitializeCore.js @@ -126,10 +126,8 @@ if (!global.__fbDisableExceptionsManager) { } // Check for compatibility between the JS and native code -if (__DEV__) { - const ReactNativeVersionCheck = require('ReactNativeVersionCheck'); - ReactNativeVersionCheck.checkVersions(); -} +const ReactNativeVersionCheck = require('ReactNativeVersionCheck'); +ReactNativeVersionCheck.checkVersions(); // Set up Promise // The native Promise implementation throws the following error: diff --git a/Libraries/Core/ReactNativeVersionCheck.js b/Libraries/Core/ReactNativeVersionCheck.js index 33e494e5887d85..39c416118d896e 100644 --- a/Libraries/Core/ReactNativeVersionCheck.js +++ b/Libraries/Core/ReactNativeVersionCheck.js @@ -34,7 +34,7 @@ exports.checkVersions = function checkVersions(): void { ReactNativeVersion.version.major !== nativeVersion.major || ReactNativeVersion.version.minor !== nativeVersion.minor ) { - throw new Error( + console.error( `React Native version mismatch.\n\nJavaScript version: ${_formatVersion( ReactNativeVersion.version, )}\n` + diff --git a/Libraries/Core/__tests__/ReactNativeVersionCheck-test.js b/Libraries/Core/__tests__/ReactNativeVersionCheck-test.js index e1b30001d2a3f8..b85abbdea6db7e 100644 --- a/Libraries/Core/__tests__/ReactNativeVersionCheck-test.js +++ b/Libraries/Core/__tests__/ReactNativeVersionCheck-test.js @@ -36,8 +36,24 @@ function _setDevelopmentModeForTests(dev) { } function _defineCheckVersionTests() { + const consoleError = console.error; + const globalConsole = global.console; + + let spyOnConsoleError; + let consoleOutput; + + beforeEach(() => { + consoleOutput = ''; + console.error = jest.fn(); + global.console = {error: jest.fn(error => (consoleOutput += error))}; + spyOnConsoleError = jest.spyOn(global.console, 'error'); + }); + afterEach(() => { jest.resetModules(); + console.error = consoleError; + global.console = globalConsole; + spyOnConsoleError.mockReset(); }); it('passes when all the versions are zero', () => { @@ -60,40 +76,43 @@ function _defineCheckVersionTests() { expect(() => ReactNativeVersionCheck.checkVersions()).not.toThrow(); }); - it("throws when the minor doesn't match when the major is zero", () => { + it("logs error when the minor doesn't match when the major is zero", () => { _mockJsVersion(0, 1, 0); _mockNativeVersion(0, 2, 0); const ReactNativeVersionCheck = require('ReactNativeVersionCheck'); - expect(() => ReactNativeVersionCheck.checkVersions()).toThrowError( - /React Native version mismatch/, - ); + + ReactNativeVersionCheck.checkVersions(); + expect(spyOnConsoleError).toHaveBeenCalledTimes(1); + expect(consoleOutput).toMatch(/React Native version mismatch/); }); - it("throws when the major doesn't match", () => { + it("logs error when the major doesn't match", () => { _mockJsVersion(1, 0, 0); _mockNativeVersion(2, 0, 0); const ReactNativeVersionCheck = require('ReactNativeVersionCheck'); - expect(() => ReactNativeVersionCheck.checkVersions()).toThrowError( - /React Native version mismatch/, - ); + ReactNativeVersionCheck.checkVersions(); + expect(spyOnConsoleError).toHaveBeenCalledTimes(1); + expect(consoleOutput).toMatch(/React Native version mismatch/); }); - it("doesn't throw if the patch doesn't match", () => { + it("doesn't log error if the patch doesn't match", () => { _mockJsVersion(0, 1, 0); _mockNativeVersion(0, 1, 2); const ReactNativeVersionCheck = require('ReactNativeVersionCheck'); - expect(() => ReactNativeVersionCheck.checkVersions()).not.toThrow(); + ReactNativeVersionCheck.checkVersions(); + expect(spyOnConsoleError).toHaveBeenCalledTimes(0); }); - it("doesn't throw if the prerelease doesn't match", () => { + it("doesn't log error if the prerelease doesn't match", () => { _mockJsVersion(0, 1, 0, 'beta.0'); _mockNativeVersion(0, 1, 0, 'alpha.1'); const ReactNativeVersionCheck = require('ReactNativeVersionCheck'); - expect(() => ReactNativeVersionCheck.checkVersions()).not.toThrow(); + ReactNativeVersionCheck.checkVersions(); + expect(spyOnConsoleError).toHaveBeenCalledTimes(0); }); }