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]) if (specs.slow && specs.slow[0])
test.timeout(90000); test.timeout(90000);
if (only) if (only)
test.markOnly(); test.__only = true;
if (!only && specs.skip && specs.skip[0]) if (!only && specs.skip && specs.skip[0])
test.pending = true; test.pending = true;
if (!only && specs.fail && specs.fail[0]) 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]; const only = wrappers.ignoreOnly ? false : specs.only && specs.only[0];
if (only) if (only)
suite.markOnly(); suite.__only = true;
if (!only && specs.skip && specs.skip[0]) if (!only && specs.skip && specs.skip[0])
suite.pending = true; suite.pending = true;
if (!only && specs.fail && specs.fail[0]) if (!only && specs.fail && specs.fail[0])

View File

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

View File

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