2020-11-25 19:35:44 +01:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
/* global specs_list */
|
|
|
|
|
2020-11-23 19:21:51 +01:00
|
|
|
$(() => {
|
2020-11-25 19:35:44 +01:00
|
|
|
const stringifyException = (exception) => {
|
2020-11-23 19:21:51 +01:00
|
|
|
let err = exception.stack || exception.toString();
|
2020-06-07 10:53:10 +02:00
|
|
|
|
|
|
|
// FF / Opera do not add the message
|
|
|
|
if (!~err.indexOf(exception.message)) {
|
2020-11-23 19:21:51 +01:00
|
|
|
err = `${exception.message}\n${err}`;
|
2020-06-07 10:53:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// <=IE7 stringifies to [Object Error]. Since it can be overloaded, we
|
|
|
|
// check for the result of the stringifying.
|
2020-11-25 19:35:44 +01:00
|
|
|
if (err === '[object Error]') err = exception.message;
|
2020-06-07 10:53:10 +02:00
|
|
|
|
|
|
|
// Safari doesn't give you a stack. Let's at least provide a source line.
|
|
|
|
if (!exception.stack && exception.sourceURL && exception.line !== undefined) {
|
2020-11-23 19:21:51 +01:00
|
|
|
err += `\n(${exception.sourceURL}:${exception.line})`;
|
2020-06-07 10:53:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
2020-11-25 19:35:44 +01:00
|
|
|
};
|
2020-06-07 10:53:10 +02:00
|
|
|
|
2020-11-25 19:35:44 +01:00
|
|
|
const customRunner = (runner) => {
|
2020-11-23 19:21:51 +01:00
|
|
|
const stats = {suites: 0, tests: 0, passes: 0, pending: 0, failures: 0};
|
2020-11-25 19:35:44 +01:00
|
|
|
let level = 0;
|
2012-11-03 15:31:33 +01:00
|
|
|
|
|
|
|
if (!runner) return;
|
|
|
|
|
2020-11-23 19:21:51 +01:00
|
|
|
runner.on('start', () => {
|
|
|
|
stats.start = new Date();
|
2012-11-03 15:31:33 +01:00
|
|
|
});
|
|
|
|
|
2020-11-23 19:21:51 +01:00
|
|
|
runner.on('suite', (suite) => {
|
2012-11-03 15:31:33 +01:00
|
|
|
suite.root || stats.suites++;
|
2020-06-07 10:53:10 +02:00
|
|
|
if (suite.root) return;
|
|
|
|
append(suite.title);
|
|
|
|
level++;
|
|
|
|
});
|
|
|
|
|
2020-11-23 19:21:51 +01:00
|
|
|
runner.on('suite end', (suite) => {
|
2020-06-07 10:53:10 +02:00
|
|
|
if (suite.root) return;
|
|
|
|
level--;
|
|
|
|
|
2020-11-25 19:35:44 +01:00
|
|
|
if (level === 0) {
|
2020-11-23 19:21:51 +01:00
|
|
|
append('');
|
2020-06-07 10:53:10 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Scroll down test display after each test
|
2020-11-23 19:21:51 +01:00
|
|
|
const mochaEl = $('#mocha')[0];
|
|
|
|
runner.on('test', () => {
|
2020-07-28 20:57:33 +02:00
|
|
|
mochaEl.scrollTop = mochaEl.scrollHeight;
|
2012-11-03 15:31:33 +01:00
|
|
|
});
|
|
|
|
|
2020-07-28 20:57:33 +02:00
|
|
|
// max time a test is allowed to run
|
|
|
|
// TODO this should be lowered once timeslider_revision.js is faster
|
2020-11-23 19:21:51 +01:00
|
|
|
let killTimeout;
|
|
|
|
runner.on('test end', () => {
|
2012-11-03 15:31:33 +01:00
|
|
|
stats.tests++;
|
2020-07-28 20:57:33 +02:00
|
|
|
});
|
2020-06-07 10:53:10 +02:00
|
|
|
|
2020-11-23 19:21:51 +01:00
|
|
|
runner.on('pass', (test) => {
|
|
|
|
if (killTimeout) clearTimeout(killTimeout);
|
|
|
|
killTimeout = setTimeout(() => {
|
|
|
|
append('FINISHED - [red]no test started since 3 minutes, tests stopped[clear]');
|
2020-06-07 10:53:10 +02:00
|
|
|
}, 60000 * 3);
|
2012-11-03 15:31:33 +01:00
|
|
|
|
2020-11-23 19:21:51 +01:00
|
|
|
const medium = test.slow() / 2;
|
2012-11-03 15:31:33 +01:00
|
|
|
test.speed = test.duration > test.slow()
|
|
|
|
? 'slow'
|
|
|
|
: test.duration > medium
|
|
|
|
? 'medium'
|
|
|
|
: 'fast';
|
|
|
|
|
|
|
|
stats.passes++;
|
2020-11-25 19:52:47 +01:00
|
|
|
append(`-> [green]PASSED[clear] : ${test.title} ${test.duration} ms`);
|
2012-11-03 15:31:33 +01:00
|
|
|
});
|
|
|
|
|
2020-11-23 19:21:51 +01:00
|
|
|
runner.on('fail', (test, err) => {
|
|
|
|
if (killTimeout) clearTimeout(killTimeout);
|
|
|
|
killTimeout = setTimeout(() => {
|
|
|
|
append('FINISHED - [red]no test started since 3 minutes, tests stopped[clear]');
|
2020-07-28 20:57:33 +02:00
|
|
|
}, 60000 * 3);
|
|
|
|
|
2012-11-03 15:31:33 +01:00
|
|
|
stats.failures++;
|
|
|
|
test.err = err;
|
2020-11-25 19:52:47 +01:00
|
|
|
append(`-> [red]FAILED[clear] : ${test.title} ${stringifyException(test.err)}`);
|
2012-11-03 15:31:33 +01:00
|
|
|
});
|
|
|
|
|
2020-11-23 19:21:51 +01:00
|
|
|
runner.on('pending', (test) => {
|
|
|
|
if (killTimeout) clearTimeout(killTimeout);
|
|
|
|
killTimeout = setTimeout(() => {
|
|
|
|
append('FINISHED - [red]no test started since 3 minutes, tests stopped[clear]');
|
2020-07-28 20:57:33 +02:00
|
|
|
}, 60000 * 3);
|
2012-11-03 15:31:33 +01:00
|
|
|
|
|
|
|
stats.pending++;
|
2020-11-25 19:52:47 +01:00
|
|
|
append(`-> [yellow]PENDING[clear]: ${test.title}`);
|
2012-11-03 15:31:33 +01:00
|
|
|
});
|
2012-11-18 12:49:34 +01:00
|
|
|
|
2020-11-23 19:21:51 +01:00
|
|
|
const $console = $('#console');
|
2020-11-25 19:52:47 +01:00
|
|
|
const append = (text) => {
|
2020-11-23 19:21:51 +01:00
|
|
|
const oldText = $console.text();
|
2012-10-28 14:16:41 +01:00
|
|
|
|
2020-11-23 19:21:51 +01:00
|
|
|
let space = '';
|
|
|
|
for (let i = 0; i < level * 2; i++) {
|
|
|
|
space += ' ';
|
2012-10-28 14:16:41 +01:00
|
|
|
}
|
|
|
|
|
2020-11-23 19:21:51 +01:00
|
|
|
let splitedText = '';
|
|
|
|
_(text.split('\n')).each((line) => {
|
|
|
|
while (line.length > 0) {
|
|
|
|
const split = line.substr(0, 100);
|
|
|
|
line = line.substr(100);
|
|
|
|
if (splitedText.length > 0) splitedText += '\n';
|
|
|
|
splitedText += split;
|
2012-11-03 15:31:33 +01:00
|
|
|
}
|
2012-10-28 14:16:41 +01:00
|
|
|
});
|
2020-11-23 19:21:51 +01:00
|
|
|
|
|
|
|
// indent all lines with the given amount of space
|
|
|
|
const newText = _(splitedText.split('\n')).map((line) => space + line).join('\\n');
|
|
|
|
|
|
|
|
$console.text(`${oldText + newText}\\n`);
|
|
|
|
};
|
|
|
|
|
|
|
|
const total = runner.total;
|
|
|
|
runner.on('end', () => {
|
|
|
|
stats.end = new Date();
|
|
|
|
stats.duration = stats.end - stats.start;
|
|
|
|
const minutes = Math.floor(stats.duration / 1000 / 60);
|
2020-11-25 19:35:44 +01:00
|
|
|
// chrome < 57 does not like this .toString().padStart('2', '0');
|
|
|
|
const seconds = Math.round((stats.duration / 1000) % 60);
|
2020-11-23 19:21:51 +01:00
|
|
|
if (stats.tests === total) {
|
2020-11-25 19:52:47 +01:00
|
|
|
append(`FINISHED - ${stats.passes} tests passed, ${stats.failures} tests failed, ` +
|
|
|
|
`${stats.pending} pending, duration: ${minutes}:${seconds}`);
|
2020-11-23 19:21:51 +01:00
|
|
|
} else if (stats.tests > total) {
|
2020-11-25 19:52:47 +01:00
|
|
|
append(`FINISHED - but more tests than planned returned ${stats.passes} tests passed, ` +
|
|
|
|
`${stats.failures} tests failed, ${stats.pending} pending, ` +
|
|
|
|
`duration: ${minutes}:${seconds}`);
|
|
|
|
append(`${total} tests, but ${stats.tests} returned. ` +
|
|
|
|
'There is probably a problem with your async code or error handling, ' +
|
|
|
|
'see https://github.com/mochajs/mocha/issues/1327');
|
2020-11-23 19:21:51 +01:00
|
|
|
} else {
|
2020-11-25 19:52:47 +01:00
|
|
|
append(`FINISHED - but not all tests returned ${stats.passes} tests passed, ` +
|
|
|
|
`${stats.failures} tests failed, ${stats.pending} tests pending, ` +
|
|
|
|
`duration: ${minutes}:${seconds}`);
|
|
|
|
append(`${total} tests, but only ${stats.tests} returned. ` +
|
|
|
|
'Check for failed before/beforeEach-hooks (no `test end` is called for them ' +
|
|
|
|
'and subsequent tests of the same suite are skipped), ' +
|
|
|
|
'see https://github.com/mochajs/mocha/pull/1043');
|
2020-11-23 19:21:51 +01:00
|
|
|
}
|
|
|
|
});
|
2020-11-25 19:35:44 +01:00
|
|
|
};
|
2012-10-28 14:16:41 +01:00
|
|
|
|
2020-11-25 19:18:11 +01:00
|
|
|
const getURLParameter = (name) => (new URLSearchParams(location.search)).get(name);
|
2020-11-23 19:21:51 +01:00
|
|
|
|
|
|
|
// get the list of specs and filter it if requested
|
|
|
|
const specs = specs_list.slice();
|
|
|
|
|
|
|
|
// inject spec scripts into the dom
|
|
|
|
const $body = $('body');
|
|
|
|
$.each(specs, (i, spec) => {
|
2020-11-25 19:35:44 +01:00
|
|
|
// if the spec isn't a plugin spec which means the spec file might be in a different subfolder
|
|
|
|
if (!spec.startsWith('/')) {
|
2020-11-23 19:21:51 +01:00
|
|
|
$body.append(`<script src="specs/${spec}"></script>`);
|
|
|
|
} else {
|
|
|
|
$body.append(`<script src="${spec}"></script>`);
|
2013-02-04 01:03:25 +01:00
|
|
|
}
|
2012-10-27 18:05:26 +02:00
|
|
|
});
|
|
|
|
|
2020-11-23 19:21:51 +01:00
|
|
|
// initalize the test helper
|
|
|
|
helper.init(() => {
|
|
|
|
// configure and start the test framework
|
|
|
|
const grep = getURLParameter('grep');
|
2020-11-25 19:18:11 +01:00
|
|
|
if (grep != null) {
|
2012-10-27 18:50:59 +02:00
|
|
|
mocha.grep(grep);
|
|
|
|
}
|
2012-10-28 14:16:41 +01:00
|
|
|
|
2020-11-23 19:21:51 +01:00
|
|
|
const runner = mocha.run();
|
2020-11-25 19:35:44 +01:00
|
|
|
customRunner(runner);
|
2012-10-08 00:34:29 +02:00
|
|
|
});
|
2020-03-24 01:04:24 +01:00
|
|
|
});
|