test: introduce --trial-run mode to capture test model (#3436)
This commit is contained in:
parent
515665908e
commit
5498ed10a8
|
|
@ -31,13 +31,17 @@ program
|
||||||
.option('-g, --grep <grep>', 'Only run tests matching this string or regexp', '.*')
|
.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('-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('--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)
|
.option('--timeout <timeout>', 'Specify test timeout threshold (in milliseconds), default: 10000', 10000)
|
||||||
.action(async (command) => {
|
.action(async (command) => {
|
||||||
// Collect files
|
// Collect files
|
||||||
const files = collectFiles(path.join(process.cwd(), command.args[0]), command.args.slice(1));
|
const files = collectFiles(path.join(process.cwd(), command.args[0]), command.args.slice(1));
|
||||||
const rootSuite = new Mocha.Suite('', new Mocha.Context(), true);
|
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;
|
let total = 0;
|
||||||
// Build the test model, suite per file.
|
// Build the test model, suite per file.
|
||||||
for (const file of files) {
|
for (const file of files) {
|
||||||
|
|
@ -54,9 +58,11 @@ program
|
||||||
let runner;
|
let runner;
|
||||||
await new Promise(f => {
|
await new Promise(f => {
|
||||||
runner = mocha.run(f);
|
runner = mocha.run(f);
|
||||||
runner.on(constants.EVENT_RUN_BEGIN, () => {
|
if (!command.reporter) {
|
||||||
process.stdout.write(colors.yellow('\u00B7'));
|
runner.on(constants.EVENT_RUN_BEGIN, () => {
|
||||||
});
|
process.stdout.write(colors.yellow('\u00B7'));
|
||||||
|
});
|
||||||
|
}
|
||||||
});
|
});
|
||||||
total += runner.grepTotal(mocha.suite);
|
total += runner.grepTotal(mocha.suite);
|
||||||
|
|
||||||
|
|
@ -64,18 +70,23 @@ program
|
||||||
mocha.suite.title = path.basename(file);
|
mocha.suite.title = path.basename(file);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Now run the tests.
|
||||||
if (rootSuite.hasOnly())
|
if (rootSuite.hasOnly())
|
||||||
rootSuite.filterOnly();
|
rootSuite.filterOnly();
|
||||||
console.log();
|
if (!command.reporter) {
|
||||||
total = Math.min(total, rootSuite.total()); // First accounts for grep, second for only.
|
console.log();
|
||||||
console.log(`Running ${total} tests using ${Math.min(command.jobs, total)} workers`);
|
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,
|
grep: command.grep,
|
||||||
jobs: command.jobs,
|
|
||||||
reporter: command.reporter,
|
reporter: command.reporter,
|
||||||
retries: command.retries,
|
retries: command.retries,
|
||||||
timeout: command.timeout,
|
timeout: command.timeout,
|
||||||
|
trialRun: command.trialRun,
|
||||||
});
|
});
|
||||||
await runner.run(files);
|
await runner.run(files);
|
||||||
await runner.stop();
|
await runner.stop();
|
||||||
|
|
|
||||||
|
|
@ -26,11 +26,11 @@ const constants = Mocha.Runner.constants;
|
||||||
process.setMaxListeners(0);
|
process.setMaxListeners(0);
|
||||||
|
|
||||||
class Runner extends EventEmitter {
|
class Runner extends EventEmitter {
|
||||||
constructor(suite, options) {
|
constructor(suite, jobs, options) {
|
||||||
super();
|
super();
|
||||||
this._suite = suite;
|
this._suite = suite;
|
||||||
|
this._jobs = jobs;
|
||||||
this._options = options;
|
this._options = options;
|
||||||
this._jobs = options.jobs;
|
|
||||||
this._workers = new Set();
|
this._workers = new Set();
|
||||||
this._freeWorkers = [];
|
this._freeWorkers = [];
|
||||||
this._workerClaimers = [];
|
this._workerClaimers = [];
|
||||||
|
|
|
||||||
|
|
@ -64,7 +64,7 @@ let failedWithError = false;
|
||||||
async function runSingleTest(file, options) {
|
async function runSingleTest(file, options) {
|
||||||
let lastOrdinal = -1;
|
let lastOrdinal = -1;
|
||||||
const mocha = new Mocha({
|
const mocha = new Mocha({
|
||||||
ui: fixturesUI.bind(null, false),
|
ui: fixturesUI.bind(null, options.trialRun),
|
||||||
timeout: options.timeout,
|
timeout: options.timeout,
|
||||||
reporter: NullReporter
|
reporter: NullReporter
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue