Skip to content

Commit

Permalink
Remove ReactNativeVersionCheck __DEV__ gating, print error instead of…
Browse files Browse the repository at this point in the history
… throwing

Reviewed By: cblappert

Differential Revision: D6192020

fbshipit-source-id: 026f376d6d43ab3de188f94f2a4d5f0cf485733c
  • Loading branch information
hramos authored and facebook-github-bot committed Oct 31, 2017
1 parent 870f540 commit 1dca01b
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 17 deletions.
6 changes: 2 additions & 4 deletions Libraries/Core/InitializeCore.js
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion Libraries/Core/ReactNativeVersionCheck.js
Original file line number Diff line number Diff line change
Expand Up @@ -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` +
Expand Down
43 changes: 31 additions & 12 deletions Libraries/Core/__tests__/ReactNativeVersionCheck-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand All @@ -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);
});
}

Expand Down

0 comments on commit 1dca01b

Please sign in to comment.