fix: output console.logs as user types (JS)

This commit is contained in:
Oliver Eyton-Williams
2019-11-02 12:03:47 +01:00
committed by mrugesh
parent 29641986ab
commit 64fcbbb491
3 changed files with 23 additions and 12 deletions

View File

@ -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);
}

View File

@ -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);
// We don't want to see the default console, so we initialise and output in
// one call.
} 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);
}
// 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);

View File

@ -177,3 +177,7 @@ export function challengeHasPreview({ challengeType }) {
challengeType === challengeTypes.modern
);
}
export function isJavaScriptChallenge({ challengeType }) {
return challengeType === challengeTypes.js;
}