fix(tests): tests optimization

This commit is contained in:
Valeriy
2019-01-17 02:50:12 +03:00
committed by mrugesh mohapatra
parent 44e9ffb6f2
commit b0f19aa3f6

View File

@ -191,7 +191,7 @@ function populateTestsForLang({ lang, challenges }) {
}); });
let { files = [] } = challenge; let { files = [] } = challenge;
let evaluateTest; let createTestRunner;
if (challengeType === challengeTypes.backend) { if (challengeType === challengeTypes.backend) {
it('Check tests is not implemented.'); it('Check tests is not implemented.');
return; return;
@ -199,9 +199,9 @@ function populateTestsForLang({ lang, challenges }) {
challengeType === challengeTypes.js || challengeType === challengeTypes.js ||
challengeType === challengeTypes.bonfire challengeType === challengeTypes.bonfire
) { ) {
evaluateTest = evaluateJsTest; createTestRunner = createTestRunnerForJSChallenge;
} else if (files.length === 1) { } else if (files.length === 1) {
evaluateTest = evaluateHtmlTest; createTestRunner = createTestRunnerForDOMChallenge;
} else { } else {
it('Check tests.', () => { it('Check tests.', () => {
throw new Error('Seed file should be only the one.'); throw new Error('Seed file should be only the one.');
@ -211,27 +211,31 @@ function populateTestsForLang({ lang, challenges }) {
files = files.map(createPoly); files = files.map(createPoly);
it('Test suite must fail on the initial contents', async function() { it('Test suite must fail on the initial contents', async function() {
this.timeout(20000); this.timeout(5000 * tests.length + 1000);
// suppress errors in the console. // suppress errors in the console.
const oldConsoleError = console.error; const oldConsoleError = console.error;
console.error = () => {}; console.error = () => {};
let fails = (await Promise.all( let fails = false;
tests.map(async function(test) { let testRunner;
try {
testRunner = await createTestRunner(
{ ...challenge, files },
'',
page
);
} catch {
fails = true;
}
if (!fails) {
for (const test of tests) {
try { try {
await evaluateTest( await testRunner(test);
{
...challenge,
files,
test
},
page
);
return false;
} catch (e) { } catch (e) {
return true; fails = true;
break;
} }
}) }
)).some(v => v); }
console.error = oldConsoleError; console.error = oldConsoleError;
assert(fails, 'Test suit does not fail on the initial contents'); assert(fails, 'Test suit does not fail on the initial contents');
}); });
@ -249,20 +253,16 @@ function populateTestsForLang({ lang, challenges }) {
describe('Check tests against solutions', function() { describe('Check tests against solutions', function() {
solutions.forEach((solution, index) => { solutions.forEach((solution, index) => {
describe(`Solution ${index + 1}`, function() { it(`Solution ${index + 1}`, async function() {
tests.forEach(test => { this.timeout(5000 * tests.length + 1000);
it(test.text, async function() { const testRunner = await createTestRunner(
await evaluateTest( { ...challenge, files },
{ solution,
...challenge, page
files, );
solution, for (const test of tests) {
test await testRunner(test);
}, }
page
);
});
});
}); });
}); });
}); });
@ -271,8 +271,9 @@ function populateTestsForLang({ lang, challenges }) {
}); });
} }
async function evaluateHtmlTest( async function createTestRunnerForDOMChallenge(
{ solution, required = [], template, files, test }, { required = [], template, files },
solution,
context context
) { ) {
if (solution) { if (solution) {
@ -288,32 +289,39 @@ async function evaluateHtmlTest(
await context.reload(); await context.reload();
await context.setContent(build); await context.setContent(build);
await context.evaluate(
const result = await context.evaluate( async(sources, loadEnzyme) => {
async(sources, testString, loadEnzyme) => {
document.__source = sources && 'index' in sources ? sources['index'] : ''; document.__source = sources && 'index' in sources ? sources['index'] : '';
document.__getUserInput = fileName => sources[fileName]; document.__getUserInput = fileName => sources[fileName];
document.__frameReady = () => {}; document.__frameReady = () => {};
document.__loadEnzyme = loadEnzyme; document.__loadEnzyme = loadEnzyme;
await document.__initTestFrame(); await document.__initTestFrame();
const { pass, err } = await document.__runTest(testString);
if (pass) {
return true;
} else {
return { ...err };
}
}, },
sources, sources,
test.testString,
loadEnzyme loadEnzyme
); );
if (result !== true) {
throw AssertionError(result.message); return async({ text, testString }) => {
} try {
const { pass, err } = await Promise.race([
new Promise((_, reject) => setTimeout(() => reject('timeout'), 5000)),
await context.evaluate(async testString => {
return await document.__runTest(testString);
}, testString)
]);
if (!pass) {
throw AssertionError(`${text}\n${err.message}`);
}
} catch (err) {
throw typeof err === 'string'
? `${text}\n${err}`
: (err.message = `${text}
${err.message}`);
}
};
} }
async function evaluateJsTest({ solution, files, test }) { async function createTestRunnerForJSChallenge({ files }, solution) {
if (solution) { if (solution) {
files[0].contents = solution; files[0].contents = solution;
} }
@ -322,16 +330,22 @@ async function evaluateJsTest({ solution, files, test }) {
const code = sources && 'index' in sources ? sources['index'] : ''; const code = sources && 'index' in sources ? sources['index'] : '';
const testWorker = createWorker('test-evaluator'); const testWorker = createWorker('test-evaluator');
return async({ text, testString }) => {
try { try {
const { pass, err } = await testWorker.execute( const { pass, err } = await testWorker.execute(
{ testString: test.testString, build, code, sources }, { testString, build, code, sources },
5000 5000
); );
if (!pass) { if (!pass) {
throw new AssertionError(err.message); throw new AssertionError(`${text}\n${err.message}`);
}
} catch (err) {
throw typeof err === 'string'
? `${text}\n${err}`
: (err.message = `${text}
${err.message}`);
} finally {
testWorker.killWorker();
} }
} finally { };
testWorker.killWorker();
}
} }