Skip to content

Commit

Permalink
test: fix fit, do not rely upon mocha suite (#3520)
Browse files Browse the repository at this point in the history
  • Loading branch information
pavelfeldman authored Aug 19, 2020
1 parent e54195c commit b0667e8
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 27 deletions.
4 changes: 2 additions & 2 deletions test/runner/fixturesUI.js
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand All @@ -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])
Expand Down
18 changes: 9 additions & 9 deletions test/runner/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,27 +37,27 @@ 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;
if (command.forbidOnly && testCollector.hasOnly()) {
console.error('=====================================');
console.error(' --forbid-only found a focused test.');
console.error('=====================================');
process.exit(1);
}

const total = rootSuite.total();
if (!total) {
console.error('=================');
console.error(' No tests found.');
console.error(' no tests found.');
console.error('=================');
process.exit(1);
}

// Filter tests.
if (rootSuite.hasOnly())
rootSuite.filterOnly();

// Trial run does not need many workers, use one.
const jobs = (command.trialRun || command.debug) ? 1 : command.jobs;
const runner = new Runner(rootSuite, total, {
Expand Down
40 changes: 24 additions & 16 deletions test/runner/testCollector.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,26 @@ 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;
if (options.grep) {
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,
Expand Down Expand Up @@ -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()) {
Expand All @@ -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) {
Expand All @@ -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 };

0 comments on commit b0667e8

Please sign in to comment.