diff --git a/src/React.Core/JavaScriptEngineFactory.cs b/src/React.Core/JavaScriptEngineFactory.cs index e8905b2d0..3f763e1ae 100644 --- a/src/React.Core/JavaScriptEngineFactory.cs +++ b/src/React.Core/JavaScriptEngineFactory.cs @@ -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. diff --git a/src/React.Core/Resources/shims.js b/src/React.Core/Resources/shims.js index ba2edb908..4c83560cf 100644 --- a/src/React.Core/Resources/shims.js +++ b/src/React.Core/Resources/shims.js @@ -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() { @@ -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 */ @@ -94,4 +108,4 @@ if (typeof Object.assign !== 'function') { } return to; }; -} \ No newline at end of file +} diff --git a/tests/React.Tests/Core/JavaScriptEngineFactoryTest.cs b/tests/React.Tests/Core/JavaScriptEngineFactoryTest.cs index 925243071..9a076ba83 100644 --- a/tests/React.Tests/Core/JavaScriptEngineFactoryTest.cs +++ b/tests/React.Tests/Core/JavaScriptEngineFactoryTest.cs @@ -164,6 +164,28 @@ public void ShouldThrowIfReactVersionNotLoaded() }); } + [Fact] + public void ShouldThrowScriptErrorIfReactFails() + { + var config = new Mock(); + config.Setup(x => x.ScriptsWithoutTransform).Returns(new List {"foo.js"}); + config.Setup(x => x.LoadReact).Returns(false); + var fileSystem = new Mock(); + fileSystem.Setup(x => x.ReadAsString("foo.js")).Returns("FAIL PLZ"); + + var jsEngine = new Mock(); + jsEngine.Setup(x => x.Evaluate("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(() => factory.GetEngineForCurrentThread()); + Assert.Equal("Error while loading \"foo.js\": Fail\r\nLine: 42\r\nColumn: 911", ex.Message); + } + [Fact] public void ShouldCatchErrorsWhileLoadingScripts() {