test: fix fit, do not rely upon mocha suite (#3520)

This commit is contained in:
Pavel Feldman 2020-08-18 18:55:26 -07:00 committed by GitHub
parent e54195ccfb
commit b0667e8bc5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 38 additions and 30 deletions

View File

@ -80,7 +80,7 @@ function fixturesUI(wrappers, suite) {
if (specs.slow && specs.slow[0])
test.timeout(90000);
if (only)
test.markOnly();
test.__only = true;
if (!only && specs.skip && specs.skip[0])
test.pending = true;
if (!only && specs.fail && specs.fail[0])
@ -96,7 +96,7 @@ function fixturesUI(wrappers, suite) {
});
const only = wrappers.ignoreOnly ? false : specs.only && specs.only[0];
if (only)
suite.markOnly();
suite.__only = true;
if (!only && specs.skip && specs.skip[0])
suite.pending = true;
if (!only && specs.fail && specs.fail[0])

View File

@ -37,26 +37,26 @@ program
const testDir = path.join(process.cwd(), command.args[0]);
const files = collectFiles(testDir, '', command.args.slice(1));
const testCollector = new TestCollector({
const testCollector = new TestCollector(files, {
forbidOnly: command.forbidOnly || undefined,
grep: command.grep,
timeout: command.timeout,
});
for (const file of files)
testCollector.addFile(file);
const rootSuite = testCollector.suite;
const total = rootSuite.total();
if (!total) {
console.error('=================');
console.error(' No tests found.');
console.error('=================');
if (command.forbidOnly && testCollector.hasOnly()) {
console.error('=====================================');
console.error(' --forbid-only found a focused test.');
console.error('=====================================');
process.exit(1);
}
// Filter tests.
if (rootSuite.hasOnly())
rootSuite.filterOnly();
const total = rootSuite.total();
if (!total) {
console.error('=================');
console.error(' no tests found.');
console.error('=================');
process.exit(1);
}
// Trial run does not need many workers, use one.
const jobs = (command.trialRun || command.debug) ? 1 : command.jobs;

View File

@ -24,7 +24,7 @@ global.testOptions = require('./testOptions');
class NullReporter {}
class TestCollector {
constructor(options) {
constructor(files, options) {
this._options = options;
this.suite = new Mocha.Suite('', new Mocha.Context(), true);
this._total = 0;
@ -32,9 +32,18 @@ class TestCollector {
const match = options.grep.match(/^\/(.*)\/(g|i|)$|.*/);
this._grep = new RegExp(match[1] || match[0], match[2]);
}
for (const file of files)
this._addFile(file);
this._hasOnly = this._filterOnly(this.suite);
}
addFile(file) {
hasOnly() {
return this._hasOnly;
}
_addFile(file) {
const mocha = new Mocha({
forbidOnly: this._options.forbidOnly,
reporter: NullReporter,
@ -90,9 +99,6 @@ class TestCollector {
}
});
if (mocha.suite.hasOnly())
mocha.suite.filterOnly();
// Clone the suite as many times as there are worker hashes.
// Only include the tests that requested these generations.
for (const [hash, {configurationObject, configurationString, tests}] of workerGeneratorConfigurations.entries()) {
@ -104,7 +110,7 @@ class TestCollector {
_cloneSuite(suite, configurationObject, configurationString, tests) {
const copy = suite.clone();
copy.__configurationObject = configurationObject;
copy.__only = suite.__only;
for (const child of suite.suites)
copy.addSuite(this._cloneSuite(child, configurationObject, configurationString, tests));
for (const test of suite.tests) {
@ -113,23 +119,25 @@ class TestCollector {
if (this._grep && !this._grep.test(test.fullTitle()))
continue;
const testCopy = test.clone();
testCopy.__only = test.__only;
testCopy.__ordinal = test.__ordinal;
testCopy.__configurationObject = configurationObject;
testCopy.__configurationString = configurationString;
copy.addTest(testCopy);
}
return copy;
}
}
}
function grepTotal(mocha, suite) {
let total = 0;
suite.eachTest(test => {
if (mocha.options.grep.test(test.fullTitle()))
total++;
});
return total;
_filterOnly(suite) {
const onlySuites = suite.suites.filter(child => this._filterOnly(child) || child.__only);
const onlyTests = suite.tests.filter(test => test.__only);
if (onlySuites.length || onlyTests.length) {
suite.suites = onlySuites;
suite.tests = onlyTests;
return true;
}
return false;
}
}
module.exports = { TestCollector };