diff --git a/client/src/client/workers/test-evaluator.js b/client/src/client/workers/test-evaluator.js index f43ecba264..2478c52658 100644 --- a/client/src/client/workers/test-evaluator.js +++ b/client/src/client/workers/test-evaluator.js @@ -8,7 +8,7 @@ self.console.log = function proxyConsole(...args) { return oldLog(...args); }; -onmessage = async e => { +self.onmessage = async e => { /* eslint-disable no-unused-vars */ const { code = '' } = e.data; const assert = chai.assert; @@ -16,8 +16,22 @@ onmessage = async e => { const DeepEqual = (a, b) => JSON.stringify(a) === JSON.stringify(b); /* eslint-enable no-unused-vars */ try { - // eslint-disable-next-line no-eval - const testResult = eval(e.data.script); + let testResult; + let __userCodeWasExecuted = false; + /* eslint-disable no-eval */ + try { + testResult = eval(` + ${e.data.build} + __userCodeWasExecuted = true; + ${e.data.testString} + `); + } catch (err) { + if (__userCodeWasExecuted) { + throw err; + } + testResult = eval(e.data.testString); + } + /* eslint-enable no-eval */ if (typeof testResult === 'function') { await testResult(fileName => __toString(e.data.sources[fileName])); } diff --git a/client/src/templates/Challenges/redux/execute-challenge-saga.js b/client/src/templates/Challenges/redux/execute-challenge-saga.js index 719715ac42..585a4e3462 100644 --- a/client/src/templates/Challenges/redux/execute-challenge-saga.js +++ b/client/src/templates/Challenges/redux/execute-challenge-saga.js @@ -89,7 +89,7 @@ function* executeJSChallengeSaga(proxyLogger) { return yield call(executeTests, async(testString, testTimeout) => { try { return await testWorker.execute( - { script: build + '\n' + testString, code, sources }, + { build, testString, code, sources }, testTimeout ); } finally { diff --git a/curriculum/test/test-challenges.js b/curriculum/test/test-challenges.js index 290b7f6e30..17bdafb91d 100644 --- a/curriculum/test/test-challenges.js +++ b/curriculum/test/test-challenges.js @@ -320,13 +320,12 @@ async function evaluateJsTest({ solution, files, test }) { const { build, sources } = await buildJSChallenge(files); const code = sources && 'index' in sources ? sources['index'] : ''; - const script = build + '\n' + test.testString; const testWorker = createWorker('test-evaluator'); try { const { pass, err } = await testWorker.execute( - { script, code, sources }, + { testString: test.testString, build, code, sources }, 5000 ); if (!pass) {