test: introduce --trial-run mode to capture test model (#3436)

This commit is contained in:
Pavel Feldman 2020-08-13 11:48:58 -07:00 committed by GitHub
parent 515665908e
commit 5498ed10a8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 23 additions and 12 deletions

View File

@ -31,13 +31,17 @@ program
.option('-g, --grep <grep>', 'Only run tests matching this string or regexp', '.*')
.option('-j, --jobs <jobs>', 'Number of concurrent jobs for --parallel; use 1 to run in serial, default: (number of CPU cores / 2)', Math.ceil(require('os').cpus().length / 2))
.option('--reporter <reporter>', 'Specify reporter to use', '')
.option('--trial-run', 'Only collect the matching tests and report them as passing')
.option('--timeout <timeout>', 'Specify test timeout threshold (in milliseconds), default: 10000', 10000)
.action(async (command) => {
// Collect files
const files = collectFiles(path.join(process.cwd(), command.args[0]), command.args.slice(1));
const rootSuite = new Mocha.Suite('', new Mocha.Context(), true);
console.log(`Parsing ${files.length} test files`);
if (!command.reporter) {
// TODO: extend reporter interface.
console.log(`Parsing ${files.length} test files`);
}
let total = 0;
// Build the test model, suite per file.
for (const file of files) {
@ -54,9 +58,11 @@ program
let runner;
await new Promise(f => {
runner = mocha.run(f);
runner.on(constants.EVENT_RUN_BEGIN, () => {
process.stdout.write(colors.yellow('\u00B7'));
});
if (!command.reporter) {
runner.on(constants.EVENT_RUN_BEGIN, () => {
process.stdout.write(colors.yellow('\u00B7'));
});
}
});
total += runner.grepTotal(mocha.suite);
@ -64,18 +70,23 @@ program
mocha.suite.title = path.basename(file);
}
// Now run the tests.
if (rootSuite.hasOnly())
rootSuite.filterOnly();
console.log();
total = Math.min(total, rootSuite.total()); // First accounts for grep, second for only.
console.log(`Running ${total} tests using ${Math.min(command.jobs, total)} workers`);
if (!command.reporter) {
console.log();
total = Math.min(total, rootSuite.total()); // First accounts for grep, second for only.
console.log(`Running ${total} tests using ${Math.min(command.jobs, total)} workers`);
}
const runner = new Runner(rootSuite, {
// Trial run does not need many workers, use one.
const jobs = command.trialRun ? 1 : command.jobs;
const runner = new Runner(rootSuite, jobs, {
grep: command.grep,
jobs: command.jobs,
reporter: command.reporter,
retries: command.retries,
timeout: command.timeout,
trialRun: command.trialRun,
});
await runner.run(files);
await runner.stop();

View File

@ -26,11 +26,11 @@ const constants = Mocha.Runner.constants;
process.setMaxListeners(0);
class Runner extends EventEmitter {
constructor(suite, options) {
constructor(suite, jobs, options) {
super();
this._suite = suite;
this._jobs = jobs;
this._options = options;
this._jobs = options.jobs;
this._workers = new Set();
this._freeWorkers = [];
this._workerClaimers = [];

View File

@ -64,7 +64,7 @@ let failedWithError = false;
async function runSingleTest(file, options) {
let lastOrdinal = -1;
const mocha = new Mocha({
ui: fixturesUI.bind(null, false),
ui: fixturesUI.bind(null, options.trialRun),
timeout: options.timeout,
reporter: NullReporter
});