From daed7ad5dd4c86ea88932d2e9ed8f184bb5a603b Mon Sep 17 00:00:00 2001 From: Oliver Eyton-Williams Date: Mon, 15 Jun 2020 15:11:34 +0200 Subject: [PATCH] fix(client): ensure dom ready before testing (#39073) jQuery challenges can fail seemingly at random. These changes should prevent the race condition between a user's $( document ).ready() and test evalution. --- client/src/client/frame-runner.js | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/client/src/client/frame-runner.js b/client/src/client/frame-runner.js index 16da84efca3..ec0433b6fd1 100644 --- a/client/src/client/frame-runner.js +++ b/client/src/client/frame-runner.js @@ -59,8 +59,22 @@ async function initTestFrame(e = {}) { // eval test string to actual JavaScript // This return can be a function // i.e. function() { assert(true, 'happy coding'); } - // eslint-disable-next-line no-eval - const test = eval(testString); + const testPromise = new Promise((resolve, reject) => + // To avoid race conditions, we have to run the test in a final + // document ready: + $(() => { + try { + // eslint-disable-next-line no-eval + const test = eval(testString); + resolve({ test }); + } catch (err) { + reject({ err }); + } + }) + ); + const { test, err } = await testPromise; + if (err) throw err; + if (typeof test === 'function') { await test(e.getUserInput); }