diff --git a/client/src/client/workers/test-evaluator.js b/client/src/client/workers/test-evaluator.js index 172b42e533..2a8a1eaec6 100644 --- a/client/src/client/workers/test-evaluator.js +++ b/client/src/client/workers/test-evaluator.js @@ -43,6 +43,7 @@ const __utils = (() => { }; })(); +/* Run the test if there is one. If not just evaluate the user code */ self.onmessage = async e => { /* eslint-disable no-unused-vars */ const { code = '' } = e.data; @@ -64,10 +65,13 @@ self.onmessage = async e => { if (__userCodeWasExecuted) { // rethrow error, since test failed. throw err; - } else { - // report errors to dev console (not the editor console, since the test - // may still pass) + } else if (e.data.testString) { + // report errors to dev console if tests are running (since some + // challenges should pass with code that throws errors) __utils.oldLog(err); + } else { + // user is editing code, so both consoles should report errors + console.log(err.toString()); } testResult = eval(e.data.testString); } diff --git a/client/src/templates/Challenges/redux/execute-challenge-saga.js b/client/src/templates/Challenges/redux/execute-challenge-saga.js index d68a77ca3b..e4dbf9a771 100644 --- a/client/src/templates/Challenges/redux/execute-challenge-saga.js +++ b/client/src/templates/Challenges/redux/execute-challenge-saga.js @@ -29,7 +29,8 @@ import { buildChallenge, getTestRunner, challengeHasPreview, - updatePreview + updatePreview, + isJavaScriptChallenge } from '../utils/build'; export function* executeChallengeSaga() { @@ -118,6 +119,7 @@ function* executeTests(testRunner, tests, testTimeout = 5000) { return testResults; } +// updates preview frame and the fcc console. function* previewChallengeSaga() { yield delay(700); @@ -134,16 +136,17 @@ function* previewChallengeSaga() { const proxyLogger = args => consoleProxy.put(args); const challengeData = yield select(challengeDataSelector); - // try to build even if there's no preview so build errors will be reported. const buildData = yield buildChallengeData(challengeData); - // then only continue if there is a preview. - if (!challengeHasPreview(challengeData)) { - return; + // evaluate the user code in the preview frame or in the worker + if (challengeHasPreview(challengeData)) { + const document = yield getContext('document'); + yield call(updatePreview, buildData, document, proxyLogger); + } else if (isJavaScriptChallenge(challengeData)) { + const runUserCode = getTestRunner(buildData, proxyLogger); + // without a testString the testRunner just evaluates the user's code + yield call(runUserCode, null, 5000); } - const document = yield getContext('document'); - yield call(updatePreview, buildData, document, proxyLogger); - // We don't want to see the default console, so we initialise and output in - // one call. + // To avoid seeing the default console, initialise and output in one call. yield all([put(initConsole('')), put(logsToConsole('// console output'))]); } catch (err) { console.error(err); diff --git a/client/src/templates/Challenges/utils/build.js b/client/src/templates/Challenges/utils/build.js index 07d5689df4..2f6240d356 100644 --- a/client/src/templates/Challenges/utils/build.js +++ b/client/src/templates/Challenges/utils/build.js @@ -177,3 +177,7 @@ export function challengeHasPreview({ challengeType }) { challengeType === challengeTypes.modern ); } + +export function isJavaScriptChallenge({ challengeType }) { + return challengeType === challengeTypes.js; +}