Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix crashes related to loading React 16.4.1 #558

Merged
merged 2 commits into from
Jun 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/React.Core/JavaScriptEngineFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ protected virtual void InitialiseEngine(IJsEngine engine)
}

LoadUserScripts(engine);
if (!_config.LoadReact)
if (!_config.LoadReact && _scriptLoadException == null)
{
// We expect to user to have loaded their own version of React in the scripts that
// were loaded above, let's ensure that's the case.
Expand Down
20 changes: 17 additions & 3 deletions src/React.Core/Resources/shims.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/

var global = global || {};
var React, ReactDOM, ReactDOMServer;
var React, ReactDOM, ReactDOMServer, setTimeout, clearTimeout;

// Basic console shim. Caches all calls to console methods.
function MockConsole() {
Expand Down Expand Up @@ -68,6 +68,20 @@ function ReactNET_initReact() {
return false;
}

setTimeout = setTimeout || global.setTimeout;
if (setTimeout === undefined) {
setTimeout = function() {
throw new Error('setTimeout is not supported in server-rendered Javascript.');
}
}

clearTimeout = clearTimeout || global.clearTimeout;
if (clearTimeout === undefined) {
clearTimeout = function() {
throw new Error('clearTimeout is not supported in server-rendered Javascript.');
}
}

/**
* Polyfill for engines that do not support Object.assign
*/
Expand All @@ -94,4 +108,4 @@ if (typeof Object.assign !== 'function') {
}
return to;
};
}
}
22 changes: 22 additions & 0 deletions tests/React.Tests/Core/JavaScriptEngineFactoryTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,28 @@ public void ShouldThrowIfReactVersionNotLoaded()
});
}

[Fact]
public void ShouldThrowScriptErrorIfReactFails()
{
var config = new Mock<IReactSiteConfiguration>();
config.Setup(x => x.ScriptsWithoutTransform).Returns(new List<string> {"foo.js"});
config.Setup(x => x.LoadReact).Returns(false);
var fileSystem = new Mock<IFileSystem>();
fileSystem.Setup(x => x.ReadAsString("foo.js")).Returns("FAIL PLZ");

var jsEngine = new Mock<IJsEngine>();
jsEngine.Setup(x => x.Evaluate<int>("1 + 1")).Returns(2);
jsEngine.Setup(x => x.Execute("FAIL PLZ")).Throws(new JsRuntimeException("Fail")
{
LineNumber = 42,
ColumnNumber = 911,
});
var factory = CreateFactory(config, fileSystem, () => jsEngine.Object);

var ex = Assert.Throws<ReactScriptLoadException>(() => factory.GetEngineForCurrentThread());
Assert.Equal("Error while loading \"foo.js\": Fail\r\nLine: 42\r\nColumn: 911", ex.Message);
}

[Fact]
public void ShouldCatchErrorsWhileLoadingScripts()
{
Expand Down