fix(console): output user logs after running tests

This commit is contained in:
Oliver Eyton-Williams
2018-09-11 20:34:08 +02:00
committed by Mrugesh Mohapatra
parent 0dc455c5dc
commit 54e14b9433
4 changed files with 12 additions and 27 deletions

View File

@ -31,7 +31,6 @@ const babelTransformCode = code => Babel.transform(code, babelOptions).code;
// const sourceReg = // const sourceReg =
// /(<!-- fcc-start-source -->)([\s\S]*?)(?=<!-- fcc-end-source -->)/g; // /(<!-- fcc-start-source -->)([\s\S]*?)(?=<!-- fcc-end-source -->)/g;
const console$logReg = /(?:\b)console(\.log\S+)/g;
const NBSPReg = new RegExp(String.fromCharCode(160), 'g'); const NBSPReg = new RegExp(String.fromCharCode(160), 'g');
const isJS = matchesProperty('ext', 'js'); const isJS = matchesProperty('ext', 'js');
@ -39,18 +38,6 @@ const testHTML = matchesProperty('ext', 'html');
const testHTMLJS = overSome(isJS, testHTML); const testHTMLJS = overSome(isJS, testHTML);
export const testJS$JSX = overSome(isJS, matchesProperty('ext', 'jsx')); export const testJS$JSX = overSome(isJS, matchesProperty('ext', 'jsx'));
// if shouldProxyConsole then we change instances of console log
// to `window.__console.log`
// this let's us tap into logging into the console.
// currently we only do this to the main window and not the test window
export const proxyLoggerTransformer = partial(
vinyl.transformHeadTailAndContents,
source =>
source.replace(console$logReg, (match, methodCall) => {
return 'window.__console' + methodCall;
})
);
export const replaceNBSP = cond([ export const replaceNBSP = cond([
[ [
testHTMLJS, testHTMLJS,

View File

@ -26,6 +26,7 @@ import {
initConsole, initConsole,
updateConsole, updateConsole,
initLogs, initLogs,
updateLogs,
logsToConsole, logsToConsole,
checkChallenge, checkChallenge,
updateTests, updateTests,
@ -53,7 +54,7 @@ function updateMainEpic(actions, { getState }, { document }) {
ofType(types.updateFile, types.challengeMounted), ofType(types.updateFile, types.challengeMounted),
debounceTime(executeDebounceTimeout), debounceTime(executeDebounceTimeout),
switchMap(() => switchMap(() =>
buildFromFiles(getState(), true).pipe( buildFromFiles(getState()).pipe(
map(frameMain), map(frameMain),
ignoreElements(), ignoreElements(),
startWith(initConsole('')), startWith(initConsole('')),
@ -71,13 +72,12 @@ function executeChallengeEpic(action$, { getState }, { document }) {
filter(Boolean), filter(Boolean),
switchMap(() => { switchMap(() => {
const frameReady = new Subject(); const frameReady = new Subject();
// Removed for investigation into freeCodeCamp/Learn#291 const proxyLogger = new Subject();
// const proxyLogger = new Subject();
const frameTests = createTestFramer( const frameTests = createTestFramer(
document, document,
getState, getState,
frameReady frameReady,
// proxyLogger proxyLogger
); );
const challengeResults = frameReady.pipe( const challengeResults = frameReady.pipe(
pluck('checkChallengePayload'), pluck('checkChallengePayload'),
@ -114,7 +114,7 @@ function executeChallengeEpic(action$, { getState }, { document }) {
const build = const build =
challengeType === backend challengeType === backend
? buildBackendChallenge(state) ? buildBackendChallenge(state)
: buildFromFiles(state, false); : buildFromFiles(state);
return build.pipe( return build.pipe(
tap(frameTests), tap(frameTests),
ignoreElements(), ignoreElements(),
@ -124,7 +124,8 @@ function executeChallengeEpic(action$, { getState }, { document }) {
); );
}) })
); );
return merge(buildAndFrameChallenge, challengeResults); return merge(buildAndFrameChallenge, challengeResults,
proxyLogger.map(updateLogs));
}) })
); );
} }

View File

@ -1,6 +1,5 @@
import { combineLatest } from 'rxjs/observable/combineLatest'; import { combineLatest } from 'rxjs/observable/combineLatest';
import { map } from 'rxjs/operators/map'; import { map } from 'rxjs/operators/map';
import identity from 'lodash/identity';
import { fetchScript } from './fetch-and-cache.js'; import { fetchScript } from './fetch-and-cache.js';
import throwers from '../rechallenge/throwers'; import throwers from '../rechallenge/throwers';
@ -12,7 +11,6 @@ import {
} from '../redux'; } from '../redux';
import { import {
applyTransformers, applyTransformers,
proxyLoggerTransformer,
testJS$JSX testJS$JSX
} from '../rechallenge/transformers'; } from '../rechallenge/transformers';
import { cssToHtml, jsToHtml, concatHtml } from '../rechallenge/builders.js'; import { cssToHtml, jsToHtml, concatHtml } from '../rechallenge/builders.js';
@ -40,7 +38,7 @@ function filterJSIfDisabled(state) {
return file => !(testJS$JSX(file) && !isJSEnabled); return file => !(testJS$JSX(file) && !isJSEnabled);
} }
export function buildFromFiles(state, shouldProxyConsole) { export function buildFromFiles(state) {
const files = challengeFilesSelector(state); const files = challengeFilesSelector(state);
const { required, template } = challengeMetaSelector(state); const { required, template } = challengeMetaSelector(state);
const finalRequires = [...globalRequires, ...required]; const finalRequires = [...globalRequires, ...required];
@ -51,7 +49,6 @@ export function buildFromFiles(state, shouldProxyConsole) {
return createFileStream(requiredFiles) return createFileStream(requiredFiles)
::pipe(throwers) ::pipe(throwers)
::pipe(applyTransformers) ::pipe(applyTransformers)
::pipe(shouldProxyConsole ? proxyLoggerTransformer : identity)
::pipe(jsToHtml) ::pipe(jsToHtml)
::pipe(cssToHtml) ::pipe(cssToHtml)
::concatHtml(finalRequires, template); ::concatHtml(finalRequires, template);

View File

@ -84,8 +84,7 @@ const addDepsToDocument = ctx => {
const buildProxyConsole = proxyLogger => ctx => { const buildProxyConsole = proxyLogger => ctx => {
const oldLog = ctx.window.console.log.bind(ctx.window.console); const oldLog = ctx.window.console.log.bind(ctx.window.console);
ctx.window.__console = {}; ctx.window.console.log = function proxyConsole(...args) {
ctx.window.__console.log = function proxyConsole(...args) {
proxyLogger.next(args); proxyLogger.next(args);
return oldLog(...args); return oldLog(...args);
}; };
@ -140,11 +139,12 @@ export const createMainFramer = (document, getState, proxyLogger) =>
writeContentToFrame writeContentToFrame
); );
export const createTestFramer = (document, getState, frameReady) => export const createTestFramer = (document, getState, frameReady, proxyLogger) =>
flow( flow(
createFrame(document, getState, testId), createFrame(document, getState, testId),
mountFrame(document), mountFrame(document),
addDepsToDocument, addDepsToDocument,
writeTestDepsToDocument(frameReady), writeTestDepsToDocument(frameReady),
buildProxyConsole(proxyLogger),
writeContentToFrame writeContentToFrame
); );