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.
This commit is contained in:
Oliver Eyton-Williams
2020-06-15 15:11:34 +02:00
committed by GitHub
parent 3fed51a3df
commit daed7ad5dd

View File

@@ -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);
}