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 => { self.onmessage = async e => {
/* eslint-disable no-unused-vars */ /* eslint-disable no-unused-vars */
const { code = '' } = e.data; const { code = '' } = e.data;
@ -64,10 +65,13 @@ self.onmessage = async e => {
if (__userCodeWasExecuted) { if (__userCodeWasExecuted) {
// rethrow error, since test failed. // rethrow error, since test failed.
throw err; throw err;
} else { } else if (e.data.testString) {
// report errors to dev console (not the editor console, since the test // report errors to dev console if tests are running (since some
// may still pass) // challenges should pass with code that throws errors)
__utils.oldLog(err); __utils.oldLog(err);
} else {
// user is editing code, so both consoles should report errors
console.log(err.toString());
} }
testResult = eval(e.data.testString); testResult = eval(e.data.testString);
} }

View File

@ -29,7 +29,8 @@ import {
buildChallenge, buildChallenge,
getTestRunner, getTestRunner,
challengeHasPreview, challengeHasPreview,
updatePreview updatePreview,
isJavaScriptChallenge
} from '../utils/build'; } from '../utils/build';
export function* executeChallengeSaga() { export function* executeChallengeSaga() {
@ -118,6 +119,7 @@ function* executeTests(testRunner, tests, testTimeout = 5000) {
return testResults; return testResults;
} }
// updates preview frame and the fcc console.
function* previewChallengeSaga() { function* previewChallengeSaga() {
yield delay(700); yield delay(700);
@ -134,16 +136,17 @@ function* previewChallengeSaga() {
const proxyLogger = args => consoleProxy.put(args); const proxyLogger = args => consoleProxy.put(args);
const challengeData = yield select(challengeDataSelector); 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); const buildData = yield buildChallengeData(challengeData);
// then only continue if there is a preview. // evaluate the user code in the preview frame or in the worker
if (!challengeHasPreview(challengeData)) { if (challengeHasPreview(challengeData)) {
return;
}
const document = yield getContext('document'); const document = yield getContext('document');
yield call(updatePreview, buildData, document, proxyLogger); yield call(updatePreview, buildData, document, proxyLogger);
// We don't want to see the default console, so we initialise and output in } else if (isJavaScriptChallenge(challengeData)) {
// one call. 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'))]); yield all([put(initConsole('')), put(logsToConsole('// console output'))]);
} catch (err) { } catch (err) {
console.error(err); console.error(err);

View File

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