fix(client): log to console saga

This commit is contained in:
Valeriy S
2018-12-10 08:22:32 +03:00
committed by Stuart Taylor
parent a50a048ee7
commit 0775766702
3 changed files with 23 additions and 28 deletions

View File

@ -10,13 +10,6 @@ if (window.frameElement && window.frameElement.id === testId) {
} }
function initTestFrame() { function initTestFrame() {
// window.__logs = [];
// const oldLog = window.console.log.bind(window.console);
// window.console.log = function proxyConsole(...args) {
// window.__logs = [...window.__logs, ...args];
// return oldLog(...args);
// };
const frameReady = document.__frameReady; const frameReady = document.__frameReady;
const source = document.__source; const source = document.__source;
const __getUserInput = document.__getUserInput || (x => x); const __getUserInput = document.__getUserInput || (x => x);
@ -49,7 +42,6 @@ function initTestFrame() {
} }
document.__runTest = async function runTests(testString) { document.__runTest = async function runTests(testString) {
window.__logs = [];
/* eslint-disable no-unused-vars */ /* eslint-disable no-unused-vars */
const code = source.slice(0); const code = source.slice(0);
const editor = { const editor = {
@ -72,7 +64,7 @@ function initTestFrame() {
if (typeof test === 'function') { if (typeof test === 'function') {
await test(getUserInput); await test(getUserInput);
} }
return { pass: true, logs: window.__logs.map(String) }; return { pass: true };
} catch (err) { } catch (err) {
if (!(err instanceof chai.AssertionError)) { if (!(err instanceof chai.AssertionError)) {
console.error(err); console.error(err);
@ -81,8 +73,7 @@ function initTestFrame() {
err: { err: {
message: err.message, message: err.message,
stack: err.stack stack: err.stack
}, }
logs: window.__logs.map(String)
}; };
} }
}; };

View File

@ -1,5 +1,13 @@
import { put, select, call, takeLatest, race } from 'redux-saga/effects'; import {
import { delay } from 'redux-saga'; put,
select,
call,
takeLatest,
takeEvery,
race,
fork
} from 'redux-saga/effects';
import { delay, channel } from 'redux-saga';
import { import {
challengeMetaSelector, challengeMetaSelector,
@ -109,36 +117,31 @@ function createTestFrame(state, ctx, proxyLogger) {
}).then(() => console.log('Frame ready')); }).then(() => console.log('Frame ready'));
} }
function* proxyLogger() { function* logToConsole(channel) {
let args = yield; yield takeEvery(channel, function*(args) {
while (true) { yield put(updateLogs(args));
args = yield put(updateLogs(args)); });
}
} }
function* ExecuteDOMChallengeSaga(tests) { function* ExecuteDOMChallengeSaga(tests) {
const testResults = []; const testResults = [];
const state = yield select(); const state = yield select();
const ctx = yield call(buildFromFiles, state); const ctx = yield call(buildFromFiles, state);
const proxy = proxyLogger(); const consoleProxy = yield channel();
proxy.next('1'); yield fork(logToConsole, consoleProxy);
proxy.next('2');
proxy.next('3'); yield call(createTestFrame, state, ctx, consoleProxy);
yield call(createTestFrame, state, ctx, proxy);
for (const { text, testString } of tests) { for (const { text, testString } of tests) {
const newTest = { text, testString }; const newTest = { text, testString };
try { try {
const [{ pass, err, logs }, timeout] = yield race([ const [{ pass, err }, timeout] = yield race([
call(runTestInTestFrame, document, testString), call(runTestInTestFrame, document, testString),
delay(testTimeout, 'timeout') delay(testTimeout, 'timeout')
]); ]);
if (timeout) { if (timeout) {
throw timeout; throw timeout;
} }
for (const log of logs) {
yield put(updateLogs(log));
}
if (pass) { if (pass) {
newTest.pass = true; newTest.pass = true;
} else { } else {
@ -160,6 +163,7 @@ function* ExecuteDOMChallengeSaga(tests) {
testResults.push(newTest); testResults.push(newTest);
} }
} }
consoleProxy.close();
return testResults; return testResults;
} }

View File

@ -77,7 +77,7 @@ const mountFrame = document => ({ element, ...rest }) => {
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.log = function proxyConsole(...args) { ctx.window.console.log = function proxyConsole(...args) {
proxyLogger.next(args); proxyLogger.put(args);
return oldLog(...args); return oldLog(...args);
}; };
return ctx; return ctx;