From f4e1fe11fca517d86238db9829cd56d913e3decb Mon Sep 17 00:00:00 2001 From: Oliver Eyton-Williams Date: Thu, 25 Feb 2021 15:39:28 +0100 Subject: [PATCH] fix: error reporting (#41249) --- client/src/client/frame-runner.js | 15 ++++++++------- curriculum/test/test-challenges.js | 23 ++++++++++------------- 2 files changed, 18 insertions(+), 20 deletions(-) diff --git a/client/src/client/frame-runner.js b/client/src/client/frame-runner.js index e2bf59931f..e022770b5d 100644 --- a/client/src/client/frame-runner.js +++ b/client/src/client/frame-runner.js @@ -82,15 +82,13 @@ async function initTestFrame(e = { code: {} }) { try { // eslint-disable-next-line no-eval const test = eval(testString); - resolve({ test }); + resolve(test); } catch (err) { - reject({ err }); + reject(err); } }) ); - const { test, err } = await testPromise; - if (err) throw err; - + const test = await testPromise; if (typeof test === 'function') { await test(e.getUserInput); } @@ -99,8 +97,11 @@ async function initTestFrame(e = { code: {} }) { if (!(err instanceof chai.AssertionError)) { console.error(err); } - // return the error so that the curriculum tests are more informative - return { err }; + // to provide useful debugging information when debugging the tests, we + // have to extract the message and stack before returning + return { + err: { message: err.message, stack: err.stack } + }; } }; } diff --git a/curriculum/test/test-challenges.js b/curriculum/test/test-challenges.js index 0ea05a44d1..8c0cf41ee0 100644 --- a/curriculum/test/test-challenges.js +++ b/curriculum/test/test-challenges.js @@ -528,14 +528,21 @@ async function createTestRunner( try { const { pass, err } = await evaluator.evaluate(testString, 5000); if (!pass) { - throw new AssertionError(err.message); + throw err; } } catch (err) { + // add more info to the error so the failing test can be identified. text = 'Test text: ' + text; - const message = solutionFromNext + const newMessage = solutionFromNext ? 'Check next step for solution!\n' + text : text; - reThrow(err, message); + // if the stack is missing, the message should be included. Otherwise it + // is redundant. + err.message = err.stack + ? newMessage + : `${newMessage} + ${err.message}`; + throw err; } }; } @@ -578,13 +585,3 @@ async function initializeTestRunner(build, sources, code, loadEnzyme) { loadEnzyme ); } - -function reThrow(err, text) { - const newMessage = `${text} - ${err.message}`; - if (err.name === 'AssertionError') { - throw new AssertionError(newMessage); - } else { - throw Error(newMessage); - } -}